from sklearn.metrics import accuracy_score
import sys
import os

# ------------------------------------------------------------------
# Ovaj fajl ne menjati, da bi automatsko ocenjivanje bilo moguce
if len(sys.argv) > 1:
    VALIDATION_DATASET_PATH = sys.argv[1]
else:
    VALIDATION_DATASET_PATH = '.'+os.path.sep+'dataset'+os.path.sep+'validation'+os.path.sep
# ------------------------------------------------------------------

labeled_age = dict()
labeled_gender = dict()
labeled_race = dict()

with open(VALIDATION_DATASET_PATH+'annotations.csv') as file:
    data = file.read()
    lines = data.split('\n')
    for index, line in enumerate(lines):
        if index == 0:
            continue
        cols = line.split(',')
        if cols and cols[0] == '':
            continue
        image = cols[0].replace('\r', '')
        age = cols[1].replace('\r', '')
        gender = cols[2].replace('\r', '')
        race = cols[3].replace('\r', '')
        labeled_age[str(image)] = int(age)
        labeled_gender[str(image)] = gender
        labeled_race[str(image)] = race
    


# read results file generated by the main.py
results_age = dict()
results_gender = dict()
results_race = dict()

# with open(VALIDATION_DATASET_PATH+'annotations.csv') as file:
with open('result.csv') as file:
    data = file.read()
    lines = data.split('\n')
    for index, line in enumerate(lines):
        cols = line.split(',')
        # if index == 0:
        #     continue
        if cols and cols[0] == '':
            continue
        image = cols[0].replace('\r', '')
        age = cols[1].replace('\r', '')
        gender = cols[2].replace('\r', '')
        race = cols[3].replace('\r', '')

        results_age[str(image)] = int(age)
        results_gender[str(image)] = gender
        results_race[str(image)] = race

# # evaluate how results file matches the labelled samples
def get_comparison(results, labeled_samples):
    truth = []
    predicted = []
    for image in results:
        truth.append(labeled_samples[image])
        predicted.append(results[image])
    return truth, predicted

truth_age, predicted_age = get_comparison(results_age, labeled_age)
truth_gender, predicted_gender = get_comparison(results_gender, labeled_gender)
truth_race, predicted_race = get_comparison(results_race, labeled_race)

age_cumsum = 0
for i in range(0, len(predicted_age)):
    diff = (abs(predicted_age[i] - truth_age[i]) // 5) + 1
    age_cumsum += 100.0/diff


percentage_age = age_cumsum/len(predicted_age)
percentage_gender = accuracy_score(truth_gender, predicted_gender)*100
percentage_race = accuracy_score(truth_race, predicted_race)*100

print("Godine: ", percentage_age)
print("Pol: ", percentage_gender)
print("Rasa: ", percentage_race)
print("Ukupno: ", sum([percentage_age, percentage_gender, percentage_race])/3)

from sklearn.metrics import accuracy_score
import sys
import os

# ------------------------------------------------------------------
# Ovaj fajl ne menjati, da bi automatsko ocenjivanje bilo moguce
if len(sys.argv) > 1:
    VALIDATION_DATASET_PATH = sys.argv[1]
else:
    VALIDATION_DATASET_PATH = '.'+os.path.sep+'dataset'+os.path.sep+'validation'+os.path.sep
# ------------------------------------------------------------------

labeled_age = dict()
labeled_gender = dict()
labeled_race = dict()

with open(VALIDATION_DATASET_PATH+'annotations.csv') as file:
    data = file.read()
    lines = data.split('\n')
    for index, line in enumerate(lines):
        if index == 0:
            continue
        cols = line.split(',')
        if cols and cols[0] == '':
            continue
        image = cols[0].replace('\r', '')
        age = cols[1].replace('\r', '')
        gender = cols[2].replace('\r', '')
        race = cols[3].replace('\r', '')
        labeled_age[str(image)] = int(age)
        labeled_gender[str(image)] = gender
        labeled_race[str(image)] = race
    


# read results file generated by the main.py
results_age = dict()
results_gender = dict()
results_race = dict()

# with open(VALIDATION_DATASET_PATH+'annotations.csv') as file:
with open('result.csv') as file:
    data = file.read()
    lines = data.split('\n')
    for index, line in enumerate(lines):
        cols = line.split(',')
        # if index == 0:
        #     continue
        if cols and cols[0] == '':
            continue
        image = cols[0].replace('\r', '')
        age = cols[1].replace('\r', '')
        gender = cols[2].replace('\r', '')
        race = cols[3].replace('\r', '')

        results_age[str(image)] = int(age)
        results_gender[str(image)] = gender
        results_race[str(image)] = race

# # evaluate how results file matches the labelled samples
def get_comparison(results, labeled_samples):
    truth = []
    predicted = []
    for image in results:
        truth.append(labeled_samples[image])
        predicted.append(results[image])
    return truth, predicted

truth_age, predicted_age = get_comparison(results_age, labeled_age)
truth_gender, predicted_gender = get_comparison(results_gender, labeled_gender)
truth_race, predicted_race = get_comparison(results_race, labeled_race)

age_cumsum = 0
for i in range(0, len(predicted_age)):
    diff = (abs(predicted_age[i] - truth_age[i]) // 5) + 1
    age_cumsum += 100.0/diff


percentage_age = age_cumsum/len(predicted_age)
percentage_gender = accuracy_score(truth_gender, predicted_gender)*100
percentage_race = accuracy_score(truth_race, predicted_race)*100

print("Godine: ", percentage_age)
print("Pol: ", percentage_gender)
print("Rasa: ", percentage_race)
print("Ukupno: ", sum([percentage_age, percentage_gender, percentage_race])/3)

# ------------------------------------------------------------------ # Ovaj fajl ne menjati, da bi automatsko ocenjivanje bilo moguce # ------------------------------------------------------------------ # read results file generated by the main.py # with open(VALIDATION_DATASET_PATH+'annotations.csv') as file: # if index == 0: #     continue # # evaluate how results file matches the labelled samples