【发布时间】:2017-10-04 15:46:38
【问题描述】:
我在调用情感 api 后得到以下响应。我怎样才能拥有一个只获得幸福分数的变量?我想要像 data=happiness 这样的东西,然后我就可以打印数据了。
{
"FaceRectangle": {
"Top": 141,
"Left": 331,
"Width": 52,
"Height": 52
},
"Scores": {
"Anger": 0.002451766,
"Contempt": 0.0005512201,
"Disgust": 0.0063303886,
"Fear": 0.000122375583,
"Happiness": 0.9589189,
"Neutral": 0.0222537462,
"Sadness": 0.008983561,
"Surprise": 0.000388026354
}
}
这是python代码
import http.client, urllib.request, urllib.parse, urllib.error, base64, sys
headers = {
# Request headers. Replace the placeholder key below with your subscription key.
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': '**************************',
}
params = urllib.parse.urlencode({
})
body = open('clouds.jpg','rb').read()
try:
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognize?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
除了 e 例外: 打印(e.args)
【问题讨论】:
-
data['Scores']['Happiness'](假设您将结果保存在名为data的变量中) -
我想要像 data=happiness 这样的东西,然后我就可以打印数据了
标签: python-3.x api microsoft-cognitive