在 API 测试控制台和 Python 2.7 中对视频使用 Microsoft Emotion API 时订阅密钥无效

Iinvalid subscription key when using Microsoft Emotion API for Video in API Testing Console and in Python 2.7

我只设法将 Emotion API 订阅密钥用于图片,但从未用于视频。无论我使用 API 测试控制台还是尝试通过 Pathon 2.7 调用 Emotion API 都没有区别。在这两种情况下,我都会收到响应状态 202 Accepted,但是在打开 Operation-Location 时显示

{ "error": { "code": "Unauthorized", "message": "Access denied due to 
invalid subscription key. Make sure you are subscribed to an API you are 
trying to call and provide the right key." } }

在 Emotion API 解释页面上,它说 Response 202 意味着

The service has accepted the request and will start the process later. In the response, there is a "Operation-Location" header. Client side should further query the operation status from the URL specified in this header.

然后是Response 401,这正是我的Operation-Location 包含的内容。我不明白为什么我会收到看起来像响应 401 的响应 202。

我尝试使用我在 Internet 上找到的至少三个代码版本用 Python 调用 API 都是一样的,我在这里找到了代码: Microsoft Emotion API for Python - upload video from memory python-从内存上传视频

   import httplib
   import urllib
   import base64
   import json
   import pandas as pd
   import numpy as np
   import requests

   _url = 'https://api.projectoxford.ai/emotion/v1.0/recognizeInVideo'
   _key = '**********************'
   _maxNumRetries = 10

   paramsPost = urllib.urlencode({'outputStyle' : 'perFrame', \
                           'file':'C:/path/to/file/file.mp4'})
   headersPost = dict()
   headersPost['Ocp-Apim-Subscription-Key'] = _key
   headersPost['content-type'] = 'application/octet-stream'
   jsonGet = {}
   headersGet = dict()
   headersGet['Ocp-Apim-Subscription-Key'] = _key
   paramsGet = urllib.urlencode({})

   responsePost = requests.request('post', _url + "?" + paramsPost, \
   data=open('C:/path/to/file/file.mp4','rb').read(), \
   headers = headersPost)

   print responsePost.status_code

   videoIDLocation = responsePost.headers['Operation-Location']
   print videoIDLocation

请注意,将 _url = 'https://api.projectoxford.ai/emotion/v1.0/recognizeInVideo' 更改为 _url = 'https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognizeInVideo' 没有帮助。

然而,之后我等待 运行 每半小时:

   getResponse = requests.request('get', videoIDLocation, json = jsonGet,\
   data = None, headers = headersGet, params = paramsGet)

   print json.loads(getResponse.text)['status']

结果已经 'Running' 几个小时了,而我的视频只有半小时左右。

这是我的测试控制台的样子 Testing Console for Emotion API, Emotion Recognition in Video 在这里,我使用了另一个大约 5 分钟长的视频,该视频可以在 Internet 上找到。我在不同的用法示例中找到了该视频

 https://benheubl.github.io/data%20analysis/fr/

它使用了非常相似的代码,这再次让我得到一个响应状态 202 Accepted 并且在打开 Operation-Location 时订阅密钥是错误的

这里是代码:

import httplib
import urllib
import base64
import json
import pandas as pd
import numpy as np
import requests



# you have to sign up for an API key, which has some allowances. Check the 
API documentation for further details:
_url = 'https://api.projectoxford.ai/emotion/v1.0/recognizeinvideo'
_key = '*********************' #Here you have to paste your 
primary key
_maxNumRetries = 10

# URL direction: I hosted this on my domain
urlVideo = 'http://datacandy.co.uk/blog2.mp4'

# Computer Vision parameters
paramsPost = { 'outputStyle' : 'perFrame'}

headersPost = dict()
headersPost['Ocp-Apim-Subscription-Key'] = _key
headersPost['Content-Type'] = 'application/json'

jsonPost = { 'url': urlVideo }

responsePost = requests.request( 'post', _url, json = jsonPost, data = None, 
headers = headersPost, params = paramsPost )
if responsePost.status_code == 202: # everything went well!
  videoIDLocation = responsePost.headers['Operation-Location']
  print videoIDLocation

互联网上还有更多示例,它们似乎都有效,但复制其中任何一个对我来说都不起作用。有谁知道哪里出了问题?

您是否使用 curl 确认您的 API 通话正常?始终首先使用 curl 进行原型调用。如果它在 curl 中有效但在 Python 中无效,请使用 Fiddler 观察 API 请求和响应。

情感视频功能 API 将于 10 月 30 日停用,所以也许您还是应该将程序更改为屏幕截图。

但是对于您的问题:API returns 您 URL 可以访问您的结果。您无法在浏览器中打开此 URL,这会给您 "invalid key" 的通知,而您需要再次调用 python 此 URL 包括您的密钥。 我会 post 你我的代码如何获得分数,我正在使用 Python 3,所以可能需要一些调整。只有 "tricky" 点是获取操作 ID,它只是 URL 中的 ID(在我的例子中是 =location),它会导致您的请求。其余参数如订阅密钥等与以前一样。

#extract operation ID from location-string
OID = location[67:]
bod = ""

try:
    conn = 
    http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("GET", "/emotion/v1.0/operations/"+OID+"?%s" %params, bod, headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

我在下面link也找到了答案,所有步骤都有解释: https://gigaom.com/2017/04/10/discover-your-customers-deepest-feelings-using-microsoft-facial-recognition/