将OpenCV输出发送到VLC流 [英] Sending OpenCV output to VLC stream
问题描述
这使我一直忙于整个下午的时间,我无法使其正常工作,但我感觉我真的很近.
This has been keeping me busy for a good part of the afternoon and I haven't been able to get it to work but I feel like I'm really close.
我已经设置了openCV,它可以从网络摄像头获取视频.为了能够访问此视频提要(带有openCV覆盖),我想将openCV python脚本的输出通过管道传输到VLC流.我设法使流启动并运行,并且可以连接到它. VLC调整为正确的宽高比和分辨率,因此它可以获取一些正确的数据,但是我得到的图像只是抖动;
I've got openCV set up which takes the videofeed from a webcam. To be able to access this video feed (with openCV overlay) I want to pipe the output of the openCV python script to a VLC stream. I managed to get the stream up and running and can connect to it. VLC resizes to the correct aspect ratio and resolution so it gets some correct data but the image I get is just Jitter;
python opencv.py | cvlc --demux=rawvideo --rawvid-fps=30 --rawvid-width=320 --rawvid-height=240 --rawvid-chroma=RV24 - --sout "#transcode{vcodec=h264,vb=200,fps=30,width=320,height=240}:std{access=http{mime=video/x-flv},mux=ffmpeg{mux=flv},dst=:8081/stream.flv}" &
脚本的输出是一个固定的视频提要,它按如下方式发送到stdout
The output of the script is a constant video feed sent to stdout as follows
from imutils.video import WebcamVideoStream
vs = WebcamVideoStream(src=0)
while True:
frame = vs.read()
sys.stdout.write(frame.tostring())
上面的示例是我正在使用的脚本的精简版本;还可以看到,我正在使用imutils库. https://github.com/jrosebr1/imutils
Above example is a dumbed down version of the script I'm using; Also as seen I'm making use of the imutils library; https://github.com/jrosebr1/imutils
如果有人可以按正确的方向推动我,我将不胜感激.我的猜测是stdout.write(frame.tostring())不是vlc期望的,但是我自己却无法弄清楚.
If anyone could give me a nudge in the right direction I would appreciate it greatly. My guess is the stdout.write(frame.tostring()) is not what vlc expects but I haven't been able to figure it out myself.
推荐答案
以下内容在Python 3下对我有效
The following works for me under Python 3
import numpy as np
import sys
import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
sys.stdout.buffer.write(frame.tobytes())
else:
break
cap.release()
和命令行(我的网络摄像头具有不同的分辨率,我只显示结果,但是您没有遇到问题)
And the command line (my webcam has a different resolution, and I only display the result, but you did not have problems with that)
python opencv.py | vlc --demux=rawvideo --rawvid-fps=25 --rawvid-width=640 --rawvid-height=480 --rawvid-chroma=RV24 - --sout "#display"
当然,这需要从BGR转换为RGB,因为在OpenCV中默认是前者.
Of course this requires a conversion from BGR to RGB as the former is default in OpenCV.
这篇关于将OpenCV输出发送到VLC流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!