forked from AymanZaki/Facial-Expression-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFER2013_Input.py
124 lines (116 loc) · 3.98 KB
/
FER2013_Input.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import csv
import numpy as np
import tensorflow as tf
from PIL import Image
from numpy import array
from scipy.misc import toimage
from resizeimage import resizeimage
class FER2013_Input:
Training_labels = []
Training_Images = []
Validation_labels = []
Validation_Images = []
Testing_labels = []
Testing_Images = []
Batch_Size = 128
path = ""
def __init__(self, path):
self.path = path
[self.Training_labels, self.Training_Images] = self.FER2013_Training_Set()
[self.Validation_labels, self.Validation_Images] = self.FER2013_Validation_Set()
[self.Testing_labels, self.Testing_Images] = self.FER2013_Testing_Set()
def FER2013_Training_Set (self):
path = self.path + 'FER2013-Training.csv'
Training_labels = []
Training_Images = []
with open(path) as csvfile:
readCSV = csv.reader(csvfile, delimiter = ',')
#row[0] = Labels, row[1] = Image Pixels
for row in readCSV:
Label = [0] * 7
Label[int(row[0])] = 1
Training_labels.append(Label)
image = row[1].split()
#Cast strings to integer
image = [int(i) for i in image]
#Convert 1D array to 2D array 48x48
Image48x48 = np.reshape(image, (48, 48))
#Convert 2D array to Image
Image42x42 = Image.fromarray(np.uint8(Image48x48))
#Resize Image to 42x42
Image42x42 = resizeimage.resize_contain(Image42x42, [42, 42])
#Convert Image to 2D array
Image42x42 = np.uint8(Image42x42.convert('L'))
Training_Images.append(Image42x42)
return Training_labels, Training_Images
def FER2013_Validation_Set (self):
Validation_labels = []
Validation_Images = []
path = self.path + 'FER2013-Validation.csv'
with open(path) as csvfile:
readCSV = csv.reader(csvfile, delimiter = ',')
#row[0] = Labels, row[1] = Image Pixels
for row in readCSV:
Label = [0] * 7
Label[int(row[0])] = 1
Validation_labels.append(Label)
image = row[1].split()
#Cast strings to integer
image = [int(i) for i in image]
#Convert 1D array to 2D array 48x48
Image48x48 = np.reshape(image, (48, 48))
#Convert 2D array to Image
Image42x42 = Image.fromarray(np.uint8(Image48x48))
#Resize Image to 42x42
Image42x42 = resizeimage.resize_contain(Image42x42, [42, 42])
#Convert Image to 2D array
Image42x42 = np.uint8(Image42x42.convert('L'))
Validation_Images.append(Image42x42)
return Validation_labels, Validation_Images
def FER2013_Testing_Set (self):
Testing_labels = []
Testing_Images = []
path = self.path + 'FER2013-Testing.csv'
with open(path) as csvfile:
readCSV = csv.reader(csvfile, delimiter = ',')
#row[0] = Labels, row[1] = Image Pixels
c=0
for row in readCSV:
Label = [0] * 7
Label[int(row[0])] = 1
Testing_labels.append(Label)
image = row[1].split()
#Cast strings to integer
image = [int(i) for i in image]
#Convert 1D array to 2D array 48x48
Image48x48 = np.reshape(image, (48, 48))
#Convert 2D array to Image
Image42x42 = Image.fromarray(np.uint8(Image48x48))
#Resize Image to 42x42
Image42x42 = resizeimage.resize_contain(Image42x42, [42, 42])
#Convert Image to 2D array
Image42x42 = np.uint8(Image42x42.convert('L'))
Testing_Images.append(Image42x42)
c=c+1
if c>=400:
break;
return Testing_labels, Testing_Images
#Batch_Number 0 based
def Get_batch(self, Batch_Number, Mode):
begin = Batch_Number * self.Batch_Size
end = begin + self.Batch_Size
Labels = []
Images = []
if(Mode == 'Training'):
end = min(end, len(self.Training_labels) + 1)
Labels = self.Training_labels[begin:end]
Images = self.Training_Images[begin:end]
elif (Mode == 'Validation'):
end = min(end, len(self.Validation_labels) + 1)
Labels = self.Validation_labels[begin:end]
Images = self.Validation_Images[begin:end]
elif (Mode == 'Testing'):
end = min(end, len(self.Testing_labels) + 1)
Labels = self.Testing_labels[begin:end]
Images = self.Testing_Images[begin:end]
return Labels, Images