diff --git a/deploy/pptracking/python/mot/visualize.py b/deploy/pptracking/python/mot/visualize.py index 8188b056e328ab720442c2c8a9f0a58c5aa7a26b..e60b732808fea40f7c010aafe6780e5b1a7a6908 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 239ea01d83a4eb36b75a036e06fa85bcb3e2c51b..51e01b9d92e1e56013103a4f9fe0c54659ba3c00 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 e9e47d0de5a137ddc6c545c29df119ec7f1b1891..2e2e320e69d23d8f52af5442cd0ab3d28f6a191e 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 175a5499bb9e9ef689458dfa9881568d9f6dedbc..5e9cebb51a6dbc41ae0ae986bc2600580664e0de 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 0000000000000000000000000000000000000000..b2451f126b8e12b7767b723b69b9696c3c1b584f --- /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 bd3461d89128db95a23590a82bee1dc0c4f05c8a..d9643a341620f79583821880046a09b96dad92b8 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 da93a616019e487d34255d11ee2b1af719159dd2..e9d69b57ce1c0d411bb448b2917b284acca788e1 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