How to use hard decode in TX2 when I capture video ?

When I use the below code ,it works well,but CPU load is too high!

import cv2

video_source = "rtsp://admin:leXXXXXX@192.168.1.224:554"
gst_str = (
    'rtspsrc location={} latency=100 ! rtph265depay ! h265parse ! omxh265dec ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx ! videoconvert !appsink').format(video_source)

cap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

while (1):
    # get a frame
    ret, frame = cap.read()

    print(ret)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()

When I change code to use omxh264dec ,as below :

gst_str = (
    'rtspsrc location={} latency=100 ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx ! videoconvert !appsink').format(video_source)

it show wrong :
(python3:6202): GStreamer-CRITICAL **: 17:07:32.434:
Trying to dispose element capsfilter0, but it is in PLAYING instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

(python3:6202): GStreamer-CRITICAL **: 17:07:32.437:
Trying to dispose element nvvconv0, but it is in PLAYING instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

Hi,
It is expected. For working with python + OpenCV, it requires to get CPU buffers in BGR format in appsink. This takes memcpy() to copy from NVMM buffers to CPU buffers. On Jetson platforms, optimal performance is to run whole pipeline in passing NVMM buffers.

@DaneLLL thanks !
I want to know how to use hard decode in python + opencv ?

The below code don’t works:

gst_str = (
    'rtspsrc location={} latency=100 ! rtph265depay ! h265parse ! omxh265dec ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx ! videoconvert !appsink').format(video_source)

cap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

Hi,
It looks like your rtsp source is h265 stream. omxh265dec plugin also enables hardware decoding. The high CPU loading is explained in #2

very thanks for your reply!
when I use the below code ,cpu load is too high!!

gst_str = (
    'rtspsrc location={} latency=100 ! rtph265depay ! h265parse ! omxh265dec ! nvvidconv ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)BGRx ! videoconvert !appsink').format(video_source)

cap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

Is there any method to decrease cpu load ?

Look forward to your reply.

Hi,
An optimal solution is to run gstreamer + OpenCV cuda::gpuMat in C code. You may check this sample

can I use hard decode ?

Yes, the optimal solution uses hardware decoding and no extra memcpy. You may give it a try.

Is there solution that don’t use gpu ?
tks!

Hi,
You can also use cv::Mat in the solution. Suggest you give it a try.

I still don’t know how to capture video with gpu ,can you give me a detailed example ? very thx!

Hi,
We would suggest you build/run the sample. The samples are for reference and you may run it first and then use it as a reference for customization.