使用yolov5检测笔记本电脑摄像头,提示AttributeError: 'NoneType' object has no attribute 'shape',求助大佬帮忙解决。
具体操作步骤如下:
- 搭建好yolov5运行环境
- 运行detect.py检测自带的图片成功
parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob/screen/0(webcam)')
- 修改detect.py中代码,变为检测笔记本电脑自带的摄像头
parser.add_argument('--source', type=str, default='0', help='file/dir/URL/glob/screen/0(webcam)')
- 运行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)
第一次玩yolo,看别人的教程操作的,不知道具体是什么问题。
补充,用的时yolov5 v7.0版本。