【YOLOv5-6.x】修改标签框的位置

找到detect.py中的 “annotator.box_label(xyxy, label, color=colors(class_sub, True))”

annotator.box_label(xyxy, label, color=colors(cls, True))  # 区域内画框、标签

按住CTRL,左击box_label,导航到plots.py下的box_label
以下代码与YOLOv5有所更改

    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]))#左顶,右底

            offset = p2[1]-p1[1]

            cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)#画矩形框
            if label:
                tf = max(self.lw - 1, 1)  # font thickness
                w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0]  # text width, height
                outside = p1[1] - h - 3 >= 0  # label fits outside box
                p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
                offset = offset + (p1[1]-p2[1])
                cv2.rectangle(self.im, (p1[0],p1[1]+offset), (p2[0],p2[1]+offset), color, -1, cv2.LINE_AA)  # filled#填充label,底色
                cv2.putText(self.im, label, (p1[0], p1[1] - 2 + offset if outside else p1[1] + h + 2 + offset), 0, self.lw / 3, txt_color,#label文字
                            thickness=tf, lineType=cv2.LINE_AA)

【YOLOv5-6.x】修改标签框的位置_第1张图片

如果喜欢博主的话,那就点赞吧q(≧▽≦q)!!!!

你可能感兴趣的:(计算机视觉,python,人工智能)