Not able to get CSI camera feed into opencv-python

I’m on Jetpack 5.1.1 on Jetson orin Nano. I’m trying to use Raspberry PI High Quality camera which have an IMX477 sensor OpenCV-python version is 4.7.0.

I’m trying to get the camera output with python and I am using this code to do so:

import cv2

def gstreamer_pipeline(
sensor_id=0,
capture_width=3840,
capture_height=2160,
display_width=1920,
display_height=1080,
framerate=30,
flip_method=2,
):
return “nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM),width=3840, height=2160,framerate=30/1, format=NV12 ! nvvidconv flip-method=2 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1”

def show_camera():
window_title = “CSI Camera”

# To flip the image, modify the flip_method parameter (0 and 2 are the most common)
print(gstreamer_pipeline(flip_method=2))
video_capture = cv2.VideoCapture(gstreamer_pipeline(flip_method=2), cv2.CAP_GSTREAMER)
print(video_capture.isOpened())
if video_capture.isOpened():
    try:
        window_handle = cv2.namedWindow(window_title, cv2.WINDOW_AUTOSIZE)
        while True:
            ret_val, frame = video_capture.read()
            # Check to see if the user closed the window
            # Under GTK+ (Jetson Default), WND_PROP_VISIBLE does not work correctly. Under Qt it does
            # GTK - Substitute WND_PROP_AUTOSIZE to detect if window has been closed by user
            if cv2.getWindowProperty(window_title, cv2.WND_PROP_AUTOSIZE) >= 0:
                cv2.imshow(window_title, frame)
            else:
                break 
            keyCode = cv2.waitKey(10) & 0xFF
            # Stop the program on the ESC key or 'q'
            if keyCode == 27 or keyCode == ord('q'):
                break
    finally:
        video_capture.release()
        cv2.destroyAllWindows()
else:
    print("Error: Unable to open camera")

if name == “main”:
show_camera()

I’m getting this output :

nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM),width=3840, height=2160,framerate=30/1, format=NV12 ! nvvidconv flip-method=2 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1

Error: Unable to open camera

Please help

Please check if sensor driver loaded and working well.

v4l2-ctl --list-devices

v4l2-ctl --set-fmt-video=width=3840,height=2160 --set-ctrl bypass_mode=0 --stream-mmap

The output of first command is

$ v4l2-ctl --list-devices
NVIDIA Tegra Video Input Device (platform:tegra-camrtc-ca):
/dev/media0

vi-output, imx477 9-001a (platform:tegra-capture-vi:1):
/dev/video0

Second Command Output is

$ v4l2-ctl --set-fmt-video=width=3840,height=2160 --set-ctrl bypass_mode=0 --stream-mmap
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 30.00 fps

Try below command to confirm nvarguscamerasrc working well.

export DISPLAY=:0
gst-launch-1.0 nvarguscamerasrc ! "video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1" ! nvvidconv ! xvimagesink sync=false

When I run this command

gst-launch-1.0 nvarguscamerasrc ! “video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1” ! nvvidconv ! xvimagesink sync=false

I get the following

Invalid MIT-MAGIC-COOKIE-1 keyInvalid MIT-MAGIC-COOKIE-1 keyInvalid MIT-MAGIC-COOKIE-1 keyInvalid MIT-MAGIC-COOKIE-1 keynvbufsurftransform: Could not get EGL display connection
Setting pipeline to PAUSED …
Invalid MIT-MAGIC-COOKIE-1 keyERROR: Pipeline doesn’t want to pause.
ERROR: from element /GstPipeline:pipeline0/GstXvImageSink:xvimagesink0: Could not initialise Xv output
Additional debug info:
xvimagesink.c(1773): gst_xv_image_sink_open (): /GstPipeline:pipeline0/GstXvImageSink:xvimagesink0:
Could not open display (null)
Setting pipeline to NULL …
Freeing pipeline …

If you connect to Orin Nano by ssh to run the command you need export DISPLAY=:0 first.

yes , I ran both

export DISPLAY=:0
gst-launch-1.0 nvarguscamerasrc ! “video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1” ! nvvidconv ! xvimagesink sync=false

BTW also I’m not running commands via SSH I have a physical display, keyboard attached to Orin Nano

Suppose should not “get Could not get EGL display” message if launch command directly input to Nano by keyboard.

Could you reboot and try nvgstcapture-1.0 ?

nvgstcapture_log.txt (4.6 KB)

Please Find attached attached txt file containing the output of the command

nvgstcapture-1.0

I’m using a dell display, I have DVI to DisplayPort connector adapter in between connecting Jetson and the display, do you think it might be because of the cable that it’s not able to detect the display?

That’s weird, don’t know why your device have display error, but you said you have connect HDMI display and working directly on Orin Nano ???

nvbufsurftransform: Could not get EGL display connection
bitrate = 4000

Cannot open display specified with DISPLAY environment variable

can't create window, Display NULL

I’m using a dell display, I have HDMI to DisplayPort connector adapter in between connecting Jetson and the display, do you think it might be because of the cable that it’s not able to detect the display?

update: I changed the display cable now its just a single DisplayPort cable connecting on both end. and the command " nvgstcapture-1.0 "works now ! But the python script still doesn’t work

Hi,

What is the error from python script?

Hello, I think in the lines

">>> video_capture = cv2.VideoCapture(gstreamer_pipeline(flip_method=2), cv2.CAP_GSTREAMER)
">>> print(video_capture.isOpened())
False

I think the main problem is here cv2.VideoCapture doesn’t open the video.

Solution:

Installed OpenCV 4.5.2 from source, removed local installation of opencv-python with

pip uninstall opencv-python

changed the line to

return “nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM), width=(int)3840, height=(int)2160, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! appsink drop=1”

and it worked for me : )

1 Like

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