mgmege 2022-12-31 21:02 采纳率: 50%
浏览 799
已结题

使用yolov5检测笔记本电脑摄像头,提示AttributeError: 'NoneType' object has no attribute 'shape',求指导

使用yolov5检测笔记本电脑摄像头,提示AttributeError: 'NoneType' object has no attribute 'shape',求助大佬帮忙解决。

具体操作步骤如下:

  1. 搭建好yolov5运行环境
  2. 运行detect.py检测自带的图片成功
parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob/screen/0(webcam)')
  1. 修改detect.py中代码,变为检测笔记本电脑自带的摄像头
parser.add_argument('--source', type=str, default='0', help='file/dir/URL/glob/screen/0(webcam)')
  1. 运行detect.py,看到笔记本电脑的补光灯亮了一会,摄像头应该是起来了,但是过了一会代码报错自动停此运行,报错如下
E:\software\anaconda3\envs\pytorch\python.exe E:/05swstudy/yolov5-master/detect.py
detect: weights=yolov5s.pt, source=0, data=data\coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs\detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False, vid_stride=1
YOLOv5  2022-12-27 Python-3.9.15 torch-1.13.1 CPU

Fusing layers... 
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
[ WARN:0@15.519] global cap_msmf.cpp:1759 CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638
1/1: 0...  Success (inf frames 640x480 at 30.00 FPS)

Traceback (most recent call last):
  File "E:\05swstudy\yolov5-master\detect.py", line 268, in <module>
    main(opt)
  File "E:\05swstudy\yolov5-master\detect.py", line 263, in main
    run(**vars(opt))
  File "E:\software\anaconda3\envs\pytorch\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "E:\05swstudy\yolov5-master\detect.py", line 106, in run
    dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
  File "E:\05swstudy\yolov5-master\utils\dataloaders.py", line 381, in __init__
    s = np.stack([letterbox(x, img_size, stride=stride, auto=auto)[0].shape for x in self.imgs])
  File "E:\05swstudy\yolov5-master\utils\dataloaders.py", line 381, in <listcomp>
    s = np.stack([letterbox(x, img_size, stride=stride, auto=auto)[0].shape for x in self.imgs])
  File "E:\05swstudy\yolov5-master\utils\augmentations.py", line 113, in letterbox
    shape = im.shape[:2]  # current shape [height, width]
AttributeError: 'NoneType' object has no attribute 'shape'

Process finished with exit code 1

具体报错是AttributeError: 'NoneType' object has no attribute 'shape',应该是运行到augmentations.py中的代码时出现问题。查了一下应该是下面的一段代码。

def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
    # Resize and pad image while meeting stride-multiple constraints
    shape = im.shape[:2]  # current shape [height, width]
    if isinstance(new_shape, int):
        new_shape = (new_shape, new_shape)

    # Scale ratio (new / old)
    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:  # only scale down, do not scale up (for better val mAP)
        r = min(r, 1.0)

    # Compute padding
    ratio = r, r  # width, height ratios
    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding
    if auto:  # minimum rectangle
        dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding
    elif scaleFill:  # stretch
        dw, dh = 0.0, 0.0
        new_unpad = (new_shape[1], new_shape[0])
        ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # width, height ratios

    dw /= 2  # divide padding into 2 sides
    dh /= 2

    if shape[::-1] != new_unpad:  # resize
        im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
    im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
    return im, ratio, (dw, dh)

img

第一次玩yolo,看别人的教程操作的,不知道具体是什么问题。
补充,用的时yolov5 v7.0版本。

  • 写回答

2条回答 默认 最新

  • shiter 人工智能领域优质创作者 2023-01-01 00:01
    关注

    [ WARN:0@15.519] global cap_msmf.cpp:1759 CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638
    1/1: 0... Success (inf frames 640x480 at 30.00 FPS)

    这两句说的稍微有点矛盾,没有抓到图像帧,但是显示success 了。

    所以,im 是none ,那么我判断还是摄像头的问题。'0' 还是 0 呢类型不一样,一个是字符串 一个是int ,这块传参的类型有可能有问题。
    最好单步调试一下。im.show 一下看看图形能出来不

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月7日
  • 已采纳回答 1月7日
  • 修改了问题 12月31日
  • 创建了问题 12月31日

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答