diff --git a/ppdet/utils/compact.py b/ppdet/utils/compact.py index b2451f126b8e12b7767b723b69b9696c3c1b584f..b2f803b5365763062a758b30befcc0df66f1afb9 100644 --- a/ppdet/utils/compact.py +++ b/ppdet/utils/compact.py @@ -1,11 +1,11 @@ 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 - \ No newline at end of file + diff --git a/ppdet/utils/download.py b/ppdet/utils/download.py index 5f3665fc8d3ea41b9d7605e917d628cc71439177..a7909b83394f8c291e9a277a16fc6947606db58f 100644 --- a/ppdet/utils/download.py +++ b/ppdet/utils/download.py @@ -237,7 +237,7 @@ def create_voc_list(data_dir, devkit_subdir='VOCdevkit'): years = ['2007', '2012'] # NOTE: since using auto download VOC - # dataset, VOC default label list should be used, + # dataset, VOC default label list should be used, # do not generate label_list.txt here. For default # label, see ../data/source/voc.py create_list(devkit_dir, years, data_dir) @@ -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 @@ -411,7 +411,7 @@ def _download_dist(url, path, md5sum=None): must_mkdirs(path) if not osp.exists(fullname): - with open(lock_path, 'w'): # touch + with open(lock_path, 'w'): # touch os.utime(lock_path, None) if rank_id_curr_node == 0: _download(url, path, md5sum) @@ -425,7 +425,7 @@ def _download_dist(url, path, md5sum=None): def _check_exist_file_md5(filename, md5sum, url): - # if md5sum is None, and file to check is weights file, + # if md5sum is None, and file to check is weights file, # read md5um from url and check, else check md5sum directly return _md5check_from_url(filename, url) if md5sum is None \ and filename.endswith('pdparams') \ @@ -525,7 +525,7 @@ def _decompress_dist(fname): # trainer pipeline in order # **change this if you have more elegent methods** if ParallelEnv().current_endpoint in unique_endpoints: - with open(lock_path, 'w'): # touch + with open(lock_path, 'w'): # touch os.utime(lock_path, None) _decompress(fname) os.remove(lock_path) diff --git a/ppdet/utils/simfang.ttf b/ppdet/utils/simfang.ttf new file mode 100644 index 0000000000000000000000000000000000000000..2b59eae4195d1cdbea375503c0cc34d5631cb0f9 Binary files /dev/null and b/ppdet/utils/simfang.ttf differ diff --git a/ppdet/utils/visualizer.py b/ppdet/utils/visualizer.py index d9643a341620f79583821880046a09b96dad92b8..e29a189d6674731c5d6634e98c75ecd40dcde2d7 100644 --- a/ppdet/utils/visualizer.py +++ b/ppdet/utils/visualizer.py @@ -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