发送和接收OpenCV图像瓶 [英] Send and receive opencv images flask

查看:35
本文介绍了发送和接收OpenCV图像瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从客户端向服务器发送和接收openCV图像,并在处理后发送回客户端.我无法理解服务器正在发送回什么类型的数据...

I am trying to send and receive openCV images form client to server and back to client after processing. I am not able to understand the what type of data is being sent back by the server ...

服务器:

from flask import Flask, request, Response, send_file
import jsonpickle
import numpy as np
import cv2

import ImageProcessingFlask

# Initialize the Flask application
app = Flask(__name__)


# route http posts to this method
@app.route('/api/test', methods=['POST'])
def test():
    r = request
    # convert string of image data to uint8
    nparr = np.fromstring(r.data, np.uint8)
    # decode image
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

    # do some fancy processing here....

    img = ImageProcessingFlask.render(img)


    #_, img_encoded = cv2.imencode('.jpg', img)
    #print ( img_encoded)

    cv2.imwrite( 'new.jpeg', img)


    #response_pickled = jsonpickle.encode(response)
    #return Response(response=response_pickled, status=200, mimetype="application/json")
    return send_file( 'new.jpeg', mimetype="image/jpeg", attachment_filename="new.jpeg", as_attachment=True)

# start flask app
app.run(host="0.0.0.0", port=5000)

客户:

import requests
import json
import cv2

addr = 'http://localhost:5000'
test_url = addr + '/api/test'

# prepare headers for http request
content_type = 'image/jpeg'
headers = {'content-type': content_type}

img = cv2.imread('lena.jpeg')
# encode image as jpeg
_, img_encoded = cv2.imencode('.jpg', img)

# send http request with image and receive response
response = requests.post(test_url, data=img_encoded.tostring(), headers=headers)



print response


cv2.imshow( 'API', response.content )

打印语句显示出来

<Response [200]>

错误是...

    cv2.imshow( 'API', response.content )
TypeError: mat is not a numpy array, neither a scalar

我是不熟悉烧瓶的人,请帮助我解决此错误.

I am new to flask, please help me solve this error.


谢谢.

推荐答案

想法:

  • 客户
  • 使用cv2.imread('lena.jpeg')将图片加载到numpy数组中.将数组腌制为数据.使用 POST 请求, multipart/form-data

    Use cv2.imread('lena.jpeg') to load image to numpy array. Pickle the array to data. Send the data with POST request, multipart/form-data

    • 服务器

    接收数据Pickle加载数据而且我们的numpy数组与cv2.imread('lena.jpeg')相同

    Receive the data Pickle loads the data And we have numpy array as same as cv2.imread('lena.jpeg')

    客户端上的序列化numpy数组

    img = cv2.imread('lena.jpeg')
    serialized = pickle.dumps(frame, protocol=0)
    

    将带有发帖请求的邮件发送到服务器

    name_img = "frame.jpeg"
    files = {'image': (name_img, serialized, 'multipart/form-data', {'Expires': '0'})}
    with requests.Session() as s:
        r = s.post(url, files=files)
        print("status ", r.status_code)
        return r
    

    然后在服务器上,将其加载回numpy数组

    @app.route('/receive-frame', methods=['POST'])
    def handler_receive_frame():
        timestamp = str(int(time.time()))
        file = request.files['image']
        file_path = os.path.join(save_dir, "image_" + timestamp + ".jpeg")
        # file.save(file_path)
        # cv2.imwrite(file_path, file.read())
        image_numpy = pickle.loads(file.read())
        cv2.imwrite(filename=file_path, img=image_numpy)
    
        print("receive file name ", file.filename)
        print("output_file_name ", file_path)
        result = {"error": 0}
        return result
    

    我们在服务器上的 image_numpy 与客户端上的 img 相同(它是一个numpy数组).

    And we have image_numpy on server is same as img on client (it is an numpy array).

    无论您想使用 img = cv2.imread('lena.jpeg')做什么,都可以使用 image_numpy

    Whatever you want to do with img = cv2.imread('lena.jpeg'), you can do with image_numpy

    这篇关于发送和接收OpenCV图像瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆