本文介绍了使用CV2读取图像文件(文件存储对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过curl向烧瓶服务器发送图像,我正在使用此curl命令

I am sending an image by curl to flask server, i am using this curl command

curl -F "file=@image.jpg" http://localhost:8000/home

,而我正在尝试阅读服务器端使用CV2文件。

and I am trying to read the file using CV2 on the server side.

在服务器端,我通过此代码处理图像

On the server side I handle the image by this code

@app.route('/home', methods=['POST'])
def home():
    data =request.files['file']
    img = cv2.imread(data)
    fact_resp= model.predict(img)
    return jsonify(fact_resp)

我收到此错误-

img = cv2.imread(data)
TypeError: expected string or Unicode object, FileStorage found

如何在服务器端使用CV2读取文件?

How do I read the file using CV2 on the server side?

谢谢!

推荐答案

在烧瓶服务器上使用opencv时,我遇到了类似的问题,为此,我首先将图像保存到磁盘,然后再次使用 cv2.imread()

I had similar issues while using opencv with flask server, for that first i saved the image to disk and read that image using saved filepath again using cv2.imread()

使用保存的文件路径读取该图像,这里是示例代码:

Here is a sample code:

data =request.files['file']
filename = secure_filename(file.filename) # save file 
filepath = os.path.join(app.config['imgdir'], filename);
file.save(filepath)
cv2.imread(filepath)

但是现在,我使用从获得了更有效的方法> cv2.imdecode()可以从numpy数组中读取图像,如下所示:

But now i have got even more efficient approach from here by using cv2.imdecode() to read image from numpy array as below:

#read image file string data
filestr = request.files['file'].read()
#convert string data to numpy array
npimg = numpy.fromstring(filestr, numpy.uint8)
# convert numpy array to image
img = cv2.imdecode(npimg, cv2.CV_LOAD_IMAGE_UNCHANGED)

这篇关于使用CV2读取图像文件(文件存储对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 05:58