Search code examples
pythonopencvgstreamernvidia-jetson

Unable to get frames from MIPI cameras on NVIDIA Jetson board in OpenCV/GStreamer


I have 4 mipi cameras connected to Jetson board. I can stream the cameras using GStreamer, buI want to use OpenCV for further processing. I tried several code but, I cant seem to get the stream from Nvargus in OpenCV, this is the code I've tried.

import cv2
import numpy as np
import time
print(cv2.__version__)
dispW = 1200
dispH = 700
flip=2
font = cv2.FONT_HERSHEY_SIMPLEX
dtav=0

camSet0='nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM), width=(int)500, height=(int)320, framerate=(fraction)30/1, format=(string)NV12 ! nvvidconv ! video/x-raw, width=(int)500, height=(int)320 '
cam1 = cv2.VideoCapture(camSet0)

camSet1='nvarguscamerasrc sensor-id=1 ! video/x-raw(memory:NVMM), width=(int)500, height=(int)320, framerate=(fraction)30/1, format=(string)NV12 ! nvvidconv ! video/x-raw, width=(int)500, height=(int)320 '
cam2 = cv2.VideoCapture(camSet1)

camSet2='nvarguscamerasrc sensor-id=2 ! video/x-raw(memory:NVMM), width=(int)500, height=(int)320, framerate=(fraction)30/1, format=(string)NV12 ! nvvidconv ! video/x-raw, width=(int)500, height=(int)320 '
cam3 = cv2.VideoCapture(camSet2)

camSet3='nvarguscamerasrc sensor-id=3 ! video/x-raw(memory:NVMM), width=(int)500, height=(int)320, framerate=(fraction)30/1, format=(string)NV12 ! nvvidconv ! video/x-raw, width=(int)500, height=(int)320 '
cam4 = cv2.VideoCapture(camSet3)

startTime=time.time()

while True:
    ret, frame1 =cam1.read()
    ret, frame2 = cam2.read()
    ret, frame3 =cam3.read()
    ret, frame4 = cam4.read()
    # Ensure frames are not None before combining them
    if frame1 is None or frame2 is None or frame3 is None or frame4 is None:
        print("Error: One or more frames is None.")
        break

    framecombined = np.hstack((frame1, frame2, frame3, frame4))

    # Check if framecombined is not empty
    if framecombined.size == 0:
        print("Error: Combined frame is empty.")
        break

    cv2.imshow('Combo', framecombined)
    cv2.moveWindow('Combo', 0, 0)

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

cam1.release()
cam2.release()
cam3.release()
cam4.release()
cv2.destroyAllWindows()

The error is always that the frames are empty, i also tried just one camera and get the same error:

File "panz1.py", line 33, in <module>
    cv2.imshow('Combo', frame1)
cv2.error: OpenCV(4.9.0) /io/opencv/modules/highgui/src/window.cpp:971: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

Which means that its not getting any frames.

Please help.


Solution

  • Tried evrything from installing opencv from source multiple times with gstreamer and other settings ON, tried different scripts. Finally had to flash the OS a couple of times with some modification to make file to make it work.