import os
import json
import xml.etree.ElementTree as ET
import cv2
import numpy as np
# 假设你的分割模型和配置
from paddleseg import PaddleSeg
# 分割模型初始化
model = PaddleSeg(config='path_to_config', model_path='path_to_model')
def seg_img(img):
# 运行分割模型
pred = model(img) # 根据你的模型进行适当调整
# 假设 pred 是分割后的结果,且pred是一个与输入图像同大小的数组,其中0, 1, 2分别表示背景,帽子,手套
# 我们只提取标签为1(帽子)的分割轮廓
pred = (pred == 1).astype(np.uint8)
# 找到分割轮廓
contours, _ = cv2.findContours(pred, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
return []
# 找到面积最大的轮廓
max_contour = max(contours, key=cv2.contourArea)
return max_contour
def expand_box(box, img_width, img_height, expansion_factor=0.15):
# 扩展检测框
xmin, ymin, xmax, ymax = box
width = xmax - xmin
height
10-06
1万+
11-20
20万+
03-12
1547
07-27