yolov5返回坐标的方法实例

作者:weixin_44726793 时间:2023-10-05 20:09:43 

yolov5返回坐标(v6版)

1 、从yolov5文件夹李找到detect.py,按Ctrl+F 输入annotator.box_label;

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}')
                       annotator.box_label(xyxy, label, color=colors(c, True))

2、找到这个代码后按住ctrl键,鼠标点击box_label,就会跳到plots.py文件并定位到box_label定义的地方;

3、找到p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3])),在这行代码下面新增:

print("左上点的坐标为:(" + str(p1[0]) + "," + str(p1[1]) + "),右下点的坐标为(" + str(p2[0]) + "," + str(p2[1]) + ")")

4、完成后的代码如下:

def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
       # Add one xyxy box to image with label
       if self.pil or not is_ascii(label):
           self.draw.rectangle(box, width=self.lw, outline=color)  # box
           if label:
               w, h = self.font.getsize(label)  # text width, height
               outside = box[1] - h >= 0  # label fits outside box
               self.draw.rectangle([box[0],
                                    box[1] - h if outside else box[1],
                                    box[0] + w + 1,
                                    box[1] + 1 if outside else box[1] + h + 1], fill=color)
               # self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls')  # for PIL>8.0
               self.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)
       else:  # cv2
           p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
           print("左上点的坐标为:(" + str(p1[0]) + "," + str(p1[1]) + "),右下点的坐标为(" + str(p2[0]) + "," + str(p2[1]) + ")")

cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)

5、测试情况:回到命令行,cd到yolov5文件夹,输入指令:python detect.py --source ../mask.1.jpg,其中mask.1.jpg应改为你yolov5文件夹下的图片名称;按回车键后运行就发现输出的信息多了刚才添加的一行

(venv) (base) rongxiao@rongxiao:~/PycharmProjects/yolococo/yolov5$ python detect.py --source ../mask.1.jpg
detect: weights=yolov5s.pt, source=../mask.1.jpg, 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
YOLOv5 🚀 v6.0-147-g628817d torch 1.8.2+cpu CPU

Fusing layers...
Model Summary: 213 layers, 7225885 parameters, 0 gradients
左上点的坐标为:(982,384),右下点的坐标为(1445,767)
左上点的坐标为:(724,237),右下点的坐标为(770,277)
左上点的坐标为:(711,226),右下点的坐标为(1689,938)
image 1/1 /home/rongxiao/PycharmProjects/yolococo/mask.1.jpg: 384x640 2 persons, 1 airplane, Done. (0.182s)
Speed: 1.1ms pre-process, 181.7ms inference, 1.0ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp15

附参考:yolov5输出检测到的目标坐标信息(旧版本)

找到detect.py,在大概113行,找到plot_one_box

                # 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
                        with open(txt_path + '.txt', 'a') as f:
                            f.write(('%g ' * 5 + '\n') % (cls, *xywh))  # label format

                    if save_img or view_img:  # Add bbox to image
                        label = '%s %.2f' % (names[int(cls)], conf)
                        plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

ctr+鼠标点击,进入general.py,并自动定位到plot_one_box函数,修改函数为

def plot_one_box(x, img, color=None, label=None, line_thickness=None):
   # Plots one bounding box on image img
   tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
   color = color or [random.randint(0, 255) for _ in range(3)]
   c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
   cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
   print("左上点的坐标为:(" + str(c1[0]) + "," + str(c1[1]) + "),右下点的坐标为(" + str(c2[0]) + "," + str(c2[1]) + ")")

即可输出目标坐标信息了

yolov5返回坐标的方法实例

来源:https://blog.csdn.net/weixin_44726793/article/details/122022467

标签:yolov5,坐标,输出
0
投稿

猜你喜欢

  • Oracle11.2.0.1如何升级到11.2.0.3 Oracle同版本升级

    2023-06-25 15:28:11
  • 一文带你掌握Go语言中文件的写入操作

    2024-02-07 13:23:54
  • 如何使用PyCharm及常用配置详解

    2021-09-22 14:59:47
  • Golang如何编写内存高效及CPU调优的Go结构体

    2024-04-23 09:45:55
  • SQL Server 中死锁产生的原因及解决办法

    2008-11-25 11:50:00
  • python切片中内存的注意事项总结

    2022-12-23 00:04:09
  • Ubuntu 18.04安装mysql 5.7.23

    2024-01-28 19:44:13
  • python pygame实现滚动横版射击游戏城市之战

    2021-07-11 00:32:51
  • VSCode 最全实用插件小结

    2022-12-11 17:03:47
  • Python中Django与Echarts的结合用法图文详解

    2022-02-09 23:10:29
  • Python PCA降维的两种实现方法

    2022-03-05 04:25:47
  • matplotlib绘制多子图共享鼠标光标的方法示例

    2023-06-15 13:45:19
  • 详解Python中的分组函数groupby和itertools)

    2022-08-30 15:03:18
  • 在Django中URL正则表达式匹配的方法

    2021-06-09 22:38:57
  • web服务器程序运行出现乱码问题的解决方法

    2023-02-26 14:46:48
  • 详解SQL中Group By的用法

    2024-01-28 14:01:32
  • centos上安装mysql并设置远程访问的操作方法

    2024-01-14 15:24:28
  • python异步Web框架sanic的实现

    2021-01-17 01:37:57
  • python掌握字符串只需这一篇就够了

    2023-01-09 03:39:10
  • 自己用python做的一款超炫酷音乐播放器

    2021-05-26 04:47:35
  • asp之家 网络编程 m.aspxhome.com