当前位置:   article > 正文

YOLOv5 调用 NVIDIA JetsonTX2 板载摄像头出错_assertionerror: failed to open 0

assertionerror: failed to open 0

在这里插入图片描述

如果调用摄像头出现以下错误,可能是摄像头驱动有问题,也有可能opencv有问题,opencv-python版本降低到4.3以下试试。
摄像头驱动参考文章

shape = im.shape[:2]
AttributeError: "NoneType" object has no attribute 'shape'
  • 1
  • 2

detect.py

执行命令

$ python3 detect.py --source 0 --exist-ok --weights yolov5s.pt
  • 1

报错:AssertionError: Failed to open 0
在这里插入图片描述

解决方法

注意:cv2.VideoCapture(0)是打不开的!!!

需要把括号里面的0替换成 gst_str 这一长串字符串

        gst_str = ('nvarguscamerasrc ! '
                   'video/x-raw(memory:NVMM), '
                   'width=(int)1920, height=(int)1080, '
                   'format=(string)NV12, framerate=(fraction)30/1 ! '
                   'nvvidconv flip-method=2 ! '
                   'video/x-raw, width=(int){}, height=(int){}, '
                   'format=(string)BGRx ! '
                   'videoconvert ! appsink').format(960, 640)
        
        cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这要求 GStreamer 打开1920像素宽,1080高@ 30帧/秒的相机流,并在960像素宽640像素高的窗口中显示它。当您需要更改相机的方向时(翻转图片),flip-method 参数可以进行设置。

以上参数说明参考文章:Jetson Nano + Raspberry Pi Camera

改动的是 utils 文件夹里面的 dataset.py 文件夹,下图是我自己的修改

这里放上我的datasets.py链接

if source_list == 0:#调用板载摄像头
    source_list = 'nvarguscamerasrc ! \
                   video/x-raw(memory:NVMM), width=%d, height=%d, format=(string)NV12,\
                   framerate=(fraction)%d/1 ! \
                   nvvidconv flip-method=%d ! nvvidconv ! \
                   video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! \
                   videoconvert ! appsink' % (1280, 720, 30, 0, 640, 480)

cap = cv2.VideoCapture(source_list, cv2.CAP_GSTREAMER)#开启摄像头 eval(s) if s.isnumeric() else s
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

解决完上面那个问题,就可以使用yolov5调用板载摄像头了

YOLOV5 执行 detect.py 正常显示

在这里插入图片描述

后来我尝试了一下命令行直接打开摄像头,弹窗一闪即逝,然后我又使用python3 detect.py --source 0 --exist-ok --weights yolov5s.pt命令时又出现报错

$ gst-launch-1.0 nvarguscamerasrc sensor_mode=0 ! 'video/x-raw(memory:NVMM),width=3820, height=2464, framerate=21/1, format=NV12' ! nvvidconv flip-method=0 ! 'video/x-raw,width=960, height=616' ! nvvidconv ! nvegltransform ! nveglglessink -e
  • 1

又遇到报错:
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:543 Failed to create CaptureSession

AttributeError: ‘NoneType’ object has no attribute ‘shape’
在这里插入图片描述

解决方法

执行下面这条命令,重启 nvargus-daemon

sudo systemctl restart nvargus-daemon
  • 1

命令行直接打开摄像头正常显示

在这里插入图片描述

这个报错的原因(看得不是很明白): Camera Software Development Solution

翻译原文:

此用例与典型的相机用例不同,在典型的相机用例中,相机传感器连续流传输帧,并且相机驱动程序和关联的硬件等待相机帧的超时时间有限。

在此用例中,触发了摄像头传感器以生成指定数量的帧,此后它将无限期停止流式传输。 只要恢复流式传输,相机驱动程序就必须继续捕获而不会出现超时问题。 摄像头驱动程序和硬件必须始终准备就绪,以捕获来自CSI传感器的帧。 由于摄像头驱动程序不知道何时将重新开始流传输,因此它必须无限期地等待传入的帧。

为了支持该用例,相机传感器硬件模块必须支持挂起和恢复流。
要启用此功能,请通过设置环境变量来运行摄像头服务器(即nvargus-daemon服务):

sudo service nvargus-daemon stop
sudo enableCamInfiniteTimeout=1 nvargus-daemon
  • 1
  • 2

在这里插入图片描述

参考文章:
1 Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvargus

2 Jetson TX2使用cv2调用板载摄像头

3 Capture and display video from either IP CAM, USB webcam, or the Tegra X2/X1 onboard camera.

4 Nano入门教程硬件篇-增加摄像头

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/888892
推荐阅读
相关标签
  

闽ICP备14008679号