[小白]python读取视频文件

lovebelobe 2017-01-30 03:47:01
我在网上找了一段调用微软的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
...全文
496 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
屎克螂 2017-02-07
  • 打赏
  • 举报
回复
视频数据不base64下 就发出去? 报错信息说: 数据格式有问题 发不出去

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧