import os
from unittest import result
import pandas as pd
import json

dirname = '人工标注'
outputname = '人工标注比对结果'
# dirname = 'AI辅助标注'
# outputname='AI辅助标注比对结果'
# dirname = '纯AI'
# outputname='纯AI比对结果'
studyID_out = []
seriesID_out = []
labelout = []
gold_labelout = []
posout = []
gold_posout = []
cnt = []
cntid = []
marker_out=[]

def iou_3d(box1, box2):
    '''
        box [x1,y1,z1,x2,y2,z2]   分别是两对角定点的坐标
    '''
    area1 = (box1[3]-box1[0])*(box1[4]-box1[1])*(box1[5]-box1[2])
    area2 = (box2[3]-box2[0])*(box2[4]-box2[1])*(box2[5]-box2[2])
    area_sum = area1 + area2

    # 计算重叠部分 设重叠box坐标为 [x1,y1,z1,x2,y2,z2]
    x1 = max(box1[0], box2[0])
    y1 = max(box1[1], box2[1])
    z1 = max(box1[2], box2[2])
    x2 = min(box1[3], box2[3])
    y2 = min(box1[4], box2[4])
    z2 = min(box1[5], box2[5])
    if x1 >= x2 or y1 >= y2 or z1 >= z2:
        return 0
    else:
        inter_area = (x2-x1)*(y2-y1)*(z2-z1)

    return inter_area/(area_sum-inter_area)


gold = pd.read_excel("原始数据/金标准.xlsx", encoding='gbk', header=0)

num = 0
for root, dirs, files in os.walk(dirname):
    for file in files:
        num = num+1
        print(str(num)+"/"+str(len(files)))
        filename = root+"/"+file
        df = pd.read_csv(filename, encoding='gbk', header=0)
        for indexs in df.index:
            data = df.loc[indexs].values
            marker=data[1]
            studyID = data[2]
            seriesID = data[3]
            startTime = data[4]
            endTime = data[5]
            label = data[6]
            label = json.loads(label)
            # posAI=label['position']
            leftTopPoint = label['rectangle']["leftTopPoint"]
            rightBottomPoint = label['rectangle']["rightBottomPoint"]
            pos = [leftTopPoint['x'], leftTopPoint['y'], leftTopPoint['z'],
                   rightBottomPoint['x'], rightBottomPoint['y'], rightBottomPoint['z']]
            # pos=[posAI[0],posAI[1],posAI[2],posAI[3],posAI[4],posAI[5]]
            typeID = data[7]
            typeNmae = data[8]
            flag = 0
            try:
                gold_place = gold.loc[gold['序列号'] == seriesID].index[0]
            except:
                print("没有找到金标准病例")
                break
            for gold_indexs in range(gold_place, gold.shape[0]):
                gold_data = gold.loc[gold_indexs].values
                if gold_data[1] == seriesID:
                    flag = 1
                    gold_label = gold_data[2]
                    gold_label = json.loads(gold_label)
                    gold_leftTopPoint = gold_label['rectangle']["leftTopPoint"]
                    gold_rightBottomPoint = gold_label['rectangle']["rightBottomPoint"]
                    gold_pos = [gold_leftTopPoint['x'], gold_leftTopPoint['y'], gold_leftTopPoint['z'],
                                gold_rightBottomPoint['x'], gold_rightBottomPoint['y'], gold_rightBottomPoint['z']]
                    # gold_pos=[gold_label['position'][0],gold_label['position'][1],gold_label['position'][2],gold_label['position'][3],gold_label['position'][4],gold_label['position'][5]]
                    iou = iou_3d(pos, gold_pos)
                    gold_type = gold_data[3]
                    gold_typename = gold_data[4]
                    if iou == 1 and typeID == gold_type:
                        marker_out.append(marker)
                        studyID_out.append(studyID)
                        seriesID_out.append(seriesID)
                        labelout.append(label)
                        gold_labelout.append(gold_label)
                        posout.append(typeNmae)
                        gold_posout.append(gold_typename)
                        cnt.append("完全正确")
                        cntid.append(1)
                        break
                    if iou == 1 and typeID != gold_type:
                        marker_out.append(marker)
                        studyID_out.append(studyID)
                        seriesID_out.append(seriesID)
                        labelout.append(label)
                        gold_labelout.append(gold_label)
                        posout.append(typeNmae)
                        gold_posout.append(gold_typename)
                        cnt.append("位置正确，分类错误")
                        cntid.append(1)
                        break
                    elif iou < 1 and typeID != gold_type and iou > 0.5:
                        marker_out.append(marker)
                        studyID_out.append(studyID)
                        seriesID_out.append(seriesID)
                        labelout.append(label)
                        gold_labelout.append(gold_label)
                        posout.append(typeNmae)
                        gold_posout.append(gold_typename)
                        cnt.append("位置调整")
                        cntid.append(0)
                        break
                elif gold_data[1] != seriesID and flag == 1:
                    marker_out.append(marker)
                    studyID_out.append(studyID)
                    seriesID_out.append(seriesID)
                    labelout.append(label)
                    gold_labelout.append("")
                    posout.append(typeNmae)
                    gold_posout.append("")
                    cnt.append("位置错误")
                    cntid.append(0)
                    break


output = pd.DataFrame({"检查号": studyID_out, "序列号": seriesID_out, "标注医生":marker_out,"标注位置": labelout,
                       "金标准位置": gold_labelout, "标注分类": posout, "金标准分类": gold_posout, "比对结果": cnt})
output.to_csv(outputname+".csv", encoding='gbk')
