Ask Your Question

Revision history [back]

VideoCapture.read() freezing when reading from gstreamer

Hello,

I've recently encountered an issue where the cv2.VideoCapture.read() function seems to freeze when reading a gstreamer pipeline. The stream seems to work in a glitchy manner for a few seconds, then it freezes completely. After debugging a bit I found it always freezes on the videocapture.read() line.

The capture line is:

cap_receive = cv2.VideoCapture('udpsrc port=5004 caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)MP4V-ES" ! rtpmp4vdepay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)

The while loop where I read and process the frames (there was more image processing but even this simple code still 'breaks'):

while (cap_receive.isOpened()):

    status, frame = cap_receive.read()

    if not status:
        print('empty frame')
        break

    timestamp = datetime.datetime.now()
    cv2.putText(frame, timestamp.strftime(
        "%A %d %B %Y %I:%M:%S%p"), (10, frame.shape[0] - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)

    (flag, encodedImage) = cv2.imencode(".jpg", frame)

    if not flag:
        continue

    yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
           bytearray(encodedImage) + b'\r\n')

This works fine on a windows PC capturing directly from webcam (cv2.VideoCapture(0)), but it doesn't work when trying to capture from the gstreamer pipeline on a microcontroller running Debian 10.4. My opencv version is 4.1.0

Anyone else encounter this issue or know any workarounds? I also tried 'grab' and 'retrieve' with the same results.