VideoCapture from socket makefile() call?

asked 2014-09-17 10:14:18 -0600

mikey gravatar image

updated 2014-09-17 10:35:16 -0600

Hi,

I am streaming video over a network through sockets. I am able to view the video with mplayer so I know it arrives correctly. What I would like to do is capture that video stream over the socket with cv2.VideoCapture. I have tried as shown below but it gives a TypeError (also shown below). Is there a way to capture this video through VideoCapture or some other opencv functionality?

My Capture code:

import socket
import cv2
import struct
import io
import numpy as np
import traceback

def main():
    try:
        server_socket = socket.socket()
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        server_socket.bind(('0.0.0.0', 5001))
        server_socket.listen(0)
        connection = server_socket.accept()[0].makefile('rb')

        cv2.namedWindow("test-h264", cv2.WINDOW_NORMAL)
        video = cv2.VideoCapture(connection)
        while True:
            ret,frame = video.read()           
            cv2.imshow("test-h264",frame)
    except Exception as e:
        traceback.print_exc()
    finally:
        connection.close()
        server_socket.close()

if __name__ == "__main__":
    main()

The error message:

camera$ python testh264.py 
<socket._fileobject object at 0x2b07cd0>
init done 
opengl support available 
Traceback (most recent call last):
  File "testh264.py", line 23, in main
    video = cv2.VideoCapture(connection)
TypeError: an integer is required
camera$

The video stream is from a picamera, is 1080p and is in h.264 format. The code above is on a laptop with ubuntu 12.04 installed.

Any suggestions would be appreciated.

Thanks...

Mike

edit retag flag offensive close merge delete

Comments

VideoCapture wants either a string or an int. but someone reported, that it works with a named pipe, too (you can pass the name to it). maybe you can build an easy translator.

berak gravatar imageberak ( 2014-09-17 13:48:24 -0600 )edit