yolov7使用中文标签,matplotlib如何正常打印中文

1. 获取本地中文字体

首先检查本地是否存在可用的中文字体:

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

# 列出所有可用的字体
for font in fm.findSystemFonts(fontpaths=None, fontext='ttf'):
    print(font)
    # 获取font的名称
    font_name = fm.FontProperties(fname=font).get_name()
    plt.rcParams['font.sans-serif'] = [font_name]
    plt.rcParams['axes.unicode_minus'] = False

    try:
        fig, ax = plt.subplots()
        ax.set_ylabel('中文')
        plt.show()
        # print(f"{font} 支持中文")
    except UnicodeEncodeError:
        print(f"{font} 不支持中文")

遍历本地所有字体,并画图测试中文打印效果。看图片左侧是否正常显示"中文"二字,不支持的会显示方框或空白。例如:
在这里插入图片描述
记录下支持中文的字体路径。

2. 使yolov7中test.py的结果显示中文

utils/metrics.py中,增加如下代码:

import matplotlib.font_manager as fm
font = '/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc'  # 中文字体路径
font_name = fm.FontProperties(fname=font).get_name()
plt.rcParams['font.sans-serif'] = [font_name]
plt.rcParams['axes.unicode_minus'] = False

3. 使yolov7中detect.py的结果显示中文

utils/plots.py中,修改plot_one_box()函数:


def plot_one_box(x, img, color=None, label=None, line_thickness=3):
    # 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)
    if label:
        tf = max(tl - 1, 1)  # font thickness
        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3

        if all(ord(c) < 128 for c in str(label)):  # 英文
            cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
            cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)  
        else:  # 中文
            # if isinstance(img, np.ndarray):  # 判断是否OpenCV图片类型
            img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

            draw = ImageDraw.Draw(img_pil)
            font = ImageFont.truetype("zh.ttf", 20)  # 使用正确的字体文件路径和大小

            w, h = font.getsize(label)  # text width, height
            outside = c1[1] - h >= 0  # label fits outside box
            draw.rectangle(
                (c1[0], c1[1] - h if outside else c1[1], c1[0] + w + 1, c1[1] + 1 if outside else c1[1] + h + 1),
                fill=tuple(color[::-1]),
            )
            draw.text((c1[0], c1[1] - h if outside else c1[1]), label, (225, 255, 255), font=font)
            img = cv2.cvtColor(np.asarray(img_pil), cv2.COLOR_RGB2BGR)
    return img

如此,检测结果图片中可正常显示中文。
在这里插入图片描述

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值