Search code examples
pythonopencvgstreamerip-camerahikvision

Changing gstreamer pipeline to opencv in python


I have successfully installed and built gstreamer on opencv. It says YES when i print(cv2.getBuildInformation()).

Gstreamer version: 1.14.0
opencv version: 3.4.5.20

Bumped into a wall for quite a while at the moment, was using the gstreamer pipeline. It works wonderfully.

gst-launch-1.0 -v playbin uri=rtsp://admin:[email protected]:554/Streaming/Channels/400 uridecodebin0::source::latency=10

This is the python script I've written for the gstreamer pipeline.

import cv2
import numpy as np

pipe = '"rtspsrc location=\"rtsp://admin:[email protected]:554/Streaming/Channels/400" latency=10 ! appsink'

cap = cv2.VideoCapture(pipe)

if not cap.isOpened():
    print('VideoCapture not opened')
    exit(0)

while True:
    ret, frame = cap.read()

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

    cv2.imshow('display', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    cap.release()

cv2.destroyAllWindows()

I keep getting this error though, unable to troubleshoot what caused this.

gst_element_get_state: assertion 'GST_IS_ELEMENT (element)' failed

Solution

  • Update: Found the answer by Fixing the pipeline using rtspsrc instead of playbin.

    Opencv's VideoCapture is not able to take autovideoconvert and autovideosink. A work around would be to use videoconvert and appsink when using rtspsrc but latency can't be adjusted. However, if you want to adjust the latency you will need to use decodebin to decode instead of avdec_h264

    e.g. gst-launch-1.0 rtspsrc location://admin:[email protected]:554 latency=20 ! rtph264depay ! h264parse ! decodebin ! videoconvert ! appsink

    NB: Pipeline will work in the python script but may not work in the command line.

    This is for RTSP with IP cameras!

    Cheers.