我遇到了 Project Oxford,并对它和使用它的 API(特别是情感 API)非常感兴趣。微软提供示例代码
########### Python 2.7 #############
import httplib, urllib, base64
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'add key',
}
params = urllib.urlencode({
# Request parameters
'faceRectangles': '{string}',
})
try:
conn = httplib.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/emotion/v1.0/recognize&%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
这不包含请求正文。我以为我需要添加的是
body = {
'url': 'url here',
}
并改变
conn.request("POST", "/emotion/v1.0/recognize&%s" % params, "{body}",headers)
到
conn.request("POST", "/emotion/v1.0/recognize&%s" % params, body, headers)
但是这不起作用。当我运行它时我得到了这个
Traceback (most recent call last):
File "C:/Users/User/Desktop/python/emotion.py", line 29, in <module>
print("[Errno {0}] {1}".format(e.errno, e.strerror))
AttributeError: 'exceptions.TypeError' object has no attribute 'errno'
非常感谢任何帮助!
最佳答案
您需要将 str(body)
传递给请求。
此外,如果您没有任何面部矩形,请确保不包含 params
。
关于python - 使用牛津项目的情感 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33769708/