我在网上找了一段调用微软的EmotionAPI来解析图片中人物感情的代码。可以用!!>_<
微软还有一个可以解析视频的API,我想使用这个API,但是如果是视频的话,是不是就不能用with open(img_file_name, 'r+b') as img:来读取了?
执行:
python postEmotionAPI.py em.jpg
⇒OK,返回值如下
[{'faceRectangle': {'height': 213, 'left': 256, 'top': 203, 'width': 213}, 'scor
es': {'anger': 7.889421e-12, 'contempt': 9.894494e-12, 'disgust': 1.96393058e-11
, 'fear': 2.18754675e-13, 'happiness': 1.0, 'neutral': 8.741801e-10, 'sadness':
5.089581e-11, 'surprise': 5.765032e-10}}]
python postEmotionAPI.py framebyframe_demo_emotion1.mp4
⇒NG,报错如下
Traceback (most recent call last):
File "postEmotionAPI_Video.py", line 21, in <module>
print(get_emotion(args[1]))
File "postEmotionAPI_Video.py", line 16, in get_emotion
return res.json()
File "C:\Users\setup\AppData\Local\Programs\Python\Python36\lib\site-packages\
requests\models.py", line 866, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\setup\AppData\Local\Programs\Python\Python36\lib\json\__init__.
py", line 354, in loads
return _default_decoder.decode(s)
File "C:\Users\setup\AppData\Local\Programs\Python\Python36\lib\json\decoder.p
y", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\setup\AppData\Local\Programs\Python\Python36\lib\json\decoder.p
y", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
#!/usr/local/bin/python3
import sys
import requests
def get_emotion(img_file_name):
with open(img_file_name, 'r+b') as img:
res = requests.post(url='https://api.projectoxford.ai/emotion/v1.0/recognize',
# res = requests.post(url='https://api.projectoxford.ai/emotion/v1.0/recognizeinvideo', 视频的时候换成此行
#params={'outputStyle':'aggregate'}, 使用视频的时候添加此行(参考:※1)
data=img.read(),
headers={'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': '6b106d11b891490298ebecbc1f99eb85'})
return res.json()
if __name__ == "__main__":
args = sys.argv
print(get_emotion(args[1]))
※1:https://dev.projectoxford.ai/docs/services/5639d931ca73072154c1ce89/operations/56f8d40e1984551ec0a0984e
原代码:https://gist.github.com/suu----/3c2275c8e62236f8c09029a6ef23c181