from sklearn.metrics import confusion_matrix
import numpy as np
from PIL import Image
import os


iou = 0
iouall = 0
ioumean = 0
ioumax = 0
ioumin = 1
filesnum = 0
sen = 0
senall = 0
senmean = 0
acc = 0
accall = 0
accmean = 0
spe = 0
speall = 0
spemean = 0

# Result txt file saving PATH
log = open(r'D:/result_contract/Otherkind/Tongue/20191119/109/result.txt', 'w')


def compute_iou(y_pred, y_true):
    # ytrue, ypred is a flatten vector
    y_pred = y_pred.flatten()
    y_true = y_true.flatten()
    tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
    # compute mean iou
    print("tn, fp, fn, tp:", (tn, fp, fn, tp), file=log)
    # tp/(tp + fp + fn)
    iou_tp = tp / (tp + fp + fn)
    sen_tp = tp / (tp + fn)
    acc_tp = (tp + tn) / (tn + fp + fn + tp)
    spe_tp = tn / (tn + fp)
    global iou
    iou = iou_tp
    global sen
    sen = sen_tp
    global acc
    acc = acc_tp
    global spe
    spe = spe_tp
    print("IoU:", iou_tp, file=log)
    print("SEN:", sen_tp, file=log)
    print("ACC:", acc_tp, file=log)
    print("SPE:", spe_tp, file=log)


#"C:/Users/user/Desktop/test16/gt_img/"
#"C:/Users/user/Desktop/test16/testdataset/Fair/gt/"

for filenames in os.listdir(r"D:/result_contract/Otherkind/Tongue/20191119/ioulabel/"):  # label gray scale images PATH
    print(filenames, file=log)
    filename = filenames.replace('', '')
    print(filename)
    img1 = Image.open("D:/result_contract/Otherkind/Tongue/20191119/ioulabel/" + filenames)  # label gray scale images PATH
    img11 = img1.convert('L')
    threshold = 128
    table1 = []
    for i in range(256):
        if i < threshold:
            table1.append(0)
        else:
            table1.append(1)
    img11 = img11.point(table1, '1')
    img_true = np.array(img11.convert("1").getdata())
    img11.save('D:/result_contract/Otherkind/Tongue/20191119/ioulabel_convert/' + filenames)  # label binary images output PATH

    img2 = Image.open("D:/result_contract/Otherkind/Tongue/20191119/109/" + filename)  # Deep Learning model generated images PATH
    img22 = img2.convert('L')
    table2 = []
    for i in range(256):
        if i < threshold:
            table2.append(0)
        else:
            table2.append(1)
    img22 = img22.point(table2, '1')
    img_pred = np.array(img22.convert("1").getdata())
    img22.save('D:/result_contract/Otherkind/Tongue/20191119/109_convert/' + filename)  # generated images binary converted output PATH

    compute_iou(img_pred, img_true)
    if iou >= ioumax:
        ioumax = iou
    if iou <= ioumin:
        ioumin = iou
    iouall = iouall + iou
    senall = senall + sen
    accall = accall + acc
    speall = speall + spe
    filesnum = filesnum + 1
    ioumean = iouall / filesnum
    senmean = senall / filesnum
    accmean = accall / filesnum
    spemean = speall / filesnum
    print("IoUmean:", ioumean, file=log)
    print("IoUmax:", ioumax, file=log)
    print("IoUmin:", ioumin, file=log)
    print("SENmean:", senmean, file=log)
    print("ACCmean:", accmean, file=log)
    print("SPEmean:", spemean, file=log)

log.close()
