ultralytics Yolov3显示中文标签

  源码地址:https://github.com/ultralytics/yolov3

  感谢帮助:https://home.cnblogs.com/u/haheihei/

  网上的教程多是设置标签图片,识别之后再替换,然而该yolov3框架已经给你写好了方法,调用就好。

  

 

   预测文件是detect.py,但我们会发现它调用的是plot_one_box:

   那么这里修改成plot_one_box_PIL方法之后,再按照yaml内类名加个json字典映射,就可以在显示的时候换成中文标签:

   对应代码修改在这里:

   这里使用im0是因为plot_one_box_PIL返回一个image对象:

def plot_one_box_PIL(box, img, color=None, label=None, line_thickness=None):
    img = Image.fromarray(img)
    draw = ImageDraw.Draw(img)
    line_thickness = line_thickness or max(int(min(img.size) / 200), 2)
    draw.rectangle(box, width=line_thickness, outline=tuple(color))  # plot
    if label:
        fontsize = max(round(max(img.size) / 40), 12)
        font = ImageFont.truetype("font/myFont.ttf", fontsize)
        txt_width, txt_height = font.getsize(label)
        draw.rectangle([box[0], box[1] - txt_height + 4, box[0] + txt_width, box[1]], fill=tuple(color))
        draw.text((box[0], box[1] - txt_height + 1), label, fill=(255, 255, 255), font=font)
    return np.asarray(img)

  font那里找一个字体文件下载过来,调用就行,不只局限于ttf。

原文地址:https://www.cnblogs.com/20183711PYD/p/14743813.html