YOLOX系列三 --图片标签显示中文

YOLOX系列三

图片标签显示中文



前言

实际项目应用中,图片的标签还是以显示中文为主,本文主要内容就是对YOLOX进行修改来进行标签中文显示。

一、类别标签中文设置

首先将类别便签修改为中文。
文件名称为YOLOX-main\tools\yolox\data\datasets\coco_classes.py。
将其中的COCO_CLASSES的标签修改为对应的中文,如下所示。
(摊牌了,我懒了,没有全部修改…)

COCO_CLASSES = (
    "人",
    "自行车",
    "汽车",
    "motorcycle",
    "飞机",
    "公共汽车",
    "火车",
    "货车",
    "船",
    "交通信号灯",
    "fire hydrant",
    "停止标志",
    "parking meter",
    "bench",
    "鸟",
    "猫",
    "狗",
    "马",
    "羊",
    "奶牛",
    "大象",
    "熊",
    "zebra",
    "giraffe",
    "backpack",
    "umbrella",
    "handbag",
    "tie",
    "suitcase",
    "frisbee",
    "skis",
    "snowboard",
    "sports ball",
    "kite",
    "baseball bat",
    "baseball glove",
    "skateboard",
    "surfboard",
    "tennis racket",
    "瓶子",
    "wine glass",
    "cup",
    "fork",
    "knife",
    "spoon",
    "bowl",
    "banana",
    "apple",
    "sandwich",
    "orange",
    "broccoli",
    "carrot",
    "热狗",
    "披萨",
    "donut",
    "cake",
    "chair",
    "couch",
    "potted plant",
    "bed",
    "dining table",
    "toilet",
    "tv",
    "laptop",
    "mouse",
    "remote",
    "keyboard",
    "cell phone",
    "microwave",
    "oven",
    "toaster",
    "sink",
    "refrigerator",
    "book",
    "clock",
    "vase",
    "scissors",
    "teddy bear",
    "hair drier",
    "toothbrush",
)

二、字体设置

首先进行汉字字体设置,将需要的汉字字体格式放到目录中,我放到了与tools目录下。
在这里插入图片描述
linux 字体*.TTC的存放路径一般是: /usr/share/fonts/opentype/noto/
查找指令locate *.ttc (应该是,好久没用Linux了)
windows 字体 *.TTF 存放路径: C:\Windows\Fonts
查找命令 win+R+fonts

三、代码修改

最后一步就是对项目代码进行修改。
首先安装pillow依赖包(有的话就算了)。pip install pillow
修改文件tools\yolox\utils\visualize.py。将vis()函数修改如下所示:

import cv2
import numpy
import numpy as np
from PIL import Image, ImageDraw, ImageFont

__all__ = ["vis"]


def vis(img, boxes, scores, cls_ids, conf=0.5, class_names=None):

    for i in range(len(boxes)):                                                          # 遍历检测结果
        box = boxes[i]
        cls_id = int(cls_ids[i])
        score = scores[i]
        if score < conf:
            continue
        x0 = int(box[0])
        y0 = int(box[1])
        x1 = int(box[2])
        y1 = int(box[3])

        color = (_COLORS[cls_id] * 255).astype(np.uint8).tolist()                            # 框颜色设置
        text = '{}:{:.1f}%'.format(class_names[cls_id], score * 100)                         # 文本格式
        txt_color = (0, 0, 0) if np.mean(_COLORS[cls_id]) > 0.5 else (255, 255, 255)         # 文本颜色设置
        font = cv2.FONT_HERSHEY_SIMPLEX                                                      # 字体格式

        txt_size = cv2.getTextSize(text, font, 0.4, 1)[0]
        cv2.rectangle(img, (x0, y0), (x1, y1), color, 2)                                     # 画框

        txt_bk_color = (_COLORS[cls_id] * 255 * 0.7).astype(np.uint8).tolist()
        cv2.rectangle(
            img,
            (x0, y0 + 1),
            (x0 + txt_size[0] + 1, y0 + int(1.5*txt_size[1])),
            txt_bk_color,
            -1
        )

        # 打印汉字
        img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

        # 字体  linux 字体*.ttc的存放路径一般是: /usr/share/fonts/opentype/noto/ 查找指令locate *.ttc
        #      win    win+R+fonts  C:\Windows\Fonts
        font = ImageFont.truetype('STFANGSO.TTF', 10, encoding="utf-8")

        draw = ImageDraw.Draw(img_pil)
        draw.text((x0, y0), text, txt_color, font=font)

        # 转换回OpenCV格式
        img = cv2.cvtColor(numpy.asarray(img_pil), cv2.COLOR_RGB2BGR)

        # cv2.putText(img, text, (x0, y0 + txt_size[1]), font, 0.4, txt_color, thickness=1)    # 图片上打印文字

    return img

接下来测试一下效果。
在这里插入图片描述
效果还不错。


总结

本文主要内容就是将YOLOX项目的图片标签进行中文显示。
若发现文章有误,欢迎指出。
各位老铁点点关注点点赞啊

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘丶小歪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值