import cv2 import numpy as np import os import glob def IOU(pred, label): Iand1 = np.sum(label[:, :] * pred[:, :]) Ior1 = np.sum(label[:, :]) + np.sum(pred[:, :]) - Iand1 IoU1 = Iand1 / Ior1 return IoU1 model_name = 'u2netp' mask_dir = os.path.join(os.getcwd(), 'test_data', model_name + '_results' + os.sep) label_dir = os.path.join(os.getcwd(), 'train_data', 'DUTS', 'DUTS-TR', 'miou_gt' + os.sep) mask_name_list = glob.glob(mask_dir + os.sep + '*') label_name_List = glob.glob(label_dir + os.sep + '*') for mask_name, label_name in mask_name_list, label_name_List: mask = cv2.imread(mask_name, cv2.IMREAD_GRAYSCALE) label = cv2.imread(label_name, cv2.IMREAD_GRAYSCALE) h = mask.shape[0] w = mask.shape[1] for i in range(h): for j in range(w): if mask[i][j] <= 127: mask[i][j] = 0 if mask[i][j] > 127: mask[i][j] = 1 if label[i][j] <= 127: label[i][j] = 0 if label[i][j] > 127: label[i][j] = 1 iou = IOU(mask, label) print(iou)