Add Chinese Character to Your YOLOv3 Project


source code

This link behind shows how to implement YOLOv3 with pytorch instructions In my tutorial, I give a solution to convert Label to Chinese Character.

4 Steps To Convert Label to Chinese

1. Change coconame

First of all, we need to translate english label to chinese label translated labels is availiable here


2. Use FreeType to write a script to draw chinese characters


Because Opencv doesn't support chinese character, we need to use a known font style to draw chinese character, it contains 3 parts:

  1. draw_ft_bitmap(self, img, bitmap, pen, color)
    draw each char
        :param bitmap: bitmap
        :param pen:    pen
        :param color:  pen color e.g.(0,0,255) - red
        :return:       image
    
  2. draw_string(self, img, x_pos, y_pos, text, color)
    draw string
        :param x_pos: text x-postion on img
        :param y_pos: text y-postion on img
        :param text:  text (unicode)
        :param color: text color
        :return:      image
    
  3. draw_text(self, image, pos, text, text_size, text_color)
    (draw chinese(or not) text with ttf
        :param image:     image(numpy.ndarray) to draw text
        :param pos:       where to draw text
        :param text:      the context, for chinese should be unicode type
        :param text_size: text size
        :param text_color:text color
        :return:          image
    

3. Download msyh.ttf

.ttf file is dependency to specify font style, it would be loaded in FreeType script, click here to Download

4. Apply ft2 Chinese Character to yolo

In main detect flow (cam_detect.py), instead of using cv2.imshow(), we use ft.draw_text()

comment cv2.putText part, use ft.draw_text instead

    # c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
    # cv2.rectangle(img, c1, c2,color, -1)
    # cv2.rectangle(img, c1, c2, color, -1)
    # cv2.putText(img, label, (c1[0], c1[1] + t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 2, [225,255,255], 1);
    ft = ft2.put_chinese_text('msyh.ttf')
    ft.draw_text(image=img, pos=(c1[0], c1[1] + t_size[1] - 7), text=label, text_size=15, text_color=[255, 255, 255])

Before Translate


After Translate


You can Download All source code from my github,here


Hanqing Guo

Graduate Assistant

Welcome to my personal page! I am a Phd student in Michigan State University, eLANs Lab