【发布时间】:2018-07-16 07:05:16
【问题描述】:
我正在创建一个 API 来接收和处理图像。我必须以字节数组格式接收图像。以下是我要发布的代码:
方法 1 将图像发布到 api
with open("test.jpg", "rb") as imageFile:
f = imageFile.read()
b = bytearray(f)
url = 'http://127.0.0.1:5000/lastoneweek'
headers = {'Content-Type': 'application/octet-stream'}
res = requests.get(url, data=b, headers=headers)
##print received json response
print(res.text)
我的 API:通过 api 接收图像
@app.route('/lastoneweek', methods=['GET'])
def get():
img=request.files['data']
image = Image.open(io.BytesIO(img))
image=cv2.imread(image)
##do all image processing and return json response
在我的 api 中,我尝试过 request.get['data'] request.params['data']....我收到 object has no attribute 错误。
我尝试将字节数组连同图像的宽度和高度一起传递给 json,例如:
方法二:上传图片到api
data = '{"IMAGE":b,"WIDTH":16.5,"HEIGHT":20.5}'
url = 'http://127.0.0.1:5000/lastoneweek'
headers = {'Content-Type': 'application/json'}
res = requests.get(url, data=data, headers=headers)
并将我在 API 上的 get 函数更改为
通过 api 接收图片
@app.route('/lastoneweek', methods=['GET'])
def get():
data=request.get_json()
w = data['WIDTH']
h = data['HEIGHT']
但收到以下错误,例如:
TypeError: 'LocalProxy' does not have the buffer interface
【问题讨论】: