求助:目标检测训练完成之后,运行detect.py系统说未检测到图像,在这个示例中,所有的图像都没有检测到目标,因此输出中都标记为“(no detections)“。训练了300次

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在 YOLOv5 的 `detect.py` 文件加入检测所有图像的平均 FPS 的代码,可以在循环外部创建一个计时器,并在循环结束后根据时间和图像数量计算平均 FPS。以下是加入检测所有图像的平均 FPS 的代码示例: ```python import time import cv2 import torch from models.experimental import attempt_load from utils.datasets import LoadStreams, LoadImages from utils.general import check_img_size, non_max_suppression, apply_classifier, \ scale_coords, xyxy2xywh, plot_one_box, strip_optimizer, set_logging from utils.torch_utils import select_device, load_classifier, time_synchronized def detect(opt): set_logging() device = select_device(opt.device) half = device.type != 'cpu' # half precision only supported on CUDA # 加载模型 model = attempt_load(opt.weights, map_location=device) # load FP32 model imgsz = check_img_size(opt.img_size, s=model.stride.max()) # check img_size if half: model.half() # to FP16 # 获取类别名称和颜色 names = model.module.names if hasattr(model, 'module') else model.names colors = [[0, 255, 0]] # 初始化摄像头或视频流 dataset = LoadStreams(opt.source, img_size=imgsz) fps = dataset.cap.get(cv2.CAP_PROP_FPS) # 获取帧率 # 初始化计时器 total_time = 0.0 num_frames = 0 # 循环检测 for path, img, im0s, vid_cap in dataset: t1 = time_synchronized() # 图像预处理 img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() img /= 255.0 if img.ndimension() == 3: img = img.unsqueeze(0) # 模型推理 t2 = time_synchronized() pred = model(img, augment=opt.augment)[0] # 后处理 t3 = time_synchronized() pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms) for i, det in enumerate(pred): if len(det): det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0s.shape).round() for *xyxy, conf, cls in reversed(det): c = int(cls) label = f'{names[c]} {conf:.2f}' plot_one_box(xyxy, im0s, label=label, color=colors[c], line_thickness=3) # 显示图像 t4 = time_synchronized() cv2.imshow('YOLOv5', im0s) # 计算 FPS t5 = time_synchronized() num_frames += 1 total_time += t5 - t1 # 按 'q' 键退出 if cv2.waitKey(1) == ord('q'): break # 释放资源 cv2.destroyAllWindows() dataset.stop() # 输出 FPS avg_fps = num_frames / total_time print(f'FPS: {avg_fps:.2f}') ``` 在上面的代码,我们在循环外部创建了 `total_time` 和 `num_frames` 变量,并在循环内部增加了每个图像的计时器。在循环结束后,我们使用这些变量计算平均 FPS,并输出到控制台。请注意,这里的 `avg_fps` 变量是所有图像的平均 FPS,而不是单个图像的 FPS。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值