首页 文章

Microsoft-Cognitive:Emotion API

提问于
浏览
0

在打电话给情感api后,我得到了以下回复 . 我怎样才能拥有一个仅获得幸福分数的变量?我想要数据=幸福然后我能够只打印数据 .

{
    "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:print(e.args)

1 回答

  • 0

    @falsetru是正确的,但你应该注意Emotion API

    • 返回一个面数组,即使只有一个面,也是

    • 与您添加的代码段不同,字段名称将是驼峰式的

    import requests
    
    body = {'url':'YOUR-IMAGE-URL'}
    headers = {
      'Content-Type': 'application/json',
      'Ocp-Apim-Subscription-Key': 'YOUR-KEY'
    }
    req = requests.post('https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize', headers=headers, data=str(body))
    faces = req.json()
    for face in faces:
       print face['scores']['happiness']
    

相关问题