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
如此,检测结果图片中可正常显示中文。