运行yolov5的detect文件的报错,source为摄像头的时候又是可以正常跑的,source为照片或者视频的时候就会报错。报错如下:
Traceback (most recent call last):
File "/Users/yuhao/Documents/yolov5-master 3/detect.py", line 271, in <module>
main(opt)
File "/Users/yuhao/Documents/yolov5-master 3/detect.py", line 265, in main
run(**vars(opt))
File "/Users/yuhao/opt/anaconda3/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/Users/yuhao/Documents/yolov5-master 3/detect.py", line 98, in run
for path, img, im0s, vid_cap in dataset:#path:图片/视频路径 img:进行resize+pad之后的图片,img0:原size图片,cap:读取当前图片时为None 读取视频时为视频源
ValueError: too many values to unpack (expected 4)
detect文件如下:
import argparse
import sys
import time
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
FILE = Path(__file__).absolute()
sys.path.append(FILE.parents[0].as_posix()) # add yolov5/ to path
from models.experimental import attempt_load
from utils.datasets import LoadStreams, LoadImages
from utils.general import check_img_size, check_requirements, check_imshow, colorstr, non_max_suppression, \
apply_classifier, scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path, save_one_box
from utils.plots import colors, plot_one_box
from utils.torch_utils import select_device, load_classifier, time_synchronized
@torch.no_grad()
def run(weights='yolov5s.pt', # model.pt path(s)
source='data/images', # file/dir/URL/glob, 0 for webcam
imgsz=640, # inference size (pixels)
conf_thres=0.25, # confidence threshold 置信度阈值
iou_thres=0.45, # NMS IOU threshold
max_det=1000, # maximum detections per image
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
view_img=False, # show results
save_txt=False, # save results to *.txt
save_conf=False, # save confidences in --save-txt labels
save_crop=False, # save cropped prediction boxes
nosave=False, # do not save images/videos
classes=None, # filter by class: --class 0, or --class 0 2 3
agnostic_nms=False, # class-agnostic NMS
augment=False, # augmented inference
visualize=False, # visualize features
update=False, # update all models
project='runs/detect', # save results to project/name
name='exp', # save results to project/name
exist_ok=False, # existing project/name ok, do not increment
line_thickness=3, # bounding box thickness (pixels)
hide_labels=False, # hide labels
hide_conf=False, # hide confidences
half=False, # use FP16 half-precision inference
):
save_img = not nosave and not source.endswith('.txt') # save inference images 是否保存图片
webcam = source.isnumeric() or source.endswith('.txt') or source.lower().startswith( #判断预测源是否为视频流
('rtsp://', 'rtmp://', 'http://', 'https://'))
# Directories 获取保存预测路径
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
# Initialize 初始化logging 写日志
set_logging()
device = select_device(device)#获取设备
half &= device.type != 'cpu' # half precision only supported on CUDA 如果设备是gpu且opt.haf=true 使用float16
# Load model 加载Float32模型,确保用户设定的输入图片分辨率能整除32 (如果不能则调整为能整除的 然后返回)
model = attempt_load(weights, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
imgsz = check_img_size(imgsz, s=stride) # check image size
names = model.module.names if hasattr(model, 'module') else model.names # get class names获取类别名字
if half:
model.half() # to FP16
# Second-stage classifier 设置二分类 默认不使用
classify = False
if classify:
modelc = load_classifier(name='resnet50', n=2) # initialize
modelc.load_state_dict(torch.load('resnet50.pt', map_location=device)['model']).to(device).eval()
# Dataloader 通过不同的输入源来设置不同的数据加载方式
if webcam:
view_img = check_imshow()#检查当前环境是否能够正常imshow
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=imgsz, stride=stride)
bs = len(dataset) # batch_size
else:
dataset = LoadImages(source, img_size=imgsz, stride=stride)
bs = 1 # batch_size
vid_path, vid_writer = [None] * bs, [None] * bs
# Run inference
if device.type != 'cpu':
model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once 进行一次前向推理 测试程序是否正常
t0 = time.time()
for path, img, im0s, vid_cap in dataset:#path:图片/视频路径 img:进行resize+pad之后的图片,img0:原size图片,cap:读取当前图片时为None 读取视频时为视频源
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32 图片也设置为Float
img /= 255.0 # 0 - 255 to 0.0 - 1.0 量化归一化
if img.ndimension() == 3:#没有batch_size的话则在最前面添加一个轴
img = img.unsqueeze(0)
# Inference
t1 = time_synchronized()
#前向传播 返回pred的shape是(1,num_boxs, 5 + num_class)
#num_boxs= h/32*w/32+h/16+w/16+h/8+w/8
#pred[...,0:4]为预测框坐标为xywh(中心点+长宽)格式
#pred[...,4]为objectness置信度
#pred[...,5:-1]为分类结果
pred = model(img,
augment=augment,
visualize=increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False)[0]
# Apply NMS 非极大值抑制
#pred:前向传播的输出,conf_thres:置信度阈值,classes:是否只保留特定的类别,agnostic_nms:进行NMS是否也去除不同类别之间的框
#经过NMS后,预测框格式:xywh->xyxy(左上角右下角)
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
t2 = time_synchronized()
# Apply Classifier 添加二次分类 默认不使用
if classify:
pred = apply_classifier(pred, modelc, img, im0s)
# Process detections
#对每一张图片进行处理
for i, det in enumerate(pred): # detections per image
#如果输入源是webcam,则batch_size不为1,取出dataset中的一张图片
if webcam: # batch_size >= 1
p, s, im0, frame = path[i], f'{i}: ', im0s[i].copy(), dataset.count
else:
p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)
p = Path(p) # to Path
save_path = str(save_dir / p.name) # img.jpg 设置保存图片/视频的路径
txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # img.txt 设置保存框坐标txt文件的路径
s += '%gx%g ' % img.shape[2:] # print string 打印图片长宽
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
imc = im0.copy() if save_crop else im0 # for save_crop
if len(det):
# Rescale boxes from img_size to im0 size 调整预测框的坐标
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
# Print results 打印检查到的类别数量
for c in det[:, -1].unique():
n = (det[:, -1] == c).sum() # detections per class
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
# Write results 保存预测结果
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
#将xyxy格式转为xywh格式,并除以w h做归一化,转为列表再保存
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
#在原图上画框
if save_img or save_crop or view_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
plot_one_box(xyxy, im0, label=label, color=colors(c, True), line_thickness=line_thickness)
if save_crop:
save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
# Print time (inference + NMS) 打印前向传播和NMS时间
print(f'{s}Done. ({t2 - t1:.3f}s)')
# Stream results 如果设置显示,则show图片/视频
if view_img:
cv2.imshow(str(p), im0)
cv2.waitKey(1) # 1 millisecond
# Save results (image with detections)保存结果
if save_img:
if dataset.mode == 'image':
cv2.imwrite(save_path, im0)
else: # 'video' or 'stream'
if vid_path[i] != save_path: # new video
vid_path[i] = save_path
if isinstance(vid_writer[i], cv2.VideoWriter):
vid_writer[i].release() # release previous video writer
if vid_cap: # video
fps = vid_cap.get(cv2.CAP_PROP_FPS)
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
else: # stream
fps, w, h = 30, im0.shape[1], im0.shape[0]
save_path += '.mp4'
vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
vid_writer[i].write(im0)
#显示保存信息
if save_txt or save_img:
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
print(f"Results saved to {save_dir}{s}")
if update:
strip_optimizer(weights) # update model (to fix SourceChangeWarning)
print(f'Done. ({time.time() - t0:.3f}s)')
def parse_opt():
parser = argparse.ArgumentParser()
#修改权重路径 weight:训练的权重
parser.add_argument('--weights', nargs='+', type=str, default='runs/train/exp/weights/best.pt', help='model.pt path(s)')
#修改测试图片路径,source:测试数据路径可以说图片视频,也可以是'0'(电脑自带摄像头),也可以是rtsp等视频流
parser.add_argument('--source', type=str, default='my_dates/images/test', help='file/dir/URL/glob, 0 for webcam')
#parser.add_argument('--source', type=str, default='0', help='file/dir/URL/glob, 0 for webcam')
#网络输入图片大小
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
# 置信度阈值
parser.add_argument('--conf-thres', type=float, default=0.55, help='confidence threshold')
# 做nms的IOU阈值
parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
# 保留最大检测框数量
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
# 设备
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
#是否展示预测之后的图片/视频 默认False,我这里设置为ture
parser.add_argument('--view-img', default=False,action='store_true', help='show results')
#是否将预测的框坐标以txt文件的形式保存,默认False
parser.add_argument('--save-txt', default=True,action='store_true', help='save results to *.txt')
#是否将置信度conf也保存到txt中,默认False
parser.add_argument('--save-conf', default=True,action='store_true', help='save confidences in --save-txt labels')
#是否保存裁剪预测框图片
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
#不保存图片或视频
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
#设置只保留某一部分类别 形如0或者2 3
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
#进行nms是否也去除不同类别之间的框 默认false
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
#推理的时候进行多尺度,翻转等操作TTA推理
parser.add_argument('--augment', action='store_true', help='augmented inference')
#是否可视化网络层输出特征
parser.add_argument('--visualize', action='store_true', help='visualize features')
#如果为true,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认False
parser.add_argument('--update', action='store_true', help='update all models')
#保存测试文件的文件夹的路径
parser.add_argument('--project', default='runs/detect', help='save results to project/name')
#保存测试日志文件夹的名字,
parser.add_argument('--name', default='exp', help='save results to project/name')
#是否重新创建日志文件,False时重新创建文件
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
#画框的线条粗细
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
#可视化时隐藏预测类别
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
#可视化时隐藏置信度
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
#是否使用F16精度推理
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
opt = parser.parse_args()
return opt
def main(opt):
print(colorstr('detect: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items()))
#检查环境
check_requirements(exclude=('tensorboard', 'thop'))
run(**vars(opt))
if __name__ == "__main__":
opt = parse_opt()
main(opt)
提供下数据集的信息,赶紧像是你的格式问题
训练时,Detect模块的作用就是使用nn.Conv2d
对输入数据进行卷积操作,使输出为3 * 85 = 255个通道。3表示每个网格有3个anchor box。85 = 4 + 1 + 80,4表示检测框相对于某anchor的位置信息,1表示包含目标的置信度,80表示coco数据集中80个类别的概率。对卷积的输出进行reshape操作,将其转换为(bs,3,h,w,85)的格式进行输出。
可以在run函数中添加以下代码用于提示用户输入相关信息:
# 添加提示语句
print("欢迎使用yolov5目标检测程序,请回答以下问题:")
# 获取用户输入
version = input("请问您使用的是哪个版本的yolov5?")
system = input("您所使用的操作系统是什么?")
python_version = input("您所使用的Python版本是多少?")
library_version = input("您所使用的yolov5相关库的版本是多少?")
input_type = input("请问您输入照片或视频的方式和格式是什么?")
# 将获取的信息打印显示
print("您的yolov5版本是:", version)
print("您的操作系统是:", system)
print("您的Python版本是:", python_version)
print("您所使用的yolov5相关库的版本是:", library_version)
print("您输入照片或视频的方式和格式是:", input_type)
# 在函数参数中添加相关参数,如下所示
def run(weights=ROOT / 'yolov5s.pt',
source=ROOT / 'data/images',
data=ROOT / '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=ROOT / 'runs/detect',
name='exp',
exist_ok=False,
line_thickness=3,
hide_labels=False,
hide_conf=False,
half=False,
dnn=False,
version=version, # 添加新参数 version
system=system, # 添加新参数 system
python_version=python_version, # 添加新参数 python_version
library_version=library_version, # 添加新参数 library_version
input_type=input_type, # 添加新参数 input_type
):
值得注意的是,以上添加的参数也需要在函数参数中进行声明,如上面代码所示。
原因:在调用函数的时候,如果函数返回值是3个,而你只写了1或者2个的话,也会报这个错误,所以将所以的返回值都写出来,不管用还是不用。
从报错信息来看,应该是在 for 循环中,dataset 返回的值数量不正确。dataset 应该返回4个值,但是实际返回的值数量不是4个。你可以检查一下 dataset 的实现,看看是否返回了正确的数量的值。
应该是source,dataset= LoadImages(source, img_size=imgsz,stride=stride)出了问题,总之就是读取不到文件。
最后解决是我用了https://blog.csdn.net/m0_53392188/article/details/119334634的文件,就是把我训练好的mydates那些文件替换这个博主的文件,然后就正常运行了。至于为什么读取不到文件,还是没有解决,可能是去年我捣鼓它的时候,改了哪里,但是忘记了改哪了。
你这个dataset 使用图片和视频的时候,输出的参数数量不对
以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:
从报错信息看,在读取图片或视频进行检测时,dataset生成器返回的元组元素个数超过了detect.py中run()函数对其的解包预期,导致ValueError。
可能的原因有两点:
dataset生成器实际返回的元组元素个数增加了,但run()函数的解包语句未作相应修改,造成解包数值不匹配。
dataset生成器逻辑错误,偶尔返回超过4个元素的元组,导致解包失败。
解决思路:
检查dataset生成器的最新代码,确认其正常情况下返回的元组元素个数是否仍为4。如果变化了,相应修改run()函数中的解包语句。
使用异常处理,捕获解包ValueError,在except中打印dataset生成器返回的实际元组,确认其元素个数。例如:
python
try:
for path, img, im0s, vid_cap in dataset:
...
except ValueError as e:
print(f'dataset returns {len(dataset)} values')
检查dataset生成器的逻辑,确认其在所有情况下均正常返回4个元素的元组。如果有异常情况返回超过4个元素的元组,应修复这部分逻辑。
作为补充,你也可以在dataset生成器的返回语句前添加assert语句,确认其返回的元组元素个数为4,例如:
python
...
assert len(output) == 4
return path, img, im0s, vid_cap
这可以快速定位dataset生成器逻辑错误导致的返回元组元素个数异常的情况。
上述思路希望能帮助你解决在读取图片/视频时出现的报错,定位和修复dataset生成器的异常逻辑。