python - OpenCV Python,从命名管道读取视频
问题描述
我正在尝试实现视频中显示的结果(使用 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
将视频流式传输到我的 PC,然后在 PC 上创建名称管道“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 数组是空的,所以有什么想法可以解决这个问题吗?
解决方案
图像数组为空。由于 raw_image 也是空的,因此似乎没有读取视频。确保视频已打开并正在阅读。
推荐阅读
- ibm-mobilefirst - ios设备注册上的INVALID_CLIENT_ID错误
- swift - 为什么struct在转换为字典时需要符合Hashable和Generic数组
- r - 如何在ggplot中没有异常值的情况下调整箱线图中的y轴大小?
- testng - Testng - 每个测试方法都可以有一个数据提供者输入吗?
- c# - 从派生类向基属性添加属性
- spring-cloud-dataflow - 用于 Kubernetes 安装的 Spring Cloud Data Flow Server
- recursion - F# sum 机器控制台
- java - 根据用户输入字符串确保我的 RNG 不会崩溃
- node.js - 在猫鼬模式中出乎意料
- vba - 在 VBA 中使用下一个和上一个按钮