未验证 提交 0da52bfb 编写于 作者: T Tingquan Gao 提交者: GitHub

[cherry-pick] fix_textbbox in ImageDraw (#8580)

* [BugFix] fix_textbbox in ImageDraw

cherry-pick from #8455:
* fix_textbbox
* compact_py37
* update_req

* support to chinese in draw_bbox

---------
Co-authored-by: Nshiyutang <34859558+shiyutang@users.noreply.github.com>
上级 c48d6bfb
...@@ -20,6 +20,8 @@ import numpy as np ...@@ -20,6 +20,8 @@ import numpy as np
from PIL import Image, ImageDraw, ImageFile from PIL import Image, ImageDraw, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True ImageFile.LOAD_TRUNCATED_IMAGES = True
from collections import deque from collections import deque
from ppdet.utils.compact import imagedraw_textsize_c
def visualize_box_mask(im, results, labels, threshold=0.5): def visualize_box_mask(im, results, labels, threshold=0.5):
...@@ -109,7 +111,7 @@ def draw_box(im, np_boxes, labels, threshold=0.5): ...@@ -109,7 +111,7 @@ def draw_box(im, np_boxes, labels, threshold=0.5):
# draw label # draw label
text = "{} {:.4f}".format(labels[clsid], score) text = "{} {:.4f}".format(labels[clsid], score)
tw, th = draw.textsize(text) tw, th = imagedraw_textsize_c(draw, text)
draw.rectangle( draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) [(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))
......
...@@ -20,6 +20,7 @@ import numpy as np ...@@ -20,6 +20,7 @@ import numpy as np
from PIL import Image, ImageDraw, ImageFile from PIL import Image, ImageDraw, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True ImageFile.LOAD_TRUNCATED_IMAGES = True
import math import math
from ppdet.utils.compact import imagedraw_textsize_c
def visualize_box_mask(im, results, labels, threshold=0.5): def visualize_box_mask(im, results, labels, threshold=0.5):
...@@ -159,7 +160,7 @@ def draw_box(im, np_boxes, labels, threshold=0.5): ...@@ -159,7 +160,7 @@ def draw_box(im, np_boxes, labels, threshold=0.5):
# draw label # draw label
text = "{} {:.4f}".format(labels[clsid], score) text = "{} {:.4f}".format(labels[clsid], score)
tw, th = draw.textsize(text) tw, th = imagedraw_textsize_c(draw, text)
draw.rectangle( draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) [(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))
...@@ -497,7 +498,7 @@ def draw_press_box_lanes(im, np_boxes, labels, threshold=0.5): ...@@ -497,7 +498,7 @@ def draw_press_box_lanes(im, np_boxes, labels, threshold=0.5):
# draw label # draw label
text = "{}".format(labels[clsid]) text = "{}".format(labels[clsid])
tw, th = draw.textsize(text) tw, th = imagedraw_textsize_c(draw, text)
draw.rectangle( draw.rectangle(
[(xmin + 1, ymax - th), (xmin + tw + 1, ymax)], fill=color) [(xmin + 1, ymax - th), (xmin + tw + 1, ymax)], fill=color)
draw.text((xmin + 1, ymax - th), text, fill=(0, 0, 255)) draw.text((xmin + 1, ymax - th), text, fill=(0, 0, 255))
...@@ -570,7 +571,7 @@ def visualize_vehicle_retrograde(im, mot_res, vehicle_retrograde_res): ...@@ -570,7 +571,7 @@ def visualize_vehicle_retrograde(im, mot_res, vehicle_retrograde_res):
# draw label # draw label
text = "retrograde" text = "retrograde"
tw, th = draw.textsize(text) tw, th = imagedraw_textsize_c(draw, text)
draw.rectangle( draw.rectangle(
[(xmax + 1, ymin - th), (xmax + tw + 1, ymin)], [(xmax + 1, ymin - th), (xmax + tw + 1, ymin)],
fill=(0, 255, 0)) fill=(0, 255, 0))
......
...@@ -51,6 +51,8 @@ from .op_helper import (satisfy_sample_constraint, filter_and_process, ...@@ -51,6 +51,8 @@ from .op_helper import (satisfy_sample_constraint, filter_and_process,
is_poly, get_border) is_poly, get_border)
from ppdet.utils.logger import setup_logger from ppdet.utils.logger import setup_logger
from ppdet.utils.compact import imagedraw_textsize_c
from ppdet.modeling.keypoint_utils import get_affine_transform, affine_transform from ppdet.modeling.keypoint_utils import get_affine_transform, affine_transform
logger = setup_logger(__name__) logger = setup_logger(__name__)
...@@ -2212,7 +2214,7 @@ class DebugVisibleImage(BaseOperator): ...@@ -2212,7 +2214,7 @@ class DebugVisibleImage(BaseOperator):
fill='green') fill='green')
# draw label # draw label
text = str(gt_class[i][0]) text = str(gt_class[i][0])
tw, th = draw.textsize(text) tw, th = imagedraw_textsize_c(draw, text)
draw.rectangle( draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green') [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green')
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
......
...@@ -31,6 +31,7 @@ import copy ...@@ -31,6 +31,7 @@ import copy
from .operators import register_op, BaseOperator from .operators import register_op, BaseOperator
from ppdet.modeling.rbox_utils import poly2rbox_le135_np, poly2rbox_oc_np, rbox2poly_np from ppdet.modeling.rbox_utils import poly2rbox_le135_np, poly2rbox_oc_np, rbox2poly_np
from ppdet.utils.logger import setup_logger from ppdet.utils.logger import setup_logger
from ppdet.utils.compact import imagedraw_textsize_c
logger = setup_logger(__name__) logger = setup_logger(__name__)
...@@ -433,7 +434,7 @@ class VisibleRBox(BaseOperator): ...@@ -433,7 +434,7 @@ class VisibleRBox(BaseOperator):
xmin = min(x1, x2, x3, x4) xmin = min(x1, x2, x3, x4)
ymin = min(y1, y2, y3, y4) ymin = min(y1, y2, y3, y4)
text = str(gt_class[i][0]) text = str(gt_class[i][0])
tw, th = draw.textsize(text) tw, th = imagedraw_textsize_c(draw, text)
draw.rectangle( draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green') [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green')
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
......
import PIL
def imagedraw_textsize_c(draw, text, font=None):
if int(PIL.__version__.split('.')[0]) < 10:
tw, th = draw.textsize(text, font=font)
else:
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): ...@@ -296,7 +296,7 @@ def get_path(url, root_dir, md5sum=None, check_exist=True):
# new weights format which postfix is 'pdparams' not # new weights format which postfix is 'pdparams' not
# need to decompress # 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) _decompress_dist(fullname)
return fullpath, False return fullpath, False
......
...@@ -17,13 +17,16 @@ from __future__ import division ...@@ -17,13 +17,16 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import os
import numpy as np import numpy as np
from PIL import Image, ImageDraw from PIL import Image, ImageDraw, ImageFont
import cv2 import cv2
import math import math
from .colormap import colormap from .colormap import colormap
from ppdet.utils.logger import setup_logger 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__) logger = setup_logger(__name__)
__all__ = ['visualize_results'] __all__ = ['visualize_results']
...@@ -85,6 +88,11 @@ def draw_bbox(image, im_id, catid2name, bboxes, threshold): ...@@ -85,6 +88,11 @@ def draw_bbox(image, im_id, catid2name, bboxes, threshold):
""" """
Draw bbox on image 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) draw = ImageDraw.Draw(image)
catid2color = {} catid2color = {}
...@@ -125,10 +133,10 @@ def draw_bbox(image, im_id, catid2name, bboxes, threshold): ...@@ -125,10 +133,10 @@ def draw_bbox(image, im_id, catid2name, bboxes, threshold):
# draw label # draw label
text = "{} {:.2f}".format(catid2name[catid], score) text = "{} {:.2f}".format(catid2name[catid], score)
tw, th = draw.textsize(text) tw, th = imagedraw_textsize_c(draw, text, font=font)
draw.rectangle( draw.rectangle(
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) [(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 return image
......
...@@ -10,7 +10,7 @@ terminaltables ...@@ -10,7 +10,7 @@ terminaltables
Cython Cython
pycocotools pycocotools
setuptools setuptools
Pillow <= 9.5.0 Pillow
# for MOT evaluation and inference # for MOT evaluation and inference
lap lap
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册