JETSON XAVIER NX开发套件通过python+opencv 调用USB camera没反应

JETSON XAVIER NX开发套件是通过sd卡烧的JP4.4DP版本镜像(nv-jetson-nx-sd-card-image-r32.4.2.zip)
NX烧写完系统启动后,任何库都没有安装
确认python版本3.6.9 cv2版本4.1.1
NX接上usb camera后,通过ls /dev/vide* 查询结果为 /dev/video0

调用代码片段如下:
import cv2
cap1 = cv2.VideoCapture(0)

while cap1.isOpened():
read1, frame1 = cap1.read()
cv2.imshow(“USB Camera 0”,frame1)
if cv2.waitKey(100) == 27:
break
cap1.release()
cv2.destroyAllWindows()

python3执行,没有反应,没有窗体产生,也没有报错。
等待大约5~10秒后,按下Ctrl+C后,终端输出如下:
(python3:29288): GStreamer-CRITICAL **: 16:37:29.044:
Trying to dispose element pipeline0, but it is in PAUSED instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (886) open OpenCV | GStreamer warning: unable to start pipeline

(python3:29288): GStreamer-CRITICAL **: 16:37:29.045:
Trying to dispose element videoconvert0, but it is in PLAYING instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

(python3:29288): GStreamer-CRITICAL **: 16:37:29.045:
Trying to dispose element appsink0, but it is in READY instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
select timeout
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
VIDEOIO ERROR: V4L: can’t open camera by index 0
select timeout
select timeout
VIDIOC_REQBUFS: Device or resource busy
Traceback (most recent call last):
File “camera.py”, line 3, in
cap1 = cv2.VideoCapture(0)
KeyboardInterrupt

查询摄像头支持的格式,结果如下:
pqai@pqai-desktop:~/Downloads/pq_temp$ v4l2-ctl -d /dev/video0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: ‘MJPG’ (compressed)
Name : Motion-JPEG
Size: Discrete 3264x2448
Interval: Discrete 0.067s (15.000 fps)
Size: Discrete 2592x1944
Interval: Discrete 0.050s (20.000 fps)
Size: Discrete 2048x1536
Interval: Discrete 0.050s (20.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1600x1200
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1080x1920
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 720x1280
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)

Index       : 1
Type        : Video Capture
Pixel Format: 'YUYV'
Name        : YUYV 4:2:2
	Size: Discrete 640x480
		Interval: Discrete 0.033s (30.000 fps)
	Size: Discrete 800x600
		Interval: Discrete 0.050s (20.000 fps)
	Size: Discrete 720x1280
		Interval: Discrete 0.100s (10.000 fps)
	Size: Discrete 1280x720
		Interval: Discrete 0.100s (10.000 fps)
	Size: Discrete 1080x1920
		Interval: Discrete 0.200s (5.000 fps)
	Size: Discrete 1600x1200
		Interval: Discrete 0.200s (5.000 fps)
	Size: Discrete 1920x1080
		Interval: Discrete 0.200s (5.000 fps)
	Size: Discrete 2048x1536
		Interval: Discrete 0.500s (2.000 fps)
	Size: Discrete 2592x1944
		Interval: Discrete 0.500s (2.000 fps)
	Size: Discrete 3264x2448
		Interval: Discrete 0.500s (2.000 fps)

但是在NX上安装cheese后执行cheese指令,是可以显示usb camera图像的,只不过有延迟。
另外,在另外一个Nano开发套件上使用同样的代码调用同一个usb camera,却是成功的。这个Nano开发套件是采购的一款jetbot小车,当时卖家提供了一个jetbot的镜像jetbot_image_v0p3p0_63GB,这个镜像烧到nano上,就可以用现在的这个摄像头。
但是,如果给这个nano烧最新的jp4.4dp(对应Nano的)镜像,也没法用这个摄像头,也就是说同样的nano开发套件,同样的摄像头,同样的代码调用方式,区别就在于nano上烧写的镜像不一样。

备注:该摄像头在PC上使用也正常

Please note that english is the usage in this forum for broad understanding.

Seems your opencv version tries gstreamer API for VideoCapture.
You may try to specify V4L API:
cv2.VideoCapture(0, cv2.CAP_V4L)

2 Likes

Thank you. It solved my problem perfectly.I will try my best to improve my English so that I can ask questions in English at this forum.