Python - response.read()在Windows中运行顺畅,但在Raspbian中根本不运行

时间:2018-02-07 20:41:13

标签: python windows raspbian microsoft-cognitive

我正在尝试运行在我的覆盆子pi3中使用Microsoft Emotion API程序的程序。 我在Windows上对Python3.6.4版本进行了尝试,它运行得非常好。 但是,当我在Raspberrypi3上尝试它时,使用默认的python3.5(没有http.client)和python3.6.2 IDLE(How to start IDLE that comes with Python 3.6),程序要么(1)根本不运行, (2)或给出超时错误,(3)或

b'{"error":{"code":"InvalidImageSize","message":"Image size is too small or too big."}}' error.

我也尝试在env环境下在python3.6上运行它,但无济于事。它甚至没有打印出应该返回浮动值的幸福。请告诉我为什么以及如何在Raspberry Pi上使用它,非常感谢!它与python版本有关吗? API?

代码如下:

import http.client, urllib.request, urllib.parse, urllib.error, base64, sys
import json


headers = {
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': 'MY-KEY',
}

params = urllib.parse.urlencode({
        })

body = open('1.jpg','rb').read()

print ('begin')
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
print ('connection')
conn.request("POST", "/emotion/v1.0/recognize?%s" % params, body, headers)
print ('request')
response = conn.getresponse()
print ('donerequest')
data = response.read()
print(data)

faces = json.loads(data)
happiness = faces[0]['scores']['happiness'] if len(faces)>0 else 0
print (happiness)
with open('result.txt', 'a') as out:
    out.write("1"+ '\n')
conn.close()

问题: 停在print(data)b'{"error":{"code": "timeout", "message": "the operation was timeout." }}' Traceback (most recent call last): File.... in line 31 in <module> happiness = faces[0]['scores']['happiness'] if len(faces)>0 else 0 keyerror: 0.

似乎问题发生在 data = response.read()as print(data)返回“operation is timeout”错误“。我可以知道如何在raspbian中编辑它,因为它在windows中运行良好吗? 我确实尝试了以下无济于事:

data = response.read().decode("utf8")

0 个答案:

没有答案