【发布时间】:2017-10-15 22:21:22
【问题描述】:
我只设法将 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 意味着
服务已接受请求,稍后将启动该过程。 在响应中,有一个“Operation-Location”标头。客户端应该进一步从这个header中指定的URL查询操作状态。
然后是Response 401,这正是我的 Operation-Location 包含的内容。我不明白为什么我收到的响应 202 看起来像响应 401。
我尝试使用我在 Internet 上找到的至少三个代码版本使用 Python 调用 API 都一样,我在这里找到了代码: Microsoft Emotion API for Python - upload video from memory python-upload-video-from-memory
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
互联网上还有更多示例,它们似乎都有效,但复制其中任何一个都对我不起作用。有谁知道可能出了什么问题?
【问题讨论】:
标签: python api key subscription microsoft-cognitive