OpenCV Python,从命名管道读取视频 [英] OpenCV Python, reading video from named pipe

查看:335
本文介绍了OpenCV Python,从命名管道读取视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得视频所示的结果(使用netcat的方法3) https://www.youtube.com/watch?v=sYGdge3T30o

I am trying to achieve results as shown on the video (Method 3 using netcat) https://www.youtube.com/watch?v=sYGdge3T30o

关键是将视频从树莓派流式传输到ubuntu PC并使用openCV和python处理它.

The point is to stream video from raspberry pi to ubuntu PC and process it using openCV and python.

我使用命令

raspivid -vf -n -w 640 -h 480 -o - -t 0 -b 2000000 | nc 192.168.0.20 5777

将视频流式传输到我的PC,然后在PC上创建名称管道"fifo"并重定向输出

to stream the video to my PC and then on the PC I created name pipe 'fifo' and redirected the output

 nc -l -p 5777 -v > fifo

然后我正在尝试读取管道并将结果显示在python脚本中

then i am trying to read the pipe and display the result in the python script

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()

但是我最后遇到一个错误

However I just end up with an error

[mp3 @ 0x18b2940]标头丢失,此错误是由命令video_capture = cv2.VideoCapture(r'fifo')

当我将PC上的netcat的输出重定向到一个文件,然后在python中读取它时,视频就可以了,但是它的速度大约提高了10倍.

When I redirect the output of netcat on PC to a file and then reads it in python the video works, however it is speed up by 10 times approximately.

我知道问题出在python脚本,因为nc传输可以(到文件)工作,但是我找不到任何线索.

I know the problem is with the python script, because the nc transmission works (to a file) but I am unable to find any clues.

如何获得所提供视频(方法3)所示的结果?

How can I achieve the results as shown on the provided video (method 3) ?

推荐答案

我也想在该视频中获得相同的结果.最初,我尝试了与您类似的方法,但似乎cv2.VideoCapture()无法从命名管道读取,需要进行一些预处理.

I too wanted to achieve the same result in that video. Initially I tried similar approach as yours, but it seems cv2.VideoCapture() fails to read from named pipes, some more pre-processing is required.

ffmpeg 是必经之路!您可以按照此链接中的说明安装和编译ffmpeg: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

ffmpeg is the way to go ! You can install and compile ffmpeg by following the instructions given in this link: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

安装后,您可以像这样更改代码:

Once it is installed, you can change your code like so:

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()

无需在树莓派pi脚本上进行任何其他更改.

No need to change any other thing on the raspberry pi side script.

这对我来说就像是一种魅力.视频滞后可以忽略不计. 希望对您有所帮助.

This worked like a charm for me. The video lag was negligible. Hope it helps.

这篇关于OpenCV Python,从命名管道读取视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆