Ask Your Question
0

Python cv2.VideoCapture from subprocess.PIPE?

asked 2014-02-20 20:37:44 -0600

SergeiF gravatar image

Hi, I am trying to capture video from a subprocess (which outputs stream to stdout).

Eg:

.

..
videopipe = subprocess.Popen(externalcmd, stdout=subprocess.PIPE)
capture = cv2.VideoCapture(videopipe.stdout)
...

Obviously snippet from above does not work.... Where the external command will be generating raw video stream...

How does captures video from a pipe with python cv2?

Thanks a lot!

Sergei.

edit retag flag offensive close merge delete

Comments

VideoCapture wants either a string(a filename or url) or a number (cam-id) as input.

what did you try to achieve with your pipe ?

berak gravatar imageberak ( 2014-02-21 01:38:43 -0600 )edit

I want to capture from an external process, so the external process handles the source (codec/format) and converts it into rawvideo format. The idea is that for example avconv can handle reliably all video cams, as I have problems with cv2.VideoCapture an rtsp stream reliably (there are a lot of h264 errors). I am attempting in using named pipe as input for VideoCapture (at this stage without success). Ideally it would be nice to do this: someprocess | videocapture.py, where someprocess provides a rawvideo stream and videocapture.py does OpenCV stuff regardless what is the source of video (motion detection, face detection etc.).

SergeiF gravatar imageSergeiF ( 2014-02-23 14:55:50 -0600 )edit

Oh, it is for linux.

SergeiF gravatar imageSergeiF ( 2014-02-23 14:57:40 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2014-07-29 03:08:13 -0600

nicosmik gravatar image

I found a way to pipe a stream to VideoCapture for windows, because openCV is based on FFmpeg, that supports named pipe.

Here is a quick example:


#!/usr/bin/env python
import cv2
import win32pipe, win32file
from threading import Thread

def runPipe():    
        p = win32pipe.CreateNamedPipe(r'\\.\pipe\myNamedPipe',
                                        win32pipe.PIPE_ACCESS_DUPLEX,
                                        win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
                                        1, 1024, 1024, 0, None)        
        win32pipe.ConnectNamedPipe(p, None)    
        with open("D:\\Streams\\mystream.ts", 'rb') as input:
            while True:
                data = input.read(1024)
                if not data:
                    break
                win32file.WriteFile(p, data)                

def extract():
    cap = cv2.VideoCapture(r'\\.\pipe\myNamedPipe')    
    fnum = 0
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()                                                                             
        print fnum, "pts:", cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC)
        fnum = fnum + 1                
    # When everything done, release the capture
    cap.release()   

if __name__ == "__main__":    
    thr = Thread(target=extract)
    thr.start()
    runPipe()
    print "bye"
edit flag offensive delete link more

Comments

oh, nice. will be helpful to some people !

berak gravatar imageberak ( 2014-07-29 04:33:02 -0600 )edit
0

answered 2014-07-24 04:48:28 -0600

nicosmik gravatar image

updated 2014-07-24 04:49:24 -0600

Hi,

I would like to do exactly the same under windows.

Do you know if it is possible to pipe a video stream to cv2.VideoCapture?

Thanks, Nicolas.

edit flag offensive delete link more

Comments

no, not supported.

also i don't understand what you'd need the VideoCapture for.

if it's decoded image pixels, why not read it into a numpy array from pyhon straight away ?

berak gravatar imageberak ( 2014-07-24 06:20:47 -0600 )edit

The goal is to pipe a compressed video stream (like MP4 or Mpeg2TS) to cv2.VideoCapture. You propose another way to do it?

nicosmik gravatar imagenicosmik ( 2014-07-24 07:16:11 -0600 )edit

oh i see. got you wrong there, sorry.

can't you redirect the pipe to a socket ? some dirty netcat trick ? then connect the VideoCapture to your localhost.

berak gravatar imageberak ( 2014-07-24 07:25:41 -0600 )edit

Question Tools

Stats

Asked: 2014-02-20 20:37:44 -0600

Seen: 5,714 times

Last updated: Jul 29 '14