未验证 提交 6504ef76 编写于 作者: T Tingquan Gao 提交者: GitHub

support to chinese in draw_bbox (#8581)

上级 249de9bd
import PIL
def imagedraw_textsize_c(draw, text):
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
......
......@@ -296,7 +296,7 @@ def get_path(url, root_dir, md5sum=None, check_exist=True):
# new weights format which postfix is 'pdparams' not
# need to decompress
if osp.splitext(fullname)[-1] not in ['.pdparams', '.yml']:
if osp.splitext(fullname)[-1] not in ['.pdparams', '.yml', '.ttf']:
_decompress_dist(fullname)
return fullpath, False
......
......@@ -17,14 +17,16 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import numpy as np
from PIL import Image, ImageDraw
from PIL import Image, ImageDraw, ImageFont
import cv2
import math
from .colormap import colormap
from ppdet.utils.logger import setup_logger
from ppdet.utils.compact import imagedraw_textsize_c
from ppdet.utils.download import get_path
logger = setup_logger(__name__)
__all__ = ['visualize_results']
......@@ -86,6 +88,11 @@ def draw_bbox(image, im_id, catid2name, bboxes, threshold):
"""
Draw bbox on 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 = ImageDraw.Draw(image)
catid2color = {}
......@@ -126,10 +133,10 @@ def draw_bbox(image, im_id, catid2name, bboxes, threshold):
# draw label
text = "{} {:.2f}".format(catid2name[catid], 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 image
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册