YOLOv8底层源代码修改解决中文标签分类显示问题,主要解决训练完成之后验证集结果图片中文显示和模型预测图片中的中文显示问题。
1、metrics文件修改如下:
2、plotting文件修改如下:
plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False
class Annotator:下的init注解调,然后修订成下面的方法,其中font的路径修改成自己下载的字体路径
def __init__(self, im, line_width=None, font_size=None, font='D:/python310/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf', pil=False, example='abc'): assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.' self.pil = pil or not is_ascii(example) or is_chinese(example) if self.pil: # use PIL self.im = im if isinstance(im, Image.Image) else Image.fromarray(im) self.draw = ImageDraw.Draw(self.im) # self.font = check_font(font='D:/python310/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf' if is_chinese(example) else font) # size = font_size or max(round(sum(self.im.size) / 2 * 0.035), 12) self.font = check_pil_font( font='D:/python310/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf' if is_chinese( example) else font, size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
3、checks文件修改:
添加内容
def check_pil_font(font='Arial.ttf',size=10): # Return a PIL TrueType Font, downloading to CONFIG_DIR if necessary font = Path(font) font = font if font.exists() else (USER_CONFIG_DIR / font.name) try: return ImageFont.truetype(str(font) if font.exists() else font.name, size) except Exception: # download if missing check_font(font) try: return ImageFont.truetype(str(font), size) except TypeError: check_requirements('Pillow>=8.4.0') #
字体先确保已下载完毕,不然还是不起作用,本次windows10整体运行完毕结果正常显示。