我的相机显示了一个绿色的屏幕。我正在使用IMX 219,我不知道为什么相机会给这个输出
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()发布于 2020-10-09 02:28:39
通用理论
正如在此链接中所说的,您可以使用v4l2-ctl来确定相机的功能。v4l2-ctl在v4l-utils中
$ sudo apt-get install v4l-utils然后:
$ v4l2-ctl --list-formats-ext通过查看相同的链接和另一个,我看到您也可以快速测试相机的发射情况:
# 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 -esensor_id=1代表正确的CSI摄像机插槽,可以是0,也可以是1。从此链接中可以看到,新的附带了两个CSI摄像头插槽,您可以使用这个属性指定正确的0是默认的。请注意,在相同的链接中,他们使用的是sensor_mode而不是sensor_id,我会尝试这两种方法。您不一定需要包括flip-method,虽然这是文档化的这里。所有这些都会让您对代码中插入的值有一个概念。
另外,在上面的例子width=816, height=616中,显示变换对宽度和高度是敏感的。如果遇到问题,请检查显示的宽度和高度是否与上述示例中选择的摄像机帧大小相同,816和616分别为3264和2464的四分之一。
OpenCV
环顾nVidia论坛,我发现了这个职位。在这种情况下,解决办法是使用:
cap = cv2.VideoCapture('nvarguscamerasrc ! video/x-raw(memory:NVMM), width=3280, height=2464, 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的20 for太高,帧大小等于3280x2464。从此链接的第一个表中可以看到,建议值为15 can,而这里建议为21 can。我建议您从上一节中检索的width、height、framerate值开始。低于标称值的framerate值将帮助您测试连接性。
cap = cv2.VideoCapture('nvarguscamerasrc ! video/x-raw(memory:NVMM), width=3280, height=2464, format=(string)NV12, framerate=(fraction)15/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink', cv2.CAP_GSTREAMER)包含用正确值更新的上一行的完整示例是可用的从这里开始。
# 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, height=(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()在另一个链接中也有一个完整的片段可以帮助您找到失败的原因。
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,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! 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错误
https://stackoverflow.com/questions/64272731
复制相似问题