我只对图片使用情感API订阅密钥,但从不对视频使用。无论我是使用API测试控制台还是尝试通过Pathon 2.7调用情感API,都没有什么区别。在这两种情况下,我都得到了一个响应状态202接受,然而,当打开操作位置时,它说
{ "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." } }
在情感API解释页面上,它说回应202意味着
服务已接受请求,稍后将启动该过程。
在响应中,有一个“操作位置”标题。客户端应进一步从该标头中指定的URL查询操作状态。
然后是
Response 401
,这正是我的操作位置包含的内容。我不明白为什么我会得到一个回复202,看起来像是回复401。
我尝试用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']
结果已经“运行”了几个小时,而我的视频只有大约半个小时长。
下面是我的测试控制台的外观
Testing Console for Emotion API, Emotion Recognition in Video
在这里,我使用了另一个视频,约5分钟长,可在互联网上。我在另一个使用示例中找到了该视频
https://benheubl.github.io/data%20analysis/fr/
这使用了一个非常类似的代码,它再次为我提供了一个响应状态202 Accepted,当打开操作位置时,订阅密钥是错误的
代码如下:
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
互联网上还有更多的例子,它们似乎都有效,但复制其中任何一个对我来说都不起作用。有人知道会出什么问题吗?