From f6c769263f193da2293cb23f6f19e809ef81ccb9 Mon Sep 17 00:00:00 2001 From: tangshiyu Date: Thu, 20 Jul 2023 16:46:28 +0800 Subject: [PATCH 1/4] fix_textbbox --- deploy/pptracking/python/mot/visualize.py | 2 +- deploy/python/visualize.py | 6 +++--- ppdet/data/transform/operators.py | 2 +- ppdet/data/transform/rotated_operators.py | 2 +- ppdet/utils/visualizer.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/deploy/pptracking/python/mot/visualize.py b/deploy/pptracking/python/mot/visualize.py index 8188b056e32..78aab97deaa 100644 --- a/deploy/pptracking/python/mot/visualize.py +++ b/deploy/pptracking/python/mot/visualize.py @@ -109,7 +109,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) + left, top, right, bottom = draw.textbbox((0, 0), text) tw, th = right - left, bottom - top draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) diff --git a/deploy/python/visualize.py b/deploy/python/visualize.py index 239ea01d83a..a05c46a62cd 100644 --- a/deploy/python/visualize.py +++ b/deploy/python/visualize.py @@ -159,7 +159,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) + left, top, right, bottom = draw.textbbox((0, 0), text) tw, th = right - left, bottom - top draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) @@ -498,7 +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) + left, top, right, bottom = draw.textbbox((0, 0), text) tw, th = right - left, bottom - top draw.rectangle( [(xmin + 1, ymax - th), (xmin + tw + 1, ymax)], fill=color) @@ -572,7 +572,7 @@ def visualize_vehicle_retrograde(im, mot_res, vehicle_retrograde_res): # draw label text = "retrograde" - left, top, right, bottom = draw.textbbox(text) + left, top, right, bottom = draw.textbbox((0, 0), text) tw, th = right - left, bottom - top draw.rectangle( [(xmax + 1, ymin - th), (xmax + tw + 1, ymin)], diff --git a/ppdet/data/transform/operators.py b/ppdet/data/transform/operators.py index e9e47d0de5a..12ab12ab464 100644 --- a/ppdet/data/transform/operators.py +++ b/ppdet/data/transform/operators.py @@ -2212,7 +2212,7 @@ def apply(self, sample, context=None): fill='green') # draw label text = str(gt_class[i][0]) - left, top, right, bottom = draw.textbbox(text) + left, top, right, bottom = draw.textbbox((0, 0), text) tw, th = right - left, bottom - top draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green') diff --git a/ppdet/data/transform/rotated_operators.py b/ppdet/data/transform/rotated_operators.py index 175a5499bb9..81a4df6f74a 100644 --- a/ppdet/data/transform/rotated_operators.py +++ b/ppdet/data/transform/rotated_operators.py @@ -433,7 +433,7 @@ def apply(self, sample, context=None): 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) + left, top, right, bottom = draw.textbbox((0, 0), text) tw, th = right - left, bottom - top draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill='green') diff --git a/ppdet/utils/visualizer.py b/ppdet/utils/visualizer.py index bd3461d8912..4647d9ed63b 100644 --- a/ppdet/utils/visualizer.py +++ b/ppdet/utils/visualizer.py @@ -125,7 +125,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) + left, top, right, bottom = draw.textbbox((0, 0), text) tw, th = right - left, bottom - top draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) From 6a5c2290b7f34be030e72e39c15cdfb0b9641c7a Mon Sep 17 00:00:00 2001 From: tangshiyu Date: Thu, 20 Jul 2023 17:28:11 +0800 Subject: [PATCH 2/4] compact_py37 --- 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 ++-- 6 files changed, 25 insertions(+), 14 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 78aab97deaa..e60b732808f 100644 --- a/deploy/pptracking/python/mot/visualize.py +++ b/deploy/pptracking/python/mot/visualize.py @@ -20,6 +20,8 @@ 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((0, 0), 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 a05c46a62cd..51e01b9d92e 100644 --- a/deploy/python/visualize.py +++ b/deploy/python/visualize.py @@ -20,6 +20,7 @@ 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((0, 0), 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((0, 0), 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((0, 0), 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 12ab12ab464..2e2e320e69d 100644 --- a/ppdet/data/transform/operators.py +++ b/ppdet/data/transform/operators.py @@ -51,6 +51,8 @@ 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 @@ def apply(self, sample, context=None): fill='green') # draw label text = str(gt_class[i][0]) - left, top, right, bottom = draw.textbbox((0, 0), 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 81a4df6f74a..5e9cebb51a6 100644 --- a/ppdet/data/transform/rotated_operators.py +++ b/ppdet/data/transform/rotated_operators.py @@ -31,6 +31,7 @@ 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 @@ def apply(self, sample, context=None): xmin = min(x1, x2, x3, x4) ymin = min(y1, y2, y3, y4) text = str(gt_class[i][0]) - left, top, right, bottom = draw.textbbox((0, 0), 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 00000000000..b2451f126b8 --- /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 4647d9ed63b..d9643a34162 100644 --- a/ppdet/utils/visualizer.py +++ b/ppdet/utils/visualizer.py @@ -24,6 +24,7 @@ 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((0, 0), 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)) From 221207f7ce82695cc6f051c1161c4a6cfd2c8c0e Mon Sep 17 00:00:00 2001 From: tangshiyu Date: Thu, 20 Jul 2023 17:35:08 +0800 Subject: [PATCH 3/4] update_req --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index da93a616019..e9d69b57ce1 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 From c48a19b3aa1a089fa3dabf01adf3b26ef2b41279 Mon Sep 17 00:00:00 2001 From: tangshiyu Date: Fri, 21 Jul 2023 11:52:28 +0800 Subject: [PATCH 4/4] fix_deploy_cannot_find --- deploy/pptracking/python/mot/visualize.py | 10 +++++++++- deploy/python/visualize.py | 13 +++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/deploy/pptracking/python/mot/visualize.py b/deploy/pptracking/python/mot/visualize.py index e60b732808f..19685ae3a89 100644 --- a/deploy/pptracking/python/mot/visualize.py +++ b/deploy/pptracking/python/mot/visualize.py @@ -17,11 +17,19 @@ import os import cv2 import numpy as np +import PIL from PIL import Image, ImageDraw, ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True from collections import deque -from ppdet.utils.compact import imagedraw_textsize_c +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 def visualize_box_mask(im, results, labels, threshold=0.5): diff --git a/deploy/python/visualize.py b/deploy/python/visualize.py index 51e01b9d92e..9e96c29df06 100644 --- a/deploy/python/visualize.py +++ b/deploy/python/visualize.py @@ -16,12 +16,21 @@ import os import cv2 +import math import numpy as np +import PIL from PIL import Image, ImageDraw, ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True -import math -from ppdet.utils.compact import imagedraw_textsize_c +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 + def visualize_box_mask(im, results, labels, threshold=0.5): """