From 484510a957aff9ba87a477e9d19a3ea1ee85669f Mon Sep 17 00:00:00 2001 From: shiyutang <34859558+shiyutang@users.noreply.github.com> Date: Thu, 20 Jul 2023 19:41:22 +0800 Subject: [PATCH] [BugFix] fix_textbbox in ImageDraw (#8455) * fix_textbbox * compact_py37 * update_req --- deploy/pptracking/python/mot/visualize.py | 5 +++-- deploy/python/visualize.py | 10 ++++------ ppdet/data/transform/operators.py | 5 +++-- ppdet/data/transform/rotated_operators.py | 4 ++-- ppdet/utils/compact.py | 11 +++++++++++ ppdet/utils/visualizer.py | 4 ++-- requirements.txt | 2 +- 7 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 ppdet/utils/compact.py diff --git a/deploy/pptracking/python/mot/visualize.py b/deploy/pptracking/python/mot/visualize.py index 8188b056e..e60b73280 100644 --- a/deploy/pptracking/python/mot/visualize.py +++ b/deploy/pptracking/python/mot/visualize.py @@ -20,6 +20,8 @@ import numpy as np from PIL import Image, ImageDraw, ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True from collections import deque +from ppdet.utils.compact import imagedraw_textsize_c + def visualize_box_mask(im, results, labels, threshold=0.5): @@ -109,8 +111,7 @@ def draw_box(im, np_boxes, labels, threshold=0.5): # draw label text = "{} {:.4f}".format(labels[clsid], score) - left, top, right, bottom = draw.textbbox(text) - tw, th = right - left, bottom - top + tw, th = imagedraw_textsize_c(draw, text) draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) diff --git a/deploy/python/visualize.py b/deploy/python/visualize.py index 239ea01d8..51e01b9d9 100644 --- a/deploy/python/visualize.py +++ b/deploy/python/visualize.py @@ -20,6 +20,7 @@ import numpy as np from PIL import Image, ImageDraw, ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True import math +from ppdet.utils.compact import imagedraw_textsize_c def visualize_box_mask(im, results, labels, threshold=0.5): @@ -159,8 +160,7 @@ def draw_box(im, np_boxes, labels, threshold=0.5): # draw label text = "{} {:.4f}".format(labels[clsid], score) - left, top, right, bottom = draw.textbbox(text) - tw, th = right - left, bottom - top + tw, th = imagedraw_textsize_c(draw, text) draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) @@ -498,8 +498,7 @@ def draw_press_box_lanes(im, np_boxes, labels, threshold=0.5): # draw label text = "{}".format(labels[clsid]) - left, top, right, bottom = draw.textbbox(text) - tw, th = right - left, bottom - top + tw, th = imagedraw_textsize_c(draw, text) draw.rectangle( [(xmin + 1, ymax - th), (xmin + tw + 1, ymax)], fill=color) draw.text((xmin + 1, ymax - th), text, fill=(0, 0, 255)) @@ -572,8 +571,7 @@ def visualize_vehicle_retrograde(im, mot_res, vehicle_retrograde_res): # draw label text = "retrograde" - left, top, right, bottom = draw.textbbox(text) - tw, th = right - left, bottom - top + tw, th = imagedraw_textsize_c(draw, text) draw.rectangle( [(xmax + 1, ymin - th), (xmax + tw + 1, ymin)], fill=(0, 255, 0)) diff --git a/ppdet/data/transform/operators.py b/ppdet/data/transform/operators.py index e9e47d0de..2e2e320e6 100644 --- a/ppdet/data/transform/operators.py +++ b/ppdet/data/transform/operators.py @@ -51,6 +51,8 @@ from .op_helper import (satisfy_sample_constraint, filter_and_process, is_poly, get_border) 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 logger = setup_logger(__name__) @@ -2212,8 +2214,7 @@ class DebugVisibleImage(BaseOperator): fill='green') # draw label text = str(gt_class[i][0]) - left, top, right, bottom = draw.textbbox(text) - tw, th = right - left, bottom - top + tw, th = imagedraw_textsize_c(draw, text) draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green') draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) diff --git a/ppdet/data/transform/rotated_operators.py b/ppdet/data/transform/rotated_operators.py index 175a5499b..5e9cebb51 100644 --- a/ppdet/data/transform/rotated_operators.py +++ b/ppdet/data/transform/rotated_operators.py @@ -31,6 +31,7 @@ import copy from .operators import register_op, BaseOperator from ppdet.modeling.rbox_utils import poly2rbox_le135_np, poly2rbox_oc_np, rbox2poly_np from ppdet.utils.logger import setup_logger +from ppdet.utils.compact import imagedraw_textsize_c logger = setup_logger(__name__) @@ -433,8 +434,7 @@ class VisibleRBox(BaseOperator): xmin = min(x1, x2, x3, x4) ymin = min(y1, y2, y3, y4) text = str(gt_class[i][0]) - left, top, right, bottom = draw.textbbox(text) - tw, th = right - left, bottom - top + tw, th = imagedraw_textsize_c(draw, text) draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green') draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) diff --git a/ppdet/utils/compact.py b/ppdet/utils/compact.py new file mode 100644 index 000000000..b2451f126 --- /dev/null +++ b/ppdet/utils/compact.py @@ -0,0 +1,11 @@ +import PIL + +def imagedraw_textsize_c(draw, text): + if int(PIL.__version__.split('.')[0]) < 10: + tw, th = draw.textsize(text) + else: + left, top, right, bottom = draw.textbbox((0, 0), text) + tw, th = right - left, bottom - top + + return tw, th + \ No newline at end of file diff --git a/ppdet/utils/visualizer.py b/ppdet/utils/visualizer.py index bd3461d89..d9643a341 100644 --- a/ppdet/utils/visualizer.py +++ b/ppdet/utils/visualizer.py @@ -24,6 +24,7 @@ import math from .colormap import colormap from ppdet.utils.logger import setup_logger +from ppdet.utils.compact import imagedraw_textsize_c logger = setup_logger(__name__) __all__ = ['visualize_results'] @@ -125,8 +126,7 @@ def draw_bbox(image, im_id, catid2name, bboxes, threshold): # draw label text = "{} {:.2f}".format(catid2name[catid], score) - left, top, right, bottom = draw.textbbox(text) - tw, th = right - left, bottom - top + tw, th = imagedraw_textsize_c(draw, text) draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255)) diff --git a/requirements.txt b/requirements.txt index da93a6160..e9d69b57c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,7 @@ terminaltables Cython pycocotools setuptools -Pillow>=10.0.0 +Pillow # for MOT evaluation and inference lap -- GitLab