使用牛津计划的Emotion API [英] Using Project Oxford's Emotion API

查看:234
本文介绍了使用牛津计划的Emotion API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了牛津计划,并对它和使用它的API(特别是情感模型)非常感兴趣.微软提供了示例代码

I came across Project Oxford and became really interested in it and using its API, specifically the emotion one. Microsoft provides sample code

########### 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))

这不包含请求正文.我以为我需要补充的是

This doesn't contain the request body. I thought all I need to add was

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)

但是那是行不通的.我在运行它时得到了它

However that isn't working. I am getting this when I run it

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'

非常感谢您的帮助!

推荐答案

以下内容也基于MSDN提供的示例代码对我有效(Python 2.7).您不需要指定faceRectangles(除非因为已经检测到它们,否则不需要这样做,以节省计算时间).

The following works for me (Python 2.7), also based on the sample code provided by MSDN. You don't need to specify faceRectangles (unless you want to because they've already been detected, to save compute time).

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))

这篇关于使用牛津计划的Emotion API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆