get_pr.py 2.97 KB
import os

import cv2
import numpy as np
from sklearn.metrics import precision_score, recall_score, confusion_matrix


def iou(box, boxes):
    x1, y1, x2, y2 = box
    x1s, y1s, x2s, y2s = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
    area1 = abs(x2 - x1) * abs(y2 - y1)
    areas = (x2s - x1s) * (y2s - y1s)
    xx1 = np.maximum(x1, x1s)
    yy1 = np.maximum(y1, y1s)
    xx2 = np.minimum(x2, x2s)
    yy2 = np.minimum(y2, y2s)
    inner = np.maximum(0, (xx2 - xx1) * (yy2 - yy1))
    return inner / (area1 + areas - inner)

def get_evaluate_score(true_image_path,true_label_path,predict_label_path,threshold):
    true_labels = os.listdir(true_label_path)
    predict_labels = os.listdir(predict_label_path)
    targets, predicts = [], []
    for label in true_labels:
        true_label = open(os.path.join(true_label_path, label)).readlines()
        img = cv2.imread(os.path.join(true_image_path, label.replace('.txt', '.jpg')))
        h, w, c = img.shape
        if len(true_label) == 0:
            targets.append(0)
            if label in predict_labels:
                predicts.append(1)
            else:
                predicts.append(0)

        else:
            targets.append(1)
            if label not in predict_labels:
                predicts.append(0)
            else:
                tmp = 0
                predict_label = open(os.path.join(predict_label_path,label)).readlines()
                boxes = []
                for pl in predict_label:
                    cls, x1, y1, w1, h1 = [float(i) for i in pl.strip().split(' ')]
                    x1, y1, w1, h1 = int(x1 * w), int(y1 * h), int(w1 * w), int(h1 * h)
                    xx1, yy1, xx2, yy2 = x1 - w1 // 2, y1 - h1 // 2, x1 + w1 // 2, y1 + h1 // 2
                    boxes.append([xx1, yy1, xx2, yy2])
                for tl in true_label:
                    cls, x1, y1, w1, h1 = [float(i) for i in tl.strip().split(' ')]
                    x1, y1, w1, h1 = int(x1 * w), int(y1 * h), int(w1 * w), int(h1 * h)
                    xx1, yy1, xx2, yy2 = x1 - w1 // 2, y1 - h1 // 2, x1 + w1 // 2, y1 + h1 // 2
                    box1 = [xx1, yy1, xx2, yy2]
                    inner_score = iou(np.array(box1),np.array(boxes))
                    if max(inner_score)>threshold:
                        tmp=1
                        predicts.append(1)
                        break
                if tmp==0:
                    predicts.append(0)
    p = precision_score(targets,predicts)
    r = recall_score(targets,predicts)
    conf = confusion_matrix(targets,predicts)
    print('precison:',p)
    print('recall:',r)
    print(conf)

if __name__ == '__main__':
    true_image_path = '/home/qfs/WorkSpace/ps_tamper/yolov5_ps/val_data/minsheng/images'
    true_label_path = '/home/qfs/WorkSpace/ps_tamper/yolov5_ps/val_data/minsheng/labels'
    predict_label_path = '/home/qfs/WorkSpace/ps_tamper/yolov5/runs/detect/exp/labels'
    threshold = 0.1
    get_evaluate_score(true_image_path,true_label_path,predict_label_path,threshold)