Analyze video using gstreamer without display on remote Jetson Nano with OpenCV (Python)

Good evening!

I`m trying to analyze videos on remote computers (Jetson Nano) without displays. All information from remote servers stores in one database on main server.

I use OpenCV (Python) to capture video from camera (Raspberry Pi camera V2).

Here is the code I use to capture video:

def get_jetson_gstreamer_source(capture_width=1280, capture_height=720, display_width=1280, display_height=720, framerate=60, flip_method=0):

    return (
            f'nvarguscamerasrc ! video/x-raw(memory:NVMM), ' +
            f'width=(int){capture_width}, height=(int){capture_height}, ' +
            f'format=(string)NV12, framerate=(fraction){framerate}/1 ! ' +
            f'nvvidconv flip-method={flip_method} ! ' +
            f'video/x-raw, width=(int){display_width}, height=(int){display_height}, format=(string)BGRx ! ' +
            'videoconvert ! video/x-raw, format=(string)BGR ! appsink'
            )

video_capture = cv2.VideoCapture(get_jetson_gstreamer_source(), cv2.CAP_GSTREAMER)

With HDMI display it works fine, but without I get an obvious error:

GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 3264 x 1848 FR = 28.000001 fps Duration = 35714284 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1920 x 1080 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 120.000005 fps Duration = 8333333 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: Running with following settings:
   Camera index = 0
   Camera mode  = 4
   Output Stream W = 1280 H = 720
   seconds to Run    = 0
   Frame Rate = 120.000005
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

(python3:7460): Gtk-WARNING **: 23:27:19.966: cannot open display:
CONSUMER: Done Success
WARNING Argus: 5 client objects still exist during shutdown:
        547503252248 (0x7f68003558)
        547508048944 (0x7f680016b0)
        547508049104 (0x7f68001750)
        547508052880 (0x7f680018b0)
        547508054176 (0x7f68003440)

I tried to replace appsink by fakesink as in the example here, but I got an error:

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (801) open OpenCV | GStreamer warning: cannot find appsink in manual pipeline
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

If i try this code from Terminal, it works fine:

gst-launch-1.0 nvarguscamerasrc sensor-id=0 ! ‘video/x-raw(memory:NVMM),width=1920, height=1080, framerate=30/1, format=NV12’ ! fakesink

It is from this branch.

But if I try it from Python:

def get_jetson_gstreamer_source():

                    
return(
 f'nvarguscamerasrc sensor-id=0 ! ' + f'video/x-raw(memory:NVMM),width=1920, height=1080, framerate=30/1, format=NV12 '        +' !          fakesink' 
)

I got:

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (801) open OpenCV | GStreamer warning: cannot find appsink in manual pipeline
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

Help me, please. What should I do to analyze videos on many remote Jetson Nano computers without display?

Best regards, Andron

Hi,
We don’t observe the issue in executing the python code:

import sys
import cv2

def read_cam():
    cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
    if cap.isOpened():
        while True:
            ret_val, img = cap.read();
            cv2.waitKey(10)
    else:
     print "camera open failed"

if __name__ == '__main__':
    read_cam()

FYR.

Thank you for quick answer! There is no errors when running your code. I`ll try it in real conditions.

It is really strange: if I paste your code in my Python script, I get an error.

 cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")

But if I create a new file and paste the same code there (so two scripts are fully identical) it works fine. Is it possible?

Hi,
Probably the file permission is not 755?

nvidia@nvidia-desktop:~$ ll simple_opencv1.py
-rwxr-xr-x 1 nvidia nvidia 632  八  18 08:15 simple_opencv1.py