使用Flask的YUV直播视频流[关闭](YUV live video streaming using Flask [closed])

系统教程 行业动态 更新时间:2024-06-14 16:53:13
使用Flask的YUV直播视频流[关闭](YUV live video streaming using Flask [closed])

我目前正在开发一个涉及使用Raspberry Pi的小型机器人汽车的项目。 为了有效地使用picamera(用于图像处理和实时网络流),我想使用YUV420格式(由picamera支持)。 这允许我直接使用Y值进行图像处理,并将任何进一步的转换留给客户端。

是否可以通过Flask Response对象快速传输此数组(使用Python生成器)(如此处显示为JPEG: http : //blog.miguelgrinberg.com/post/video-streaming-with-flask )? 如果是这样,我知道我可以在JavaScript中将YUV数据转换为RGB,然后将其绘制到画布,但是如何一次一帧地访问YUV流?

如果还有其他更有效的解决方案(同时坚持使用Flask),我也想听听它们。 使用Python生成器的Flask Response就像JPEG网络流的魅力一样。

I'm currently working on a project involving a small robotic car using the Raspberry Pi. In order to use the picamera efficiently (both for image processing and a live webstream), I would like to use the YUV420 format (which is supported by picamera). This allows me to directly use the Y values for image processing and leaving any further conversion to the client.

Is it possible to quickly stream this array (with a Python generator) through a Flask Response object (as shown here with JPEG: http://blog.miguelgrinberg.com/post/video-streaming-with-flask)? If so, I know I can convert the YUV data to RGB in JavaScript and then draw it to a canvas, but how do I access the YUV stream one frame at a time?

If there are any other, more efficient solutions (while sticking to Flask), I'd like to hear about them too. The Flask Response with Python generator just worked like a charm for a JPEG webstream.

最满意答案

查看picamera文档和您链接的博客文章 ,您可以创建一个多部分响应,其中包含从相机捕获的YUV420帧流,其代码如下所示:

def gen(): with picamera.PiCamera() as camera: camera.start_preview() # Camera warm-up time time.sleep(2) while True: stream = io.BytesIO() camera.capture(stream, 'yuv') yield stream.getvalue() #perhaps add a time.sleep() here to enforce a constant framerate?

但是,这并没有解决客户端问题。 因为您想在JavaScript中使用图像数据,而不是仅仅在<img>标记中显示它,您需要使用XMLHTTPRequest从JavaScript中获取它,而XMLHTTPRequest不支持多部分响应(具体来说,Firefox 曾经和不再是 )。

更好的方法是使用WebSocket ,从那时起很容易从JavaScript打开WebSocket连接 ,依次读取每个帧(每个帧都在自己的WebSocket消息中发送),并执行必要的图像处理。 但是服务器端怎么样? Flask-Sockets看起来会起作用,然后发送帧流看起来像这样:

@sockets.route('/stream') def stream_socket(ws): with picamera.PiCamera() as camera: camera.start_preview() # Camera warm-up time time.sleep(2) while not ws.closed: stream = io.BytesIO() camera.capture(stream, 'yuv') ws.send(stream.getvalue()) #perhaps add a time.sleep() here to enforce a constant framerate?

Looking at the picamera documentation and the blog post you've linked, you could create a multipart response consisting of a stream of YUV420 frames captured from the camera with code like this:

def gen(): with picamera.PiCamera() as camera: camera.start_preview() # Camera warm-up time time.sleep(2) while True: stream = io.BytesIO() camera.capture(stream, 'yuv') yield stream.getvalue() #perhaps add a time.sleep() here to enforce a constant framerate?

However, that doesn't address the client-side. Because you want to use the image data in JavaScript, rather than just displaying it in an <img> tag, you'd need to fetch it from JavaScript with XMLHTTPRequest, and XMLHTTPRequest doesn't support multipart responses (specifically, Firefox used to and no longer does).

The better approach would be to use WebSocket, since then it would be easy to open a WebSocket connection from JavaScript, read each frame in turn (each frame being sent in its own WebSocket message), and perform the necessary image processing. But what about the server-side? Flask-Sockets looks like it will do the trick, and then sending the stream of frames would look something like this:

@sockets.route('/stream') def stream_socket(ws): with picamera.PiCamera() as camera: camera.start_preview() # Camera warm-up time time.sleep(2) while not ws.closed: stream = io.BytesIO() camera.capture(stream, 'yuv') ws.send(stream.getvalue()) #perhaps add a time.sleep() here to enforce a constant framerate?

更多推荐

本文发布于:2023-04-06 01:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/daaa2f4f0c4ce20e09e0a4c89f699852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视频   YUV   Flask   live   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!