Ric*_*igh 5 python opencv pipe stream raspberry-pi2
我正在尝试实现如视频(使用netcat的方法3)所示的结果 https://www.youtube.com/watch?v=sYGdge3T30o
关键是将视频从树莓派流式传输到ubuntu PC并使用openCV和python处理它。
我用命令
raspivid -vf -n -w 640 -h 480 -o - -t 0 -b 2000000 | nc 192.168.0.20 5777
将视频流式传输到我的PC,然后在PC上创建名称管道“ fifo”并重定向输出
nc -l -p 5777 -v > fifo
Run Code Online (Sandbox Code Playgroud)
然后我试图读取管道并在python脚本中显示结果
import cv2
import sys
video_capture = cv2.VideoCapture(r'fifo')
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640);
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480);
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
if ret == False:
pass
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
但是我最终会遇到一个错误
[mp3 @ 0x18b2940]命令产生的标题缺少此错误video_capture = cv2.VideoCapture(r'fifo')
当我将PC上的netcat的输出重定向到一个文件,然后在python中读取它时,视频有效,但是它的速度大约提高了10倍。
我知道问题出在python脚本上,因为nc传输有效(到文件),但是我找不到任何线索。
如何获得所提供视频中显示的结果(方法3)?
我也想在该视频中获得相同的结果。最初,我尝试了与您类似的方法,但似乎cv2.VideoCapture()无法从命名管道读取,因此需要进行一些预处理。
ffmpeg是要走的路!您可以按照此链接中提供的说明安装和编译ffmpeg:https : //trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
安装完成后,您可以像这样更改代码:
import cv2
import subprocess as sp
import numpy
FFMPEG_BIN = "ffmpeg"
command = [ FFMPEG_BIN,
'-i', 'fifo', # fifo is the named pipe
'-pix_fmt', 'bgr24', # opencv requires bgr24 pixel format.
'-vcodec', 'rawvideo',
'-an','-sn', # we want to disable audio processing (there is no audio)
'-f', 'image2pipe', '-']
pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=10**8)
while True:
# Capture frame-by-frame
raw_image = pipe.stdout.read(640*480*3)
# transform the byte read into a numpy array
image = numpy.fromstring(raw_image, dtype='uint8')
image = image.reshape((480,640,3)) # Notice how height is specified first and then width
if image is not None:
cv2.imshow('Video', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
pipe.stdout.flush()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
无需在树莓派pi脚本上进行任何其他更改。
这对我来说就像是一种魅力。视频滞后可以忽略不计。希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
5252 次 |
| 最近记录: |