无法在 OpenCV/GStreamer 中从 NVIDIA Jetson 板上的 MIPI 相机获取帧

问题描述 投票:0回答:1

我有 4 个 mipi 摄像头连接到 Jetson 板。我可以使用 GStreamer 流式传输相机,但我想使用 OpenCV 进行进一步处理。我尝试了几个代码,但是,我似乎无法从 OpenCV 中的 Nvargus 获取流,这是我尝试过的代码。

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()

错误始终是框架为空,我也尝试了仅一台相机并得到相同的错误:

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'

这意味着它没有获得任何帧。

请帮忙。

python opencv gstreamer nvidia-jetson
1个回答
0
投票

假设您的相机提供500x320@30,您可以尝试:

import cv2
import re
print('GStreamer support: %s' % re.search(r'GStreamer\:\s+(.*)', cv2.getBuildInformation()).group(1))

camSet0 = 'nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM), width=500, height=320, framerate=30/1, format=NV12 ! nvvidconv ! video/x-raw,format=BGRx,width=500,height=320 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1'
cam1 = cv2.VideoCapture(camSet0, cv2.CAP_GSTREAMER)
if not cam1.isOpened():
     print('Failed to open cam1')
© www.soinside.com 2019 - 2024. All rights reserved.