def evaluateResults():
    alfas = np.arange(0, 15)
    rhos = np.arange(0, 1.1, 0.1)
    TP = []
    TN = []
    FP = []
    FN = []
    precision = []
    recall = []
    F1 = []
    F1_adaptative = []
    bestF1 = 0
    optAlpha = 0

    # Compute the evaluation
    '''
        for alfa in alfas:

        # One gaussian
        dataset    = "Traffic"
        datasetGT  = "TrafficGT"
        colorSpace = 'gray' # 'gray', 'HSV', 'YCrCb', 'BGR'
        gm.obtainGaussianModell(dataset, datasetGT, colorSpace, alfa)

        TPi,TNi,FPi,FNi,precisioni,recalli,F1i = ev.evaluateFolder("./results/imagesGaussianModelling/","Traffic")

        TP.append(TPi)
        TN.append(TNi)
        FP.append(FPi)
        FN.append(FNi)
        precision.append(precisioni)
        recall.append(recalli)
        F1.append(F1i)

        # Adaptative gaussian
        mu, sigma = ag.obtainGaussianModell("Traffic")
        ag.foreground_substraction("Traffic", "TrafficGT", mu, sigma, alfa, 0) # rho equal to 0, in order to find the optimal alpha
        aux,aux,aux,aux,aux,aux,F1i_adaptative = ev.evaluateFolder("./results/imagesAdaptativeGaussian/")
        F1_adaptative.append(F1i_adaptative)

        if F1i_adaptative > bestF1:
            bestF1 = F1i_adaptative
            optAlpha = alfa

        print ('--- Alfa: ' + str(alfa) + ' --- Rho: 0')
        print ('--- F1 Gaussian Model: ' + str(F1i))
        print ('--- F1 Adaptative Gaussian Model: ' + str(F1i_adaptative))
        '''

    bestF1 = 0
    optAlpha = 1.8
    for rho in rhos:
        # Adaptative gaussian
        mu, sigma = ag.obtainGaussianModell("Highway")
        ag.foreground_substraction("Highway", "HighwayGT", mu, sigma, optAlpha,
                                   rho)
        aux, aux, aux, aux, aux, aux, F1 = ev.evaluateFolder(
            "./results/imagesAdaptativeGaussianModelling/")

        if F1 > bestF1:
            bestF1 = F1
            optRho = rho

        print('--- Rho: ' + str(rho) + ' --- ' + 'optimal Alpha: ' +
              str(optAlpha))
        print('--- F1 Adaptative Gaussian Model: ' + str(F1))
def evaluateTask1(colorSpace='gray'):
    datasets = ['Fall']  # ["Highway","Fall","Traffic"]
    datasetsGT = [el + "GT" for el in datasets]
    alfaRange = np.arange(0, 5.001, 0.5)
    rhoRange = np.arange(0, 1.1, 0.1)

    if (os.path.exists('results/task1.json')):
        with open('results/task1.json') as f:
            datasetsResults = json.load(f)
            print "Dataset results loaded"
    else:
        datasetsResults = dict()

    for d, dGT in zip(datasets, datasetsGT):
        if d not in datasetsResults.keys():
            datasetsResults[d] = []
        alfaResults = [el['Alfa'] for el in datasetsResults[d]]

        for alfa in alfaRange:
            if alfa in alfaResults:
                print "Alfa " + str(
                    alfa) + " already done for this dataset. Skipping"
                continue
            print "Dataset " + d + ", alfa: " + str(alfa)
            gm.obtainGaussianModell(d, dGT, colorSpace, alfa)
            TPi, TNi, FPi, FNi, precisioni, recalli, F1i, _ = ev.evaluateFolder(
                "./results/imagesGaussianModelling/", d)
            performance = dict()
            performance['Alfa'] = alfa
            performance['TP'] = TPi
            performance['TN'] = TNi
            performance['FP'] = FPi
            performance['FN'] = FNi
            performance['precision'] = precisioni
            performance['recall'] = recalli
            performance['F1'] = F1i
            print F1i
            datasetsResults[d].append(performance)

            with open('results/task1.json', "w") as f:
                json.dump(datasetsResults, f)

    alfaRange = sorted([el['Alfa'] for el in datasetsResults['Fall']])

    fall = sorted(datasetsResults['Fall'], key=lambda k: k['Alfa'])
    traffic = sorted(datasetsResults['Traffic'], key=lambda k: k['Alfa'])
    highway = sorted(datasetsResults['Highway'], key=lambda k: k['Alfa'])

    for el in fall:
        print str(el['Alfa']) + ': ' + str(el['F1'])

    fig = plt.figure()
    plt.plot(alfaRange, [el['F1'] for el in fall], 'r', label='Fall')
    plt.plot(alfaRange, [el['F1'] for el in traffic], 'g', label='Traffic')
    plt.plot(alfaRange, [el['F1'] for el in highway], 'b', label='Highway')
    plt.xlabel("Alfa")
    plt.ylabel("F1")
    plt.title("F1 vs alfa")
    plt.axis([0, 5, 0, 1])
    lgd = plt.legend(bbox_to_anchor=(1.3, 0.8))
    plt.savefig("task1.png", bbox_extra_artists=(lgd, ), bbox_inches='tight')
    plt.show()

    #AUC:

    y = np.array([el['precision'] for el in fall])
    x = np.array([el['recall'] for el in fall])
    AUC = trapz(y, x)
    print " AUC of Fall: " + str(AUC)

    y = np.array([el['precision'] for el in traffic])
    x = np.array([el['recall'] for el in traffic])
    AUC = trapz(y, x)
    print " AUC of Traffic: " + str(AUC)

    y = np.array([el['precision'] for el in highway])
    x = np.array([el['recall'] for el in highway])
    AUC = trapz(y, x)
    print " AUC of Highway: " + str(AUC)

    fig = plt.figure()
    plt.plot([el['recall'] for el in fall], [el['precision'] for el in fall],
             'r',
             label='Fall')
    plt.plot([el['recall'] for el in traffic],
             [el['precision'] for el in traffic],
             'g',
             label='Traffic')
    plt.plot([el['recall'] for el in highway],
             [el['precision'] for el in highway],
             'b',
             label='Highway')
    plt.axis([0, 1, 0, 1])
    plt.title("Precision-Recall")
    plt.xlabel("Recall")
    plt.ylabel("Precision")

    lgd = plt.legend(bbox_to_anchor=(1.3, 0.8))
    plt.savefig("task1-curves.png",
                bbox_extra_artists=(lgd, ),
                bbox_inches='tight')
    plt.show()
if not os.path.exists(gM_path):
    os.makedirs(gM_path)

aG_path = results_path + "/imagesAdaptativeGaussianModelling/"  #"/imagesAdaptativeGaussian/"
if not os.path.exists(aG_path):
    os.makedirs(aG_path)

# Compute the evaluation
for alfa in alfas:

    # One gaussian
    dataset = "Highway"
    datasetGT = "HighwayGT"
    colorSpace = 'gray'  # 'gray', 'HSV', 'YCrCb', 'BGR'
    gm.obtainGaussianModell(dataset, datasetGT, colorSpace, alfa)
    TPi, TNi, FPi, FNi, precisioni, recalli, F1i, auci = ev.evaluateFolder(
        gM_path, datasetGT)

    TP.append(TPi)
    TN.append(TNi)
    FP.append(FPi)
    FN.append(FNi)
    precision.append(precisioni)
    recall.append(recalli)
    F1.append(F1i)
    auc.append(auci)

    # Adaptative gaussian
    mu, sigma = ag.obtainGaussianModell(dataset)
    ag.foreground_substraction(
        dataset, datasetGT, mu, sigma, alfa,
        0)  # rho equal to 0, in order to find the optimal alpha
Esempio n. 4
0
        alfaRange = np.concatenate((alfaPre, alfaRange, alfaPost), axis=0)

    for alfa in alfaRange:
        if alfa in alfaResults:
            print "Alfa " + str(
                alfa) + " already done for this dataset. Skipping"
            continue
        print "Dataset " + d + ", alfa: " + str(alfa)

        if conf.isShadowremoval:
            mu, sigma = acg.obtainGaussianModell(
                d, conf.OptimalColorSpaces["ShadowRemoval"])
            acg.foreground_substraction(
                d, dGT, mu, sigma, alfa, conf.OptimalRhoParameter[d],
                conf.OptimalColorSpaces["ShadowRemoval"])
            TPi, TNi, FPi, FNi, precisioni, recalli, F1i = ev.evaluateFolder(
                "./results/imagesAdaptativeGaussianModelling/", d)
        else:
            mu, sigma = acg.obtainGaussianModell(d, conf.OptimalColorSpaces[d])
            acg.foreground_substraction(d, dGT, mu, sigma, alfa,
                                        conf.OptimalRhoParameter[d],
                                        conf.OptimalColorSpaces[d])
            TPi, TNi, FPi, FNi, precisioni, recalli, F1i = ev.evaluateFolder(
                "./results/imagesAdaptativeGaussianModelling/", d)

        performance = dict()
        performance['Alfa'] = alfa
        performance['TP'] = TPi
        performance['TN'] = TNi
        performance['FP'] = FPi
        performance['FN'] = FNi
        performance['precision'] = precisioni
        instance = np.stack([out, out, outError], axis=-1)
        # cv2.imshow("OutputColor", instance * 255)
        # cv2.imshow("Image", frame)
        # cv2.imshow("Output", out * 255)
        # cv2.imshow("GT", groundTruth*255)

        # k = cv2.waitKey(5) & 0xff
        # if k == 27:
        #     break

        file_name = framesFiles[idx]
        cv2.imwrite(
            './results/StaufferAndGrimson/' +
            file_name[file_name.rfind('\\') + 1:], out)
        videoOutput.write(instance * 255)

    videoOutput.release()


if __name__ == "__main__":
    dataset = "Highway"
    datasetGT = "HighwayGT"
    history = conf.history
    nGauss = conf.nGauss
    bgThresh = conf.bgThresh
    StaufferAndGrimsonAlgorithm(dataset, datasetGT, history, nGauss, bgThresh)
    aux, aux, aux, aux, aux, aux, F1 = ev.evaluateFolder(
        "./results/StaufferAndGrimson/")

    print('--- F1 Stauffer And Grimson Model: ' + str(F1))
    aG_path = results_path + "/imagesAdaptativeGaussianModelling/"  # "/imagesAdaptativeGaussian/"
    if not os.path.exists(aG_path):
        os.makedirs(aG_path)

    if conf.isShadowremoval:
        print('--- obtainGaussianModell')
        mu, sigma = obtainGaussianModell(
            dataset, conf.OptimalColorSpaces["ShadowRemoval"])
        print('--- foreground_substraction')
        foreground_substraction(dataset, datasetGT, mu, sigma,
                                conf.OptimalAlphaParameter[dataset],
                                conf.OptimalRhoParameter[dataset],
                                conf.OptimalColorSpaces["ShadowRemoval"])
        print('--- evaluateFolder')
        aux, aux, aux, aux, aux, aux, F1 = ev.evaluateFolder(aG_path, dataset)
    else:
        print('--- obtainGaussianModell')
        mu, sigma = obtainGaussianModell(dataset,
                                         conf.OptimalColorSpaces[dataset])
        print('--- foreground_substraction')
        foreground_substraction(dataset, datasetGT, mu, sigma,
                                conf.OptimalAlphaParameter[dataset],
                                conf.OptimalRhoParameter[dataset],
                                conf.OptimalColorSpaces[dataset])
        print('--- evaluateFolder')
        aux, aux, aux, aux, aux, aux, F1 = ev.evaluateFolder(aG_path, dataset)

    print('--- DataSet: ' + dataset)
    print('--- Rho: ' + str(conf.OptimalRhoParameter[dataset]) + ' --- ' +
          ' Alpha: ' + str(conf.OptimalAlphaParameter[dataset]))
Precision = []
Recall = []
F1 = []

for p in range(0, 200, 10):
    print "Filtering objects smaller than " + str(p)
    for f in files:
        out = bwareaopen.bwareaopen(f, p)
        if operativeSystem == 'posix':
            #posix systems go here: ubuntu, debian, linux mint, red hat, etc, even osX (iew)
            cv2.imwrite('results/areaFiltering/' + ID + f.split('/')[-1], out)

        else:
            #say hello to propietary software
            cv2.imwrite('results/areaFiltering/' + ID + f.split('\\')[-1], out)
    tp, tn, fp, fn, precision, recall, f1 = ev.evaluateFolder(
        'results/areaFiltering/', ID)
    for f in glob.glob('results/areaFiltering/*'):
        os.remove(f)
    print tp, tn, fp, fn, precision, recall, f1
    TP.append(tp)
    TN.append(tn)
    FP.append(fp)
    FN.append(fn)
    Precision.append(precision)
    Recall.append(recall)
    F1.append(f1)

results['TP'] = TP
results['TN'] = TN
results['FP'] = FP
results['FN'] = FN