网上非常多的代码打不开的原因是要使用Python2!!!Python2!!!Python2!!!
可能是因为Gstreamer管道只适用于Python2
断电安装CSI摄像头,注意排线线缆的方向和正反。安装完成后上电开机
如果是USB摄像头,安装有手就行
JetsonNano终端输入:
ls -l /dev/video*
代码如下(示例):
git clone https://github.com/NVIDIA-AI-IOT/jetcam
cd jetcam
sudo python3 setup.py install
import cv2
# 设置gstreamer管道参数
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,
)
)
if __name__ == "__main__":
capture_width = 1280
capture_height = 720
display_width = 1280
display_height = 720
framerate = 60
flip_method = 0
# 创建管道
print(gstreamer_pipeline(capture_width,capture_height,display_width,display_height,framerate,flip_method))
#管道与视频流绑定
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
if cap.isOpened():
window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
# 逐帧显示
while cv2.getWindowProperty("CSI Camera", 0) >= 0:
ret_val, img = cap.read()
cv2.imshow("CSI Camera", img)
keyCode = cv2.waitKey(30) & 0xFF
if keyCode == 27:# ESC键退出
break
cap.release()
cv2.destroyAllWindows()
else:
print("打开摄像头失败")
from jetcam.csi_camera import CSICamera
camera = CSICamera(width=640, height=480)
image = camera.read()
import ipywidgets
from IPython.display import display
from jetcam.utils import bgr8_to_jpeg
image_widget = ipywidgets.Image(format='jpeg')
image_widget.value = bgr8_to_jpeg(image)
display(image_widget)
camera.running = True
def update_image(change):
image = change['new']
image_widget.value = bgr8_to_jpeg(image)
camera.observe(update_image, names='value')
# 断开摄像头连接
camera.unobserve(update_image, names='value')
import cv2
if __name__ == "__main__":
cap = cv2.VideoCapture(3)
if cap.isOpened():
window_handle = cv2.namedWindow("D435", cv2.WINDOW_AUTOSIZE)
# 逐帧显示
while cv2.getWindowProperty("D435", 0) >= 0:
ret_val, img = cap.read()
cv2.imshow("D435", img)
keyCode = cv2.waitKey(30) & 0xFF
if keyCode == 27:# ESC键退出
break
cap.release()
cv2.destroyAllWindows()
else:
print("打开摄像头失败")
修改usb_camera.py 和 csi_camera.py 20行:
#self.cap = cv2.VideoCapture(self._gst_str(), cv2.CAP_GSTREAMER)
self.cap = cv2.VideoCapture(self.capture_device)
然后重新安装jetcam
pip3 install ipywidgets
jupyter nbextension enable --py widgetsnbextension
jupyter labextension install @jupyter-widgets/jupyterlab-manager
参照JetsonNano学习(二)
终端直接输入命令:
nvgstcapture-1.0
成功调用摄像头
大概率未使用Python2编译