iou函数,我已经写出来了,为啥总是运行不了

img

img

这是需要处理的数据和代码,究竟哪里不行


import cmath
import math
from cmath import pi


def list_xyr(url):  # url 为需要提取的txt文件的位置,m为需要提取的数据的位置
    '''
    提取txt文件中的数据并将其存为列表的形式
    :param url:
    :param m:
    :param n:
    :return:
    '''
    f = open(url)  # 打开txt文件
    lines = f.read().splitlines()  # 以行的形式进行读取
    list1 = []  # 建立空列表将所需要的数据存放其中
    for line in lines:
        a = line.split(',')
        x = a[0]
        y = a[1]
        r = a[2]
        list1.append([x, y, r])
    f.close()
    return list1


# 文件地址
box1_list = list_xyr(r'D:\pycharm\ka\gt_xyr.txt')
# 文件地址
box2_list = list_xyr(r'D:\pycharm\ka\pr_xyr.txt')
print(box1_list)
print(box2_list)
#
# box1=list1
# box2=list2
# area1 =box1[2]**2*pi
# area2 =box2[2]**2*pi
# #     area2 = _box_b[0][2]**2*pi
temp_ious = []


def circle_iou(box1, box2):
    new1 = []
    new2 = []
    for a in box1:
        new1.append(float(a))
    for b in box2:
        new2.append(float(b))
    box1 = new1
    box2 = new2
    # print(box1,box2)
    area1 = (box1[2]) ** 2 * pi
    area2 = (box2[2]) ** 2 * pi
    d = abs(cmath.sqrt((box2[0] - box1[0]) ** 2 + (box2[1] - box1[1]) ** 2))
    if d >= box1[2] + box2[2]:
        temp_ious.append(0.0)
    elif max(box1[2], box2[2]) >= d + min(box1[2], box2[2]):
        iou_inter = pi * min(box1[2], box2[2]) ** 2
        iou = iou_inter * 1.0 / (pi * max(box1[2], box2[2]) ** 2)
        iou = round(iou, 2)
        temp_ious.append(iou)
    else:
        half_a = math.acos((box1[2] ** 2 + d ** 2 - box2[2] ** 2) / (2 * box1[2] * d))
        s1 = half_a * (box1[2] ** 2)
        half_b = math.acos((box2[2] ** 2 + d ** 2 - box1[2] ** 2) / (2 * box2[2] * d))
        s2 = half_b * (box2[2] ** 2)
        s_quad = box1[2] * d * math.sin(half_a)
        iou_inter = s1 + s2 - s_quad
        iou = iou_inter / (area2 + area1 - iou_inter)
        iou = round(iou, 2)
        temp_ious.append(iou)
    return temp_ious


# circle_iou(list1,list2)
for i in range(249):
    box1 = box1_list[i]
    box2 = box2_list[i]
    result = circle_iou(box1, box2)
    with open('iou_result.txt', 'a+') as f:
        for i in result:
            f.write(str(i) + '\n')
    print(result)