-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data.py
83 lines (73 loc) · 2.61 KB
/
get_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import sys
import numpy as np
import tkinter as tk
import pandas as pd
from tkinter.filedialog import askopenfilename
#Module takes 1 parameter - csv_file path
#Module returns 2 lists - train_data and test_data
#Each list has a tuple as an element
#Each tuple contains (Image, Emotion id)
#Each image is 48x48 in size
#35887 Rows. 3 columns.
#Col 1 Emotion
#Col 2 Pixels
#Col 3 Usage
#Total pixels of 1 image = 2304
#Each image is 48x48
# 0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral
#28709 training images
#7178 test images
#Flip image is an augmentation technique where we invert the image vertically and add it to the database
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def flip_image(pixels_2d):
flipped_imge = np.fliplr(pixels_2d)
return flipped_imge
def get_file():
root = tk.Tk()
root.withdraw()
return askopenfilename()
def convert_image(pixels_1d):
pixels_2d = np.reshape(pixels_1d, (48, 48))
pixels_2d = np.array(pixels_2d, dtype=np.uint8)
return pixels_2d
def split_train_test(filepath):
train_data = []
test_data = []
csv_file = pd.read_csv(filepath)
for i in csv_file.index:
usage = csv_file.iloc[i][2]
if usage == "Training":
pixels_1d = csv_file.iloc[i][1].split(' ')
pixels_2d = convert_image(pixels_1d)
emotion = csv_file.iloc[i][0]
flipped_image = flip_image(pixels_2d)
flipped_set = (flipped_image, emotion)
current_set = (pixels_2d, emotion)
train_data.append(current_set)
train_data.append(flipped_set)
else:
pixels_1d = csv_file.iloc[i][1].split(' ')
pixels_2d = convert_image(pixels_1d)
emotion = csv_file.iloc[i][0]
flipped_image = flip_image(pixels_2d)
flipped_set = (flipped_image, emotion)
current_set = (pixels_2d, emotion)
test_data.append(current_set)
test_data.append(flipped_set)
return train_data, test_data
def get_file_path():
try:
with open(resource_path('Dataset/fer2013.csv'), 'r'):
filepath = resource_path('Dataset/fer2013.csv')
print("<-------------------Dataset Found. Splitting And Cleaning Data------------------->")
except FileNotFoundError:
print("<-------------------Dataset Not Found------------------->")
filepath = get_file()
train_data, test_data = split_train_test(filepath)
return train_data, test_data