OpenCV Video Capture with GStreamer doesn’t work but work on terminal

I use the below gst-launch:
gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM),width=3264, height=2464, framerate=21/1, format=NV12' ! nvvidconv flip-method=0 ! 'video/x-raw,width=320, height=240' ! nvvidconv ! nvegltransform ! nveglglessink -e

it work on terminal but when I try the below code for python it wont work

def t2():
    capture_device = 0
    capture_fps = 21
    capture_width = 3264
    capture_height = 2464
    width = 224
    height = 224
    return 'gst-launch-1.0 nvarguscamerasrc ! video/x-raw(memory:NVMM), width=%d, height=%d, format=(string)NV12, framerate=(fraction)%d/1 ! nvvidconv flip-method=0 ! video/x-raw, width=(int)%d, height=(int)%d ! nvvidconv ! nvegltransform ! nveglglessink -e' % (
                capture_width, capture_height, capture_fps, width, height)

cap = cv2.VideoCapture(t2(), cv2.CAP_GSTREAMER)
#cap = cv2.VideoCapture(0)
re, image = cap.read()

Hi,
Please refer to this sample:
OpenCV Video Capture with GStreamer doesn't work on ROS-melodic - #3 by DaneLLL

For putting gstreamer pipeline in cv2.VideoCapture(), please begin with nvarguscamerasrc and end with appsink.

Thank you and yes, I did the same as the link you shared like 2 days ago but still dont work. Also, I did many different ways to include the gstreamer in python but all did not work.

For better advice, you may post the exact command or code that that you’re trying and error messages that you’re facing, so that your case can be easily reproduced.

First be sure that your python env has an opencv version supporting GStreamer:

import cv2
print(cv2.getBuildInformation())

If it shows Gstreamer SUPPORT YES, then you can get further.

Try:

import cv2

# Reading BGRx frames into opencv:
cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM),width=3264, height=2464, framerate=21/1, format=NV12 ! nvvidconv ! video/x-raw,format=BGRx ! appsink drop=1", cv2.CAP_GSTREAMER)

# Or reading BGR frames as expected by most opencv algorithms:
# cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM),width=3264, height=2464, framerate=21/1, format=NV12 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)

if not cap.isOpened():
    print("failed to open video capture")
    exit(-1)

cv2.namedWindow("CamPreview", cv2.WINDOW_AUTOSIZE)

frames=0
# Run for 10s @21fps
while frames < 210:
   ret_val, img = cap.read();
   if not ret_val:
       print("failed to read from video capture")
       break
   frames = frames + 1

   cv2.imshow('CamPreview',img)

   if cv2.waitKey(1) == ord('q'):
       break
 
cv2.destroyAllWindows()
cap.release()

If this works, you may try resizing into lower resolution.
Note that nvvidconv may not be able to scale into huge factors (but multiple nvviconv rescaling stages may work).
I notice that your gst-launch command uses 320x240 while your opencv example uses 224x224.

1 Like

Thank you for sharing and I just found out that I need to install the right version for opencv when using csi camera in jetson nano which was 4.1.1. After installing this version, it run successfully. Also, the code that you shared is helpful.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.