判断检测结果与标定结果的交并比

人脸检测算法没有输出分数,所以只能计算检测结果的矩形框和标定结果的矩形框之间的交并比。

 

 

# -*- coding: utf-8 -*-
import os
import cv2 as cv
filepathresultname=[]
faceplus=[]
filepath="E:/DataSet/IristarFace/"
markedfilename='onefacepath1080.txt'#标定结果路径  格式:name\n  num\n  rect\n#
comparefilename='0628.txt'#算法结果路径   格式:name\n  num\n  rect\n
imagepath="E:/DataSet/IristarFace/"#图像路径
saveimagepath="E:/DataSet/IristarFace/temp/mtcnnsave/"#结果图像存储路径
markedfile=open(filepath+markedfilename,'r')
comparefile=open(filepath+comparefilename,'r')

resultin=[]

def ComparefileToNamepath(markedresults,compareresults):#Comparefile中的结果按照markedfilename中顺序重新排序
    markedthree=[]#markedresults中的文件名称
    comparethree = []
    temp=[]
    #将markedresults 转为3列
    for i in range(len(markedresults)):
        if i%3==0:#name
            temp.append(markedresults[i])
        if i%3==1:#num
            temp.append(markedresults[i])
        if i % 3 == 2:#rect
            temp.append(markedresults[i])
            markedthree.append(temp)
            temp=[]
    #compareresults 转为3列
    for i in range(len(compareresults)):
        if i%3==0:#name
            temp.append(compareresults[i])
        if i%3==1:#num
            temp.append(compareresults[i])
        if i % 3 == 2:#rect
            temp.append(compareresults[i])
            comparethree.append(temp)
            temp=[]
    compareout=[]
    #提取第一列的文件名
    markedname=[x[0] for x in markedthree]
    comparename = [x[0] for x in comparethree]
    #按照markedname在comparename中的顺序对comparethree进行排序  并转化为一列数据
    for i in range(len(markedname)):
        if markedname[i] in comparename:
            compareout.append(comparethree[comparename.index(markedname[i])])
        else:
            temp=[]
            temp.append(markedname[i])
            temp.append("0")
            temp.append("0 0 0 0 ")
            compareout.append(temp)
    #将compareout转为一列
    compare_order=[]

    for i in range(len(compareout)):
        for t in range(3):
            if t%3==0:
                compare_order.append(compareout[i][t])
            if t%3==1:
                compare_order.append(compareout[i][t])
            if t%3==2:
                compare_order.append(compareout[i][t])

    return compare_order


def mat_inter(box1, box2):
    # 判断两个矩形是否相交
    # box=(xA,yA,xB,yB)
    x01, y01, x02, y02 = box1
    x11, y11, x12, y12 = box2

    lx = abs((x01 + x02) / 2 - (x11 + x12) / 2)
    ly = abs((y01 + y02) / 2 - (y11 + y12) / 2)
    sax = abs(x01 - x02)
    sbx = abs(x11 - x12)
    say = abs(y01 - y02)
    sby = abs(y11 - y12)
    if lx <= (sax + sbx) / 2 and ly <= (say + sby) / 2:
        return True
    else:
        return False


def solve_coincide(box1, box2):
    # box=(xA,yA,xB,yB)
    # 计算两个矩形框的重合度
    if mat_inter(box1, box2) == True:
        x01, y01, x02, y02 = box1
        x11, y11, x12, y12 = box2
        col = min(x02, x12) - max(x01, x11)
        row = min(y02, y12) - max(y01, y11)
        intersection = col * row
        area1 = (x02 - x01) * (y02 - y01)
        area2 = (x12 - x11) * (y12 - y11)
        coincide = float(intersection )/ float(area1 )
        return coincide
    else:
        return 0

def compare():
    overlapthreshold=0.8
    goodnum=0
    lowoverlapnum=0
    markedresults=[]
    compareresults=[]
    comparezero=0
    for lines in markedfile.readlines():
        markedresults.append(lines)

    for lines in comparefile.readlines():
        compareresults.append(lines)
        if lines=='0\n':
            compareresults.append("0 0 0 0 \n")
            comparezero=comparezero+1
    compare_order=ComparefileToNamepath(markedresults, compareresults)#调整compareresults顺序
    for i in range(len(compare_order)):
        if i % 3 == 0:
            print(markedresults[i])
            if markedresults[i] != compare_order[i]:
                print ("check file name ")
                print(markedresults[i])
                print(compare_order[i])

        if i % 3 == 2 :
            marked_x, marked_y, marked_w, marked_h = markedresults[i].split()
            rectmarked = int(marked_x), int(marked_y), int(marked_x) + int(marked_w), int(marked_y) + int(marked_h)
            comparesplit=compare_order[i].split()
            #print(compareresults[i])
            if len(comparesplit)==1:
                readpath = (imagepath + markedresults[i - 2]).replace('\n', '')
                img = cv.imread(readpath)
                cv.rectangle(img, (rectmarked[0], rectmarked[1]), (rectmarked[2], rectmarked[3]), (255, 0, 0), 3, 4, 0)
                cv.imwrite(saveimagepath+compare_order[i-2].replace('\n', ''),img)
                continue;
            elif len(comparesplit)>=5:
                #compare_x, compare_y, compare_w, compare_h ,score= compareresults[i].split()
                rectcompare = int(comparesplit[0]), int(comparesplit[1]), int(comparesplit[0]) + int(comparesplit[2]), int(comparesplit[1]) + int(comparesplit[3]),
                interper = solve_coincide(rectmarked, rectcompare)
                if interper > overlapthreshold:
                    goodnum = goodnum + 1
                else:  # save result
                    lowoverlapnum=lowoverlapnum+1
                    readpath = (imagepath + markedresults[i - 2]).replace('\n', '')
                    img = cv.imread(readpath)
                    cv.rectangle(img, (rectmarked[0], rectmarked[1]),
                                 ( rectmarked[2], rectmarked[3]), (255, 0, 0), 3, 4, 0)
                    cv.rectangle(img, (rectcompare[0], rectcompare[1]),
                                 ( rectcompare[2],  rectcompare[3]), (0, 255, 0), 3, 4, 0)
                    cv.imwrite(saveimagepath + compare_order[i - 2].replace('\n', '').replace('/', ''), img)

    markedfile.close()
    comparefile.close()
    print("未检出图像"+str(comparezero)+"个")
    print("交并比低于" + str(overlapthreshold)+"的图像" +str(lowoverlapnum)+ "个")
    return goodnum



print("正确检出图像"+str(compare())+"个")



 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值