【发布时间】:2020-02-28 16:44:36
【问题描述】:
我正在尝试实现视频中显示的结果(使用 netcat 的方法 3)https://www.youtube.com/watch?v=sYGdge3T30o 它是将视频从树莓派流式传输到PC,并使用openCV和python进行处理。
我使用命令
raspivid -vf -n -w 640 -h 480 -o - -t 0 -b 2000000 | nc 192.168.1.137 8000
将视频流式传输到我的电脑,然后在电脑上创建名称管道“fifo”并重定向输出
nc -l -p 8000 -v > fifo
然后我尝试读取管道并在 python 脚本中显示结果
import cv2
import subprocess as sp
import numpy
FFMPEG_BIN = "ffmpeg.exe"
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.frombuffer(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()
但是我收到了这个错误:
Traceback (most recent call last):
File "C:\Users\Nick\Desktop\python video stream\stream.py", line 19, in <module>
image = image.reshape((480,640,3)) # Notice how height is specified first and then width
ValueError: cannot reshape array of size 0 into shape (480,640,3)
numpy 数组似乎是空的,有什么办法可以解决这个问题吗?
【问题讨论】:
-
也许可以尝试将完整路径添加到
ffmpeg.exe,包括目录。 -
从您的帖子看来,
pipe.stdout.flush()在while循环内。是复制粘贴的问题,还是循环里面的问题? -
@Rotem 无论是在循环内还是循环外都会出现上面提到的错误
-
@Mark Setchell 如果我添加完整路径 FFMPEG_BIN = "G:\tool\ffmpeg\bin\ffmpeg.exe",将会出现“FileNotFoundError”
-
您发布了“将视频从树莓派流式传输到 ubuntu PC”,但根据您的错误消息,您似乎使用的是 Windows。
标签: python opencv ffmpeg raspberry-pi