Skip to content

Commit

Permalink
support chinese in draw_bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
TingquanGao committed Sep 1, 2023
1 parent 6504ef7 commit d40090e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions deploy/python/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
from PIL import Image, ImageDraw, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

def imagedraw_textsize_c(draw, text):
from ppdet.utils.download import get_path


def imagedraw_textsize_c(draw, text, font=None):
if int(PIL.__version__.split('.')[0]) < 10:
tw, th = draw.textsize(text)
tw, th = draw.textsize(text, font=font)
else:
left, top, right, bottom = draw.textbbox((0, 0), text)
left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
tw, th = right - left, bottom - top

return tw, th
Expand Down Expand Up @@ -134,6 +137,11 @@ def draw_box(im, np_boxes, labels, threshold=0.5):
Returns:
im (PIL.Image.Image): visualized image
"""
font_url = "https://paddledet.bj.bcebos.com/simfang.ttf"
font_path , _ = get_path(font_url, "~/.cache/paddle/")
font_size = 18
font = ImageFont.truetype(font_path, font_size, encoding="utf-8")

draw_thickness = min(im.size) // 320
draw = ImageDraw.Draw(im)
clsid2color = {}
Expand Down Expand Up @@ -169,10 +177,10 @@ def draw_box(im, np_boxes, labels, threshold=0.5):

# draw label
text = "{} {:.4f}".format(labels[clsid], score)
tw, th = imagedraw_textsize_c(draw, text)
tw, th = imagedraw_textsize_c(draw, text, font=font)
draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255), font=font)
return im


Expand Down

0 comments on commit d40090e

Please sign in to comment.