Python 如何将 openCV 视频流式传输到 HTML 网页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20205358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I stream an openCV video to an HTML webpage?
提问by PyroAVR
I am making a robot that will have a webcam on it to provide some simple object detection. For right now, I would like to simply stream the video to a webpage hosted on the robot and be able to view it from another device. I have written a simple test script in Python ( I will eventually move to C++, my language of choice) which can get a stream from my webcam, and then do whatever I need with it from there. The problem then, is that I can't write the video to a file while the app is running, it only writes the file after I quit the script. I already have a webserver running, and I can write the basic code in HTML to host a video from a file as well, and all of that works.
我正在制作一个机器人,上面有一个网络摄像头来提供一些简单的物体检测。现在,我想简单地将视频流式传输到机器人上托管的网页,并能够从其他设备查看。我用 Python 编写了一个简单的测试脚本(我最终将转向 C++,我选择的语言),它可以从我的网络摄像头获取流,然后从那里做我需要的任何事情。那么问题是,我无法在应用程序运行时将视频写入文件,它仅在我退出脚本后才写入文件。我已经有一个网络服务器在运行,我可以用 HTML 编写基本代码来托管文件中的视频,所有这些都有效。
To summarize: Is openCV2 in Python and/or C++ capable of livestreaming video using only openCV? If not, what library would you recommend that I try to take a CV capture object or Mat object and write it to a stream that I can then put on a webpage? In HTML, is the tag a good idea to stream video with?
总结一下:Python 和/或 C++ 中的 openCV2 是否能够仅使用 openCV 直播视频?如果没有,您会建议我尝试使用哪个库来获取 CV 捕获对象或 Mat 对象并将其写入流中,然后我可以将其放在网页上?在 HTML 中,使用该标签流式传输视频是个好主意吗?
Thank you very much for the advice, I can use all the pointers* I can get!
非常感谢您的建议,我可以使用所有我能得到的指针*!
If you need something clarified/code posted/explanations further than what I have given, please ask and I will do so!
如果您需要比我给出的更进一步的澄清/代码发布/解释,请询问,我会这样做!
回答by tomriddle_1234
So basically you have to use OpenCV capture the frames and pack them into specific formats that fit the streaming protocol, then from your server use HTML5 to put it on the page. You may need to use VLC or FFMepg to pack your cv::Mat. Hope this will be helpful.
所以基本上你必须使用 OpenCV 捕获帧并将它们打包成适合流媒体协议的特定格式,然后从你的服务器使用 HTML5 将它放在页面上。您可能需要使用 VLC 或 FFMepg 来打包您的 cv::Mat。希望这会有所帮助。
回答by Michel Vidal-Naquet
The issue of streaming frames out of OpenCV and Python has been addressed in the following thread: Pipe raw OpenCV images to FFmpeg
从 OpenCV 和 Python 流式传输帧的问题已在以下线程中得到解决: Pipe raw OpenCV images to FFmpeg
This didn't work for me, but they claim it did for them.
这对我不起作用,但他们声称对他们有用。
The reason for it not working in my case seems to be that for some output frames additional bytes were added or lost, somewhere between the output to stdout in capture.py and the input to FFMPEG. Therefore, the number of bytes doesn't correspond to the number of frames. I am not sure why this is the case. I used Windows 7.
它在我的情况下不起作用的原因似乎是,对于某些输出帧,在 capture.py 中输出到 stdout 和 FFMPEG 输入之间的某个地方添加或丢失了额外的字节。因此,字节数与帧数不对应。我不确定为什么会这样。我用的是 Windows 7。
I will be curious to hear what is your experience if you try this. I also tried a modified version of capture.py using cv2, and failed for the same reasons.
如果您尝试此操作,我会很想知道您的体验如何。我也尝试过使用 cv2 修改过的 capture.py 版本,但由于同样的原因失败了。
回答by Ariel M.
Under lab conditions you send full images
在实验室条件下,您发送完整图像
You seem to be under lab conditions, so there is a simplistic, yet usable solution, just stream PNG's in Base64 using Websockets. On the client side (web browser) you just receive the base64 images and directly load them into the srcof an <img>. It works for lab scenarios very well, albeit slow.
您似乎处于实验室条件下,因此有一个简单但可用的解决方案,只需使用 Websockets 在 Base64 中流式传输 PNG。在客户端(网页浏览器),你刚刚收到的base64图像,并直接将其加载到src的<img>。它非常适用于实验室场景,尽管速度很慢。

