问题描述
我的相机显示绿色屏幕。我使用的是IMX 219,我不知道为什么相机会显示this output
import cv2
cap=cv2.VideoCapture(0)
while True:
r,im=cap.read()
cv2.imshow('dd',im)
k=cv2.waitKey(30) & 0xff
if k==27:
break
cap.release()
cv2.destroyAllWindows()
解决方法
Ciao
通论
如this link中所述,您可以使用v4l2-ctl
来确定相机的功能。 v4l2-ctl
在v4l-utils
中:
$ sudo apt-get install v4l-utils
然后:
$ v4l2-ctl --list-formats-ext
通过查看相同的链接和this other,我发现您还可以快速测试相机的启动:
# Simple Test
# Ctrl^C to exit
# sensor_id selects the camera slot: 0 or 1 on Jetson Nano B01
$ gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! nvoverlaysink
这个简单的gst-launch
示例可用于确定您所使用的传感器报告的相机模式。例如说您得到以下输出:
GST_ARGUS: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000,max 10.625000; Exposure Range min 13000,max 683709000
然后您应该相应地调整下一个命令:
$ gst-launch-1.0 nvarguscamerasrc sensor_id=1 ! \
'video/x-raw(memory:NVMM),width=3264,height=2464,framerate=21/1,format=NV12' ! \
nvvidconv flip-method=0 ! 'video/x-raw,width=816,height=616' ! \
nvvidconv ! nvegltransform ! nveglglessink -e
sensor_id=1
代表右侧的CSI摄像机插槽,可以是0
或1
。从this link中可以看到,较新的Jetson Nano开发套件带有两个CSI摄像机插槽,您可以使用此属性指定正确的一个[[0
是默认设置]。请注意,在同一链接上,他们使用sensor_mode
而不是sensor_id
,我会同时尝试两者。不过,您不一定需要包含here中已记录的flip-method
。所有这些都应该使您对将值插入代码中有了一个想法
此外,已经注意到显示变换对宽度和高度敏感[在上面的示例中,width=816,height=616
]。如果遇到问题,请检查显示的宽度和高度是否与选定的相机框架尺寸相同[在上面的示例中,816和616分别是3264和2464的四分之一]
OpenCV
在nVidia论坛上四处寻找,this post。在这种情况下,解决方案是使用:
cap = cv2.VideoCapture('nvarguscamerasrc ! video/x-raw(memory:NVMM),width=3280,format=(string)NV12,framerate=(fraction)20/1 ! nvvidconv ! video/x-raw,format=(string)BGRx ! videoconvert ! video/x-raw,format=(string)BGR ! appsink',cv2.CAP_GSTREAMER)
不过,在您的情况下,IMX 219的20fps太高了,帧大小等于3280x2464
。从this link的第一张表中可以看出,建议值为15fps,而here则为21fps。我建议您从上一节中获取的width
,height
,framerate
值开始。低于标称值的framerate
值应该可以帮助您测试连通性
cap = cv2.VideoCapture('nvarguscamerasrc ! video/x-raw(memory:NVMM),framerate=(fraction)15/1 ! nvvidconv ! video/x-raw,cv2.CAP_GSTREAMER)
完整示例包含{strong>已使用正确值更新的前一行的地方from here:
# MIT License
# Copyright (c) 2019 JetsonHacks
# See license
# Using a CSI camera (such as the Raspberry Pi Version 2) connected to a
# NVIDIA Jetson Nano Developer Kit using OpenCV
# Drivers for the camera and OpenCV are included in the base image
import cv2
# gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera
# Defaults to 1280x720 @ 60fps
# Flip the image by setting the flip_method (most common values: 0 and 2)
# display_width and display_height determine the size of the window on the screen
def gstreamer_pipeline(
capture_width=1280,capture_height=720,display_width=1280,display_height=720,framerate=60,flip_method=0,):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM),"
"width=(int)%d,height=(int)%d,"
"format=(string)NV12,framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw,width=(int)%d,format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw,format=(string)BGR ! appsink"
% (
capture_width,capture_height,framerate,flip_method,display_width,display_height,)
)
def show_camera():
# To flip the image,modify the flip_method parameter (0 and 2 are the most common)
print(gstreamer_pipeline(flip_method=0))
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0),cv2.CAP_GSTREAMER)
if cap.isOpened():
window_handle = cv2.namedWindow("CSI Camera",cv2.WINDOW_AUTOSIZE)
# Window
while cv2.getWindowProperty("CSI Camera",0) >= 0:
ret_val,img = cap.read()
cv2.imshow("CSI Camera",img)
# This also acts as
keyCode = cv2.waitKey(30) & 0xFF
# Stop the program on the ESC key
if keyCode == 27:
break
cap.release()
cv2.destroyAllWindows()
else:
print("Unable to open camera")
if __name__ == "__main__":
show_camera()
this other link上还有完整的摘要,可以帮助您找到失败的原因
import cv2
import gi
gi.require_version('Gst','1.0')
from gi.repository import Gst
def read_cam():
cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM),width=(int)1920,height=(int)1080,framerate=(fraction)30/1 ! nvvidconv ! video/x-raw,format=(string)BGR ! appsink")
if cap.isOpened():
cv2.namedWindow("demo",cv2.WINDOW_AUTOSIZE)
while True:
ret_val,img = cap.read();
cv2.imshow('demo',img)
if cv2.waitKey(30) == ord('q'):
break
else:
print ("camera open failed")
cv2.destroyAllWindows()
if __name__ == '__main__':
print(cv2.getBuildInformation())
Gst.debug_set_active(True)
Gst.debug_set_default_threshold(3)
read_cam()
最后,如果您可以从命令行在GStreamer中打开相机,但在Python中却不能,请使用以前的print(cv2.getBuildInformation())
或更早地使用以下命令检查OpenCV版本:
print(cv2.__version__)
从L4T 32.2.1 / JetPack 4.2.2开始,GStreamer支持内置在OpenCV中。这些版本的OpenCV版本为3.3.1,如果您使用的是OpenCV的早期版本(很可能是从Ubuntu存储库安装的),则会出现Unable to open camera
错误
祝你有美好的一天,
安东尼诺