栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【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)

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009736.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号