IoU计算与读取Xml

  • Post author:
  • Post category:其他




IoU计算

def calc_iou(box1, box2):
    x1min, y1min, x1max, y1max = box1[0], box1[1], box1[2], box1[3]
    x2min, y2min, x2max, y2max = box2[0], box2[1], box2[2], box2[3]
    # 计算两个框的面积
    s1 = (y1max - y1min + 1.) * (x1max - x1min + 1.)
    s2 = (y2max - y2min + 1.) * (x2max - x2min + 1.)

    # 计算相交部分的坐标
    xmin = max(x1min, x2min)
    ymin = max(y1min, y2min)
    xmax = min(x1max, x2max)
    ymax = min(y1max, y2max)

    inter_h = max(ymax - ymin + 1, 0)
    inter_w = max(xmax - xmin + 1, 0)

    intersection = inter_h * inter_w
    union = s1 + s2 - intersection

    # 计算iou
    iou = intersection / union
    return iou



读取xml

def readXml(filename):
    tree = ET.parse(filename)
    objects = []    # 会有多个物体,所以list
    for obj in tree.findall('object'):
        obj_struct = {}
        obj_struct['name'] = obj.find('name').text
        bbox = obj.find('bndbox')
        obj_struct['bbox'] = [int(float(bbox.find('xmin').text)),
                              int(float(bbox.find('ymin').text)),
                              int(float(bbox.find('xmax').text)),
                              int(float(bbox.find('ymax').text))]
        objects.append(obj_struct)
    return objects



小于图像宽高的框可显示

wh = predicts[:, 2:4] - predicts[:, :2]
valid_mask = np.bitwise_and(wh[:, 0] < 0.5 * self.size[0], wh[:, 1] < 0.4 * self.size[1])
predicts = predicts[valid_mask, ...]

基本是为了取消掉预测出的那些预测框大于图像宽或高*0.5的。

# 基本是为了实现以下功能
delete_flag = 0
for index, i in enumerate(ext_model_predict):
    # print(index)
    shape2 = abs(i[0] - i[2])
    shape1 = abs(i[1] - i[3])
    compare_height = img.shape[1]*0.5
    compare_width = img.shape[0]*0.5
    if ((shape2 > compare_height) or (shape1 > compare_width)):
       ext_model_predict = np.delete(ext_model_predict, index-delete_flag, 0)
       delete_flag+=1



版权声明:本文为Cloud_yan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。