コード例 #1
0
    def cumTpFp(self, gtFile, detFile, label, overlapRatio, file_format):
        # 计算对应的图片中预测结果的TP和FP
        gtRects = []  # gtRect: label, xmin, ymin, xmax, ymax
        detRects = []  # detRect: label, xmin, ymin, xmax, ymax, score
        scores = []  # 当前类别的置信度
        num_pos = 0  # 真实数据中目标的个数
        if file_format[1] == '.txt':
            gtCon = open(gtFile)  # 打开当前文件
            gtLines = gtCon.readlines()  # 读取内容
            for gtLine in gtLines:
                if gtLine.split(' ')[0] == str(label):
                    gtRects.append((int(float(gtLine.split(' ')[1])), int(float(gtLine.split(' ')[2])),
                                    int(float(gtLine.split(' ')[3])), int(float(gtLine.split(' ')[4].strip('\n')))))
                    num_pos += 1
        elif file_format[1] == '.xml':
            gtLines=[]
            rects_xml = io_file.parse_xml(gtFile)  # 获取xml文件内容
            for rect_xml in rects_xml:
                if rect_xml[0] == str(label):
                    gtRects.append((rect_xml[1], rect_xml[2], rect_xml[3], rect_xml[4]))
                    gtLines.append(0)
                    num_pos += 1

        detCon = open(detFile)
        detLines = detCon.readlines()
        for detLine in detLines:
            # 统计当前类别下的目标
            if detLine.split(' ')[0] == str(label):
                detRects.append((int(detLine.split(' ')[1]), int(detLine.split(' ')[2]),
                                 int(detLine.split(' ')[3]), int(detLine.split(' ')[4])))
                scores.append(float(detLine.split(' ')[5].strip('\n')))
        # 统计当前类别下的TP和FP
        # det_state: [label, score, tp, fp], tp, fp = 0 or 1
        det_state = [(label, 0., 0, 1)] * len(detRects)
        iou_max = 0
        maxIndex = -1
        blockIdx = -1
        for cnt in range(len(det_state)):
            det_state[cnt] = (label, scores[cnt], 0, 1)  # 更新score值
        visited = [0] * len(gtLines)
        if len(detRects) != len(scores):
            print("Num of scores does not match detection results!")

        for indexDet, deti in enumerate(detRects):
            # 双循环保证乱序下的正确统计
            iou_max = 0
            maxIndex = -1
            blockIdx = -1
            for indexGt, gti in enumerate(gtRects):
                iou = utils.JaccardOverlap(detRects[indexDet], gtRects[indexGt])
                if iou > iou_max:
                    iou_max = iou
                    maxIndex = indexDet
                    blockIdx = indexGt
            # 没有进行NMS抑制,所以不使用标记位
            if iou_max >= overlapRatio and visited[blockIdx] == 0:
                det_state[maxIndex] = (label, scores[indexDet], 1, 0)
                visited[blockIdx] = 1
        # 返回每个预测结果的TP和FP统计,以及真实数据中目标数量
        return det_state, num_pos
コード例 #2
0
    def cumTpFp(self, gtFile, detFile, label, overlapRatio, file_format):

        # gtRect: label, xmin, ymin, xmax, ymax
        gtRects = []
        # gtRect: label, xmin, ymin, xmax, ymax, score
        detRects = []
        # scores: scores for label
        scores = []
        num_pos = 0
        if file_format[0] == '.txt':
            gtCon = open(gtFile)
            gtLines = gtCon.readlines()
            for gtLine in gtLines:
                if gtLine.split(' ')[0] == str(label):
                    gtRects.append((int(float(gtLine.split(' ')[1])), int(float(gtLine.split(' ')[2])),
                                    int(float(gtLine.split(' ')[3])), int(float(gtLine.split(' ')[4].strip('\n')))))
                    num_pos += 1
        elif file_format[0] == '.xml':
            rects_xml = io_file.parse_xml(gtFile)
            for rect_xml in rects_xml:
                if rect_xml[0] == str(label):
                    gtRects.append((rect_xml[0], rect_xml[1], rect_xml[2], rect_xml[3]))
                    num_pos += 1

        detCon = open(detFile)
        detLines = detCon.readlines()
        for detLine in detLines:
            if detLine.split(' ')[0] == str(label):
                detRects.append((int(detLine.split(' ')[1]), int(detLine.split(' ')[2]),
                                 int(detLine.split(' ')[3]), int(detLine.split(' ')[4])))
                scores.append(float(detLine.split(' ')[5].strip('\n')))
                #print(detLine.split(' ')[5])
        # det_state: [label, score, tp, fp], tp, fp = 0 or 1
        det_state = [(label, 0., 0, 1)] * len(detRects)
        iou_max = 0
        maxIndex = -1
        blockIdx = -1
        for cnt in range(len(det_state)):
            det_state[cnt] = (label, scores[cnt], 0, 1)
        visited = [0] * len(gtLines)
        if len(detRects) != len(scores):
            print("Num of scores does not match detection results!")
        for indexDet, deti in enumerate(detRects):
            iou_max = 0
            maxIndex = -1
            blockIdx = -1
            for indexGt, gti in enumerate(gtRects):
                iou = utils.JaccardOverlap(detRects[indexDet], gtRects[indexGt])
                if iou > iou_max:
                    iou_max = iou
                    maxIndex = indexDet
                    blockIdx = indexGt
            if iou_max >= overlapRatio and visited[blockIdx] == 0:
                det_state[maxIndex] = (label, scores[indexDet], 1, 0)
                visited[blockIdx] = 1

        return det_state, num_pos