Table of Content
1. 字体确认
首先确定需要使用的字体,比如: simhei.tff
2. train.py 修改
1 |
with open(data) as f: |
修改为:
1 |
with open(data, encoding='UTF-8') as f: |
3. test.py 修改
1 |
with open(data) as f: |
修改为:
1 |
with open(data, encoding='UTF-8') as f: |
4. utils/plots.py 修改
找到函数 plot_one_box
,将 if label:
之后修改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if label: tf = max(tl - 1, 1) # font thickness t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0] font_size = t_size[1] font = ImageFont.truetype('SimHei.ttf', font_size) t_size = font.getsize(label) c2 = c1[0] + t_size[0], c1[1] - t_size[1] cv2.rectangle(im, c1, c2, color, -1, cv2.LINE_AA) # filled img_PIL = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img_PIL) draw.text((c1[0], c2[1] - 2), label, fill=(255, 255, 255), font=font) return cv2.cvtColor(np.array(img_PIL), cv2.COLOR_RGB2BGR) |
找到 plot_images
,将:
1 |
plot_one_box(box, mosaic, label=label, color=color, line_thickness=tl) |
修改为:
1 |
mosaic = plot_one_box(box, mosaic, label=label, color=color, line_thickness=tl) |
5. detect.py 修改
找到:
1 |
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3) |
修改为:
1 |
im0 = plot_one_box(xyxy,im0, label=label, color=colors(c, True), line_thickness=3) |
Done, enjoy.