目标检测算法——YOLOv5如何改变bbox检测框的粗细大小

目标检测算法——YOLOv5如何改变bbox检测框的粗细大小,第1张

问题1:

由于不同数据集中图片分辨率存在一定的差异性,比如自身数据集图片大小为1920×1080,发现在预测过程中bbox检测框太细,最终甚至无法看清楚实际预测值。


问题2:

部分小伙伴的检测框太粗而导致检测目标被遮挡,尤其是当目标较小并且很密集时,目标很容易就被框挡住,不容易观察整体检测效果如何。


解决方法:

在detect.py文件中找到下面这行:

# Process detections
        for i, det in enumerate(pred):  # detections per image
            if webcam:  # batch_size >= 1
                p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count
            else:
                p, s, im0, frame = path, '', im0s, 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
            s += '%gx%g ' % img.shape[2:]  # print string
            gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwh
            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
                        xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywh
                        line = (cls, *xywh, conf) if opt.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 view_img:  # Add bbox to image
                        label = f'{names[int(cls)]} {conf:.3f}'
                        
############################
plot_one_box(xyxy, im0, label=label, 
color=colors[int(cls)], line_thickness=6)

​各位小伙伴可自行调整line_thickness值大小,就可以改变预测框的粗细啦~ 

方便的话,顺便给博主点个赞哇~

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/578021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-11
下一篇 2022-04-11

发表评论

登录后才能评论

评论列表(0条)

保存