更新 util/PlotsUtils.py

This commit is contained in:
zhoushuliang 2025-07-10 17:27:00 +08:00
parent 5cc22405a4
commit dadb4007ca
1 changed files with 79 additions and 1 deletions

View File

@ -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
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. imgarray 为ndarray类型可以为cv.imread直接读取的数据
2. boxarray为所画多边形的顶点坐标
3. 所画四边形是否闭合通常为True
4. colortupleBGR三个通道的值
5. thicknessint画线的粗细
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