From dadb4007caa07b73c7c1f4caba21a7655a147e10 Mon Sep 17 00:00:00 2001 From: zhoushuliang Date: Thu, 10 Jul 2025 17:27:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20util/PlotsUtils.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/PlotsUtils.py | 80 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/util/PlotsUtils.py b/util/PlotsUtils.py index ddbbce3..372b4a8 100644 --- a/util/PlotsUtils.py +++ b/util/PlotsUtils.py @@ -67,6 +67,12 @@ def xywh2xyxy2(param): # return [(lt, yt), (rt, yt), (rt, yr), (lt, yr)] return np.asarray(param[0][0:4], np.int32), float(param[1]), int(param[2]) +def xy2xyxy(box): + if not isinstance(box[0], (list, tuple, np.ndarray)): + x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3]) + # 顺时针 + box = [(x1, y1), (x2, y1), (x2, y2), (x1, y2)] + return box def draw_painting_joint(box, img, label_array, score=0.5, color=None, config=None, isNew=False): # 识别问题描述图片的高、宽 @@ -218,6 +224,11 @@ def draw_name_joint(box, img, label_array_dict, score=0.5, color=None, config=No cv2.putText(img, label, p3, 0, config[3], [225, 255, 255], thickness=config[4], lineType=cv2.LINE_AA) return img, box +def draw_name_ocr(box, img, color, label, line_thickness=2, outfontsize=40): + font = ImageFont.truetype(FONT_PATH, outfontsize, encoding='utf-8') + #(color=None, label=None, font=None, fontSize=40, unify=False) + label_zh = get_label_array(color, label, font, outfontsize) + return plot_one_box_auto(box, img, color, line_thickness, label_zh) def filterBox(det0, det1, pix_dis): # det0为 (m1, 11) 矩阵 @@ -255,4 +266,71 @@ def filterBox(det0, det1, pix_dis): # 类别相同 & 中心点在上一帧的框内 判断为True res = np.sum(mask, axis=1) det0_copy[..., -1] = res - return det0_copy \ No newline at end of file + return det0_copy + +def plot_one_box_auto(box, img, color=None, line_thickness=2, label_array=None): + # print("省略 :%s, box:%s"%('+++' * 10, box)) + # 识别问题描述图片的高、宽 + lh, lw = label_array.shape[0:2] + # print("省略 :%s, lh:%s, lw:%s"%('+++' * 10, lh, lw)) + # 图片的长度和宽度 + imh, imw = img.shape[0:2] + box = xy2xyxy(box) + # 框框左上的位置 + x0, y1 = box[0][0], box[0][1] + # print("省略 :%s, x0:%s, y1:%s"%('+++' * 10, x0, y1)) + x1, y0 = x0 + lw, y1 - lh + # 如果y0小于0, 说明超过上边框 + if y0 < 0: + y0 = 0 + # y1等于文字高度 + y1 = y0 + lh + # 如果y1框框的高大于图片高度 + if y1 > imh: + # y1等于图片高度 + y1 = imh + # y0等于y1减去文字高度 + y0 = y1 - lh + # 如果x0小于0 + if x0 < 0: + x0 = 0 + x1 = x0 + lw + if x1 > imw: + x1 = imw + x0 = x1 - lw + # box_tl = max(int(round(imw / 1920 * 3)), 1) or round(0.002 * (imh + imw) / 2) + 1 + ''' + 1. img(array) 为ndarray类型(可以为cv.imread)直接读取的数据 + 2. box(array):为所画多边形的顶点坐标 + 3. 所画四边形是否闭合,通常为True + 4. color(tuple):BGR三个通道的值 + 5. thickness(int):画线的粗细 + 6. shift:顶点坐标中小数的位数 + ''' + # Plots one bounding box on image img + tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness + box1 = np.asarray(box, np.int32) + cv2.polylines(img, [box1], True, color, tl) + img[y0:y1, x0:x1, :] = label_array + + return img, box + + +def draw_name_crowd(dets, img, color, label, line_thickness=2, outfontsize=20): + font = ImageFont.truetype(FONT_PATH, outfontsize, encoding='utf-8') + H,W = img.shape[:2] + # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + # img = Image.fromarray(img) + # width, height = img.size + Wrate = W // 128 * 128/W + Hrate = H // 128 * 128/H + + # img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) + + for p in dets: + img = cv2.circle(img, (int(p[0]/Wrate), int(p[1]/Hrate)), line_thickness, color, -1) + Calc_label_arr = get_label_array(color, label, font, outfontsize) + lh, lw = Calc_label_arr.shape[0:2] + img[0:lh, 0:lw, :] = Calc_label_arr + + return img, dets \ No newline at end of file