在TX2上使用cv2.VideoCapture(0)是无法打开板载摄像头的!
如果插入额外的usb摄像头,使用cv2.VideoCapture(1)就可以轻松打开usb摄像头。
而对于板载摄像头的调用,需要设置一些列参数,如下:
def open_cam_onboard(width=1920, height=1080):
import subprocess
gst_elements = str(subprocess.check_output('gst-inspect-1.0'))
if 'nvcamerasrc' in gst_elements:
# On versions of L4T prior to 28.1, add 'flip-method=2' into gst_str
gst_str = ('nvcamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)2592, height=(int)1458, '
'format=(string)I420, framerate=(fraction)30/1 ! '
'nvvidconv ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(width, height)
elif 'nvarguscamerasrc' in gst_elements:
gst_str = ('nvarguscamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)1920, height=(int)1080, '
'format=(string)NV12, framerate=(fraction)30/1 ! '
'nvvidconv flip-method=0 ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(width, height)
else:
raise RuntimeError('onboard camera source not found!')
return gst_str
用gst_str代替摄像头编号0就可以了,我的TX2使用的是'nvarguscamerasrc'的设置,其中flip-method用于改变画面方向
gst_str = open_cam_onboard()
ret, frame = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
参考文献
https://gist.github.com/jkjung-avt/86b60a7723b97da19f7bfa3cb7d2690e
网友评论