OpenCV Python, reading video from named pipe

2024/4/15 1:18:29

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

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

I use command

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

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

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

import cv2
import sysvideo_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-frameret, frame = video_capture.read()if ret == False:passcv2.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] Header missing this error is produced by the command video_capture = cv2.VideoCapture(r'fifo')

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.

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.

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

Answer

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 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 numpyFFMPEG_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-frameraw_image = pipe.stdout.read(640*480*3)# transform the byte read into a numpy arrayimage =  numpy.fromstring(raw_image, dtype='uint8')image = image.reshape((480,640,3))          # Notice how height is specified first and then widthif image is not None:cv2.imshow('Video', image)if cv2.waitKey(1) & 0xFF == ord('q'):breakpipe.stdout.flush()cv2.destroyAllWindows()

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.

https://en.xdnf.cn/q/72587.html

Related Q&A

Load model with ML.NET saved with keras

I have a Neural Network implemented in Python with Keras. Once I have trained it I have exported the model and I have got two files: model.js and model.h5. Now I want to classify in real time inside a …

unable to add spark to PYTHONPATH

I am struggling to add spark to my python path:(myenv)me@me /home/me$ set SPARK_HOME="/home/me/spark-1.2.1-bin-hadoop2.4" (myenv)me@me /home/me$ set PYTHONPATH=$PYTHONPATH:$SPARK_HOME:$SPARK_…

Is there a way to shallow copy an existing file-object?

The use case for this would be creating multiple generators based on some file-object without any of them trampling each others read state. Originally I (thought I) had a working implementation using s…

Python - Import package modules before as well as after setup.py install

Assume a Python package (e.g., MyPackage) that consists of several modules (e.g., MyModule1.py and MyModule2.py) and a set of unittests (e.g., in MyPackage_test.py).. ├── MyPackage │ ├── __ini…

Where can I find the _sre.py python built-in module? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

When numba is effective?

I know numba creates some overheads and in some situations (non-intensive computation) it become slower that pure python. But what I dont know is where to draw the line. Is it possible to use order of …

How to launch a Windows shortcut using Python

I want to launch a shortcut named blender.ink located at "D://games//blender.ink". I have tryed using:-os.startfile ("D://games//blender.ink")But it failed, it only launches exe fil…

Providing context in TriggerDagRunOperator

I have a dag that has been triggered by another dag. I have passed through to this dag some configuration variables via the DagRunOrder().payload dictionary in the same way the official example has don…

Install gstreamer support for opencv python package

I have built my own opencv python package from source. import cv2 print(cv2.__version__)prints: 3.4.5Now the issue I am facing is regarding the use of gstreamer from the VideoCapture class of opencv. I…

How to use query function with bool in python pandas?

Im trying to do something like df.query("column == a").count()but withdf.query("column == False").count()What is the right way of using query with a bool column?