Reading frames from an RTSP stream in Python

I recently installed a Rabberry Pi camera and transmit frames over RTSP. Although this may not be entirely necessary, here is the command I use to broadcast the video:

raspivid -o - -t 0 -w 1280 -h 800 |cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/output.h264}' :demux=h264 

It perfectly conveys the video.

Now I would like to analyze this stream using Python and read each frame separately. I would like to do some motion detection for observation purposes.

I completely lost where to start this task. Can someone point me to a good tutorial? If this is not possible for Python, what tools / languages ​​can I use for this?

+7
python video-streaming video rtsp
source share
3 answers

A bit of a hacky solution, but you can use python VLC bindings and play the stream:

 player=vlc.MediaPlayer('rtsp://:8554/output.h264') player.play() 

Then take a picture every second or so:

 while 1: time.sleep(1) player.video_take_snapshot(0, '.snapshot.tmp.png', 0, 0) 

And then you can use SimpleCV or something for processing (just upload the image file '.snapshot.tmp.png' to your processing library).

+7
source share

Depending on the type of stream, you can probably look at this project for some ideas.

https://code.google.com/p/python-mjpeg-over-rtsp-client/

If you want to be a mega professional, you can use something like http://opencv.org/ (Python modules available to me) to handle motion detection.

+3
source share

Hi, reading frames from a video can be achieved using python and OpenCV. The following is sample code. Works great with python and opencv2 versions.

 import cv2 import os #Below code will capture the video frames and will sve it a folder (in current working directory) dirname = 'myfolder' #video path cap = cv2.VideoCapture("TestVideo.mp4") count = 0 while(cap.isOpened()): ret, frame = cap.read() if not ret: break else: cv2.imshow('frame', frame) #The received "frame" will be saved. Or you can manipulate "frame" as per your needs. name = "rec_frame"+str(count)+".jpg" cv2.imwrite(os.path.join(dirname,name), frame) count += 1 if cv2.waitKey(20) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() 
0
source share

All Articles