Home Read Frames from RTSP Stream in Python
Reply: 3

Read Frames from RTSP Stream in Python

fmorstatter
1#
fmorstatter Published in 2013-07-31 03:54:53Z

I have recently set up a Raspberry Pi camera and am streaming the frames over RTSP. While it may not be completely necessary, here is the command I am using the 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

This streams the video perfectly.

What I would now like to do is parse this stream with Python and read each frame individually. I would like to do some motion detection for surveillance purposes.

I am completely lost on where to start on this task. Can anyone point me to a good tutorial? If this is not achievable via Python, what tools/languages can I use to accomplish this?

Samadi
2#
Samadi Reply to 2014-01-10 19:22:00Z

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

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

Then take a snapshot 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 load the image file '.snapshot.tmp.png' into your processing library).

synthesizerpatel
3#
synthesizerpatel Reply to 2013-07-31 12:10:24Z

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

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

If you want to be mega-pro, you could use something like http://opencv.org/ (Python modules available I believe) for handling the motion detection.

deepu
4#
deepu Reply to 2016-07-04 11:06:14Z

Hi reading frames from video can be achieved using python and OpenCV . Below is the sample code. Works fine with python and opencv2 version.

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()
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.450338 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO