将RGB图像数据从Python Numpy发送到浏览器HTML页面
我需要通过HTTP将实时图像RGB数据(以Numpy格式)发送到浏览器(基于Web的GUI)的HTML页面。
以下代码可与众所周知的 multipart/x-mixed-replace
trick:运行此并访问 http://127.0.0.1:5000/video_feed :您将在浏览器中看到视频。
from flask import Flask, render_template, Response
import numpy as np, cv2
app = Flask('')
def gen_frames():
while True:
img = np.random.randint(0, 255, size=(1000, 1000, 3))
ret, buf = cv2.imencode('.jpg', img)
frame = buf.tobytes()
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
app.run()
但是,根据我的基准,真正的性能瓶颈是 cv2.imencode('。jpg',img)
。
在我的真实应用中,如果我只是生成图像,则Python的CPU为1%。
当i Imencode(...)
时,CPU跳至25%,Chrome为15%。
我也尝试使用PNG格式,但相似。
问题:如何有效地从numpy数组(示例:1000 x 1000像素x 3颜色)将RGB图像数据发送到浏览器HTML页面?
(没有压缩/压缩,它可能会更好,它可能会更好, 呢?
但是如何 =“在此处输入图像描述”>
这是基准
FPS CPU PYTHON CPU CHROME
PNG 10.8 20 % 10 %
JPG 14 23 % 12 %
JPG 10.7 16 % 10 % (with time.sleep to match PNG 10.8 fps)
BMP 19 17 % 23 %
BMP 10.8 8 % 12 % (with time.sleep to match PNG 10.8 fps)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用枕头模块,看看是否可以改善性能。我的基准表明,基于枕头的
gen_frames()
生成器功能的每次迭代都需要CV2版本的CPU的一半。Try using the PILLOW module instead and see if that improves performance. My benchmark shows that each iteration of the
gen_frames()
generator function based on PILLOW requires less than half the CPU of the CV2 version.根据文档,您可以检查默认优化是否已打开:
因此,请尝试以下操作:
According to the docs you could check if the default optimization is turned on:
So try this:
因为它的架子或MP4压缩对于电影来说也是好的质量,但是压缩本身需要太多的CPU时间来在实时数据上执行它。
如果创建了某些任意协议/格式,则不仅要编程服务器,还需要对客户端进行编程来消耗此协议/格式。因此,仍然应该优选一些标准解决方案。
我相信,您可以在视频会议系统中找到压缩和CPU负载之间的折衷,其中实时相机数据需要通过网络压缩和流式传输。考虑到这一点,我相信这里有可以帮助追求该主题的信息来源:
As it stands AVI or MP4 compression would be good quality even for movies, but the compression itself takes too much CPU time to perform it on live data.
If some arbitrary protocol/format were created, one would not just have to program the server but also the client to consume this protocol/format. Therefore still some standard solution should be preferred.
I believe you can find a compromise between compression and CPU load in video conferencing systems, where the live camera data needs to be compressed and streamed via the network. With that in mind I believe here are sources of information that can help pursuing the topic:
也许您可以尝试将其编码为base64以压缩视频/图像,然后将其发送给base64 Mime型EGDATA:Image/jpeg; base64。
Maybe you can try encoding it as base64 for compression of the video/image and then send it to the browser with the base64 mime type e.g.data:image/jpeg;base64.