【问题标题】:Send and Receive Images in Bytes以字节为单位发送和接收图像
【发布时间】:2019-10-05 00:03:10
【问题描述】:

我想使用 Python 和 Flask 发送和接收图像。我当前的解决方案不起作用。

from flask import Flask, render_template, request
import pandas as pd
import cv2
import numpy as np
import base64

app = Flask(__name__)

@app.route('/add_face', methods=['GET', 'POST'])
def add_face():
    if request.method == 'POST':
        #  read encoded image
        imageString = base64.b64decode(request.form['img'])
        #  convert binary data to numpy array
        nparr = np.fromstring(imageString, np.uint8)
        #  let opencv decode image to correct format
        img = cv2.imdecode(nparr, cv2.IMREAD_ANYCOLOR);
        cv2.imshow("frame", img)
        cv2.waitKey(0)

    return "list of names & faces"

if __name__ == '__main__':
    app.run(debug=True, port=5000)

客户:

import requests
import base64

URL = "http://localhost:5000/add_face"

#first, encode our image with base64
with open("block.png", "rb") as imageFile:
    img = base64.b64encode(imageFile.read())

response = requests.post(URL, data={"name":"obama", "img":str(img)})
print(response.content)

错误:

    return binascii.a2b_base64(s)\nbinascii.
Error: Incorrect padding\n\n-->\n' 

【问题讨论】:

  • 你为什么要删除所有的import 语句(可能在你的代码中),这样没有人可以运行它?
  • 请检查您的缩进,尤其是在with 声明下。
  • 很抱歉我添加了它

标签: python opencv flask


【解决方案1】:

我认为您没有在服务器上正确解析请求。此外,变量“imageString”实际上是二进制的,因为它是从 post 请求中解码“img”字符串的结果

尝试改变

imageString = base64.b64decode(request.form['img'])

req = request.get_json(silent=True, force=True)
imageString = base64.b64decode(req.get('img'))

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2019-08-03
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多