atom-predict/msunet/test_acc.py

50 lines
1.4 KiB
Python
Executable File

import os
import cv2
import glob
import json
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.metrics
from tqdm import tqdm
from PIL import Image
from utils.e2e_metrics import get_metrics,acc
# from core.data import get_y_3
# from core.data import load_data
from sklearn.metrics import accuracy_score, f1_score, recall_score, precision_score, confusion_matrix
def read_json_file(json_path,img):
with open(json_path) as f:
json_data = json.load(f)
points = np.array([item['points'][0][::-1] for item in json_data['shapes']], np.int32)
pred = np.array([class_dict_rev[item['label']] for item in json_data['shapes']], np.int32)
mask = np.zeros_like(img)
mask[points[:, 0], points[:, 1]] = pred + 1
return mask
class_dict_rev = {
'Norm': 0
}
res = []
img_lst = glob.glob('/home/gao/mouclear/1(1)/test/img/*.jpg')
for item in img_lst:
# item = '/home/gao/mouclear/1(1)/test/img/1.json'
img = cv2.imread(item, 0)
json_path = item.replace('.jpg', '.json')
pred = read_json_file(json_path,img)
file_name_without_ext = os.path.basename(item).rsplit('.', 1)[0]
label = read_json_file('/home/gao/mouclear/1(1)/test/label_json/' + file_name_without_ext + '.json',img)
print('当前检测'+item+'准确率为'+str(acc(label, pred)))
res.append(acc(label, pred))
print('平均准确率:')
print(np.average(np.array(res)))