网络编程
位置:首页>> 网络编程>> Python编程>> yolov5返回坐标的方法实例

yolov5返回坐标的方法实例

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

标签:yolov5,坐标,输出

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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com