简体   繁体   English

flask在Web上渲染opencv图像而不保存文件

[英]flask render opencv image on web without saving file

Basically I followed this tutorial to stream processed video (not just retrieving frames and broadcasting) and it works for me (I'm new to html and flask). 基本上,我遵循本教程来流化处理后的视频(而不仅仅是检索帧和广播),并且对我有用(我是html和flask的新手)。 But I want to save some computation here: 但我想在这里保存一些计算:

  1. I wonder if it's possible to avoid saving opencv image object to a jpeg file and then reading again? 我想知道是否有可能避免将opencv图像对象保存到jpeg文件中,然后再次读取? Is it a waste of computation? 这是浪费计算吗?

  2. I think it's even better if flask/html template could render the image by using raw 3 data channels RGB of the image. 我认为,flask / html模板可以使用图像的原始3个数据通道RGB渲染图像甚至更好。

Any idea? 任何想法? Thanks! 谢谢!

P/S: I actually tried this following code: P / S:我实际上尝试了以下代码:

_, encoded_img = cv2.imencode('.jpg', img, [ int( cv2.IMWRITE_JPEG_QUALITY ), 95 ] )

But it gives the following error: 但是它给出了以下错误:

Debugging middleware caught exception in streamed response at a point where response headers were already sent. 调试中间件在流响应中已发送响应头的位置捕获了异常。 Traceback (most recent call last): File "/home/trungnb/virtual_envs/tf_cpu/lib/python3.5/site-packages/werkzeug/wsgi.py", line 704, in next return self._next() File "/home/trungnb/virtual_envs/tf_cpu/lib/python3.5/site-packages/werkzeug/wrappers.py", line 81, in _iter_encoded for item in iterable: File "/home/trungnb/workspace/coding/Mask_RCNN/web.py", line 25, in gen if frame == None: ValueError: The truth value of an array with more than one element is ambiguous. 追溯(最近一次通话):文件“ /home/trungnb/virtual_envs/tf_cpu/lib/python3.5/site-packages/werkzeug/wsgi.py”,行704, 在下一个返回self._next()文件“ / “ home / trungnb / virtual_envs / tf_cpu / lib / python3.5 / site-packages / werkzeug / wrappers.py”第81行,在_iter_encoded中针对可迭代项进行编码:文件“ / home / trungnb / workspace / coding / Mask_RCNN / web。 py”,第25行,在gen中,如果frame == None:ValueError:具有多个元素的数组的真值不明确。 Use a.any() or a.all() 使用a.any()或a.all()

You would want to compress it to JPEG anyway as sending the raw RGB data would be slower due to the data size. 无论如何,您都希望将其压缩为JPEG,因为发送原始RGB数据会因数据大小而变慢。

You could try using cv::imencode to compress the image. 您可以尝试使用cv :: imencode压缩图像。 Then you may be able send the image in a similar way to flask return image created from database 然后,您可以通过与从数据库创建的烧瓶返回图像类似的方式发送图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM