Python 使用牛津计划';s情感API
我遇到了牛津项目,对它非常感兴趣,并使用了它的API,特别是情感API。Microsoft提供了示例代码Python 使用牛津计划';s情感API,python,python-2.7,microsoft-cognitive,Python,Python 2.7,Microsoft Cognitive,我遇到了牛津项目,对它非常感兴趣,并使用了它的API,特别是情感API。Microsoft提供了示例代码 ########### Python 2.7 ############# import httplib, urllib, base64 headers = { # Request headers 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': 'add key', } params
########### 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)
到
然而,这是行不通的。当我运行它时,我得到了这个
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'
回溯(最近一次呼叫最后一次):
文件“C:/Users/User/Desktop/python/emotion.py”,第29行,在
打印(“[Errno{0}]{1}”。格式(e.Errno,e.strerror))
AttributeError:“exceptions.TypeError”对象没有属性“errno”
非常感谢您的帮助 您需要向请求传递str(body)
另外,如果没有任何面矩形,请确保不包含
参数。以下内容对我适用(Python 2.7),这也是基于MSDN提供的示例代码。您不需要指定面矩形(除非您想指定,因为它们已经被检测到,以节省计算时间)
导入httplib、urllib、base64
#要分析的图像(请求正文)
正文=“{'URL\”:\”https://.jpg\'}'
#情感检测的API请求
标题={
“内容类型”:“应用程序/json”,
}
params=urllib.urlencode({
“订阅密钥”:“输入情感API密钥”
#“面矩形”:“,
})
尝试:
conn=httplib.HTTPSConnection('api.projectoxford.ai')
conn.request(“POST”,“/emotion/v1.0/recognize?%s”%params,body,headers)
response=conn.getresponse()
打印(“发送请求”)
data=response.read()
打印(数据)
康涅狄格州关闭
例外情况除外,如e:
打印(“[Errno{0}]{1}”。格式(e.Errno,e.strerror))
changeprint(“[Errno{0}]{1}”.format(e.Errno,e.strerror))
toprint(e.message)
以获取正确的错误消息。@sobolevn错误消息是“unhabable type”@sobolevn异常在conn.request(“POST”、“/emotion/v1.0/recognizer&%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'
import httplib, urllib, base64
# Image to analyse (body of the request)
body = '{\'URL\': \'https://<path to image>.jpg\'}'
# API request for Emotion Detection
headers = {
'Content-type': 'application/json',
}
params = urllib.urlencode({
'subscription-key': '', # Enter EMOTION API key
#'faceRectangles': '',
})
try:
conn = httplib.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/emotion/v1.0/recognize?%s" % params, body , headers)
response = conn.getresponse()
print("Send request")
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))