Jetson nano csi camera read opencv with python

the command below runs on command line and shows me the camera but when I want to read frames with python in opencv it doesn’t work.

gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! 'video/x-raw(memory:NVMM),width=3280, height=2464,framerate=21/1, format=NV12' ! nvvidconv flip-method=2 ! 'video/x-raw, width=816, height=616' ! nvvidconv ! nvegltransform ! nveglglessink -e

The code is below:

import cv2
class CSIRetriever:

    def _gstreamer_pipeline(self):
           return "gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! 'video/x-raw(memory:NVMM),width=3280, height=2464,framerate=21/1, format=NV12' ! nvvidconv flip-method=2 ! 'video/x-raw, width=816, height=616' ! nvvidconv ! nvegltransform ! nveglglessink -e ! appsink"
        
    def __init__(self):
        self.video_capture = cv2.VideoCapture(self._gstreamer_pipeline(), cv2.CAP_GSTREAMER)
        
    def isAvailable(self)->bool:
        if not self.video_capture.isOpened():
	        print("Couldn't open camera")
	        return False
        return True

    def get_frame(self):
        ret_val=False
        frame=None
        if self.isAvailable():
	        try:
	            ret_val, frame = self.video_capture.read()
	        except:
	            return False, None
        return ret_val, frame

    def _show_camera(self):
        window_title = "CSI Camera"
        window_handle = cv2.namedWindow(window_title, cv2.WINDOW_AUTOSIZE)
        # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
        if self.isAvailable():
	        try:
	            window_handle = cv2.namedWindow(window_title, cv2.WINDOW_AUTOSIZE)
	            while True:
	                ret_val, frame = self.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:
	            self.video_capture.release()
	            cv2.destroyAllWindows()
        else:
	        print("Error: Unable to open camera")

if __name__ == "__main__":
    cret=CSIRetriever()
    cret._show_camera()

I would be grateful if you can help.
I am using 4GB ram version of jetson nano

The pipeline is not correct.
First remove gst-launch-1.0 that is the command from shell for running a pipeline. Also remove single quotes that are only useful from shell for preventing it from interpreting parenthesis.
Second, nveglglessink being a pure sink, it has no output.
For receiving with OpenCv you would probably need a pipeline providing BGR format to OpenCv for processing color frames. Try:

return "nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM),width=3280, height=2464,framerate=21/1, format=NV12 ! nvvidconv flip-method=2 ! video/x-raw,format=BGRx, width=816, height=616, pixel-aspect-ratio=1/1 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1"

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