CSI 摄像头可以在终端中使用,但不能在 Visual Studio 中与 Python 一起使用。我正在使用 Jetson Orin Nano。当我在终端中运行以下命令时,相机即可工作:
gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=1280, height=720, framerate=30/1' ! nvvidconv ! nvegltransform ! nveglglessink -e
但是,在以下Python脚本中相机无法工作:
import cv2
cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=1280, height=720, framerate=30/1 ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink")
while True:
ret, frame = cap.read()
if not ret:
print("Unable to capture camera image")
break
cv2.imshow("CSI Camera", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
运行脚本时收到以下输出:
onur@ubuntu:~/Desktop/Project$ /bin/python /home/onur/Desktop/Project/simple_camera.py
nvarguscamerasrc sensor-id=0 ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, framerate=(fraction)30/1 ! nvvidconv flip-method=0 ! video/x-raw, width=(int)960, height=(int)540, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink
Error: Unable to open camera
经过研究,建议我运行以下代码:
import cv2
print(cv2.getBuildInformation())
在输出中,我看到:
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (59.37.100)
avformat: YES (59.27.100)
avutil: YES (57.28.100)
swscale: YES (6.7.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
GStreamer: NO
线似乎表明有问题。我该如何解决这个问题?
您必须在启用 gstreamer 的情况下构建 opencv。
https://qengineering.eu/install-opencv-on-orin-nano.html 按照这个并阅读脚本,有很多选项可供选择(例如 cuda、cudnn 之类的)