Search code examples
pythonopencvraspberry-pi3h.264rtsp

Capturing a SINGLE image from an rtsp H.264 video stream


I'm trying to capture a single image on demand from an RTSP H.264 video frame. I'm using OpenCV with Python running on a Raspberry Pi.

My understanding is that you can't simply capture an image, but rather must constantly read the stream of images from the video and discard all but the occasional one you want. This is very computationally expensive and consumes about 25% of the CPU on a Pi to do nothing but read and discard 1280x720 15 fps H.264 rtsp video frames.

Is there another way? I'm flexible and can also use GStreamer, FFMPEG or anything else that is more computationally efficient.


Solution

  • To answer my own question. Instead of using read():

    cap = cv2.VideoCapture('rtsp_url')
    
    def captureimages():
        while True:
            image = cap.read()
    
    s = threading.Thread(target=captureimages)
    s.start()
    
    if takepic == True:
        picture = image.copy()
    

    It is more efficient to break it up in to grab() and retrieve(). Not a perfect solution, but better:

    cap = cv2.VideoCapture('rtsp_url')
    
    def captureimages():
        while True:
            cap.grab()
    
    s = threading.Thread(target=captureimages)
    s.start()
    
    if takepic == True:
        picture = cap.retrieve()