#This file load the dataset fer2013 as arrays. 
import csv
import numpy as np
import cv2
import matplotlib.pyplot as plt


nbrImages = 35887
maxNbrImages = nbrImages
emotions = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Suprise", "Neutral"]

def traitement(a,b,c):  #For testing
	pass
	# arr = strToArray(b)
	# print(a)
	# plt.imshow(arr)
	# plt.show()
	# pass

def strToArray(string):  #Fer2013 provides images as string so it needs to be transformed
	A = []
	lenght = len(string)
	i=0
	nbr = ""

	while i<lenght:
		car = string[i]

		if car != " ":
			nbr += car
		else:
			A.append(int(nbr))
			nbr = ""
		i+=1
	A.append(int(nbr))
	
	A = np.array(A)
	A = np.reshape(A, (48, 48))

	return A



#LOAD DATA AS ARRAY
X = []
Y = []

filename = "data/fer2013.csv"

with open(filename,'r',encoding='utf-8') as file:
	
	csv_reader = csv.reader(file, delimiter=",")
	next(csv_reader)  								#Passe la ligne de titre
	
	i=0
	for row in csv_reader:

		i+=1
		if i>maxNbrImages: break
		
		emotionNbr, stringImage, typeImage = row
		traitement(emotionNbr, stringImage, typeImage)

		X.append(strToArray(stringImage))
		Y.append(emotionNbr)

		print(f"Image {i} sur {nbrImages} chargée", end='\r')

X = np.array(X)
Y = np.array(Y)