forked from mohamed-elsayed-mohamed/Face-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
133 lines (93 loc) · 3.5 KB
/
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
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
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# Created by Mohamed Elsayed
import numpy as np, os
from scipy import misc
from sklearn.utils import shuffle
(TrainingData, TrainingLables, start) = ([], [], 0)
(TestingData, TestingLables, startT) = ([], [], 0)
def LoadTrainingData(Dir, Img_Shape):
(Images, Lbls, Labels, ID, NClasses) = ([], [], [], 0, 0)
for(_, Dirs, _) in os.walk(Dir):
Dirs = sorted(Dirs)
for SubDir in Dirs:
SubjectPath = os.path.join(Dir, SubDir)
for FileName in os.listdir(SubjectPath):
path = SubjectPath + "/" + FileName
Img = misc.imread(path, mode='L')
#print Img.shape
(height, width) = Img.shape
if(width != Img_Shape[0] or height != Img_Shape[1]):
Img = Img.resize((Img_Shape[0], Img_Shape[1]))
Images.append(Img)
Lbls.append(int(ID))
NClasses += 1
ID += 1
Images, Lbls = shuffle(Images, Lbls)
Images = np.asarray(Images, dtype='float32').reshape([-1, Img_Shape[0], Img_Shape[1], 1]) /255
#print "Classes: " + str(NClasses)
for label in Lbls:
Labels.append(Categorical([label], NClasses)[0])
return (Images, np.asarray(Labels))
def LoadTestingData(Dir, Img_Shape):
(Images, Labels, Names, Classes, Paths, ID, NClasses) = ([], [], [], [], [], 0, 0)
for(_, Dirs, _) in os.walk(Dir):
Dirs = sorted(Dirs)
for SubDir in Dirs:
SubjectPath = os.path.join(Dir, SubDir)
for FileName in os.listdir(SubjectPath):
path = SubjectPath + "/" + FileName
Img = misc.imread(path, mode='L')
Paths.append(path)
#print Img.shape
(height, width) = Img.shape
if(width != Img_Shape[0] or height != Img_Shape[1]):
Img = Img.resize((Img_Shape[0], Img_Shape[1]))
Images.append(Img)
Labels.append(int(ID))
Names.append(SubDir)
Classes.append(SubDir)
NClasses += 1
ID += 1
Images = np.asarray(Images, dtype='float32').reshape([-1, Img_Shape[0], Img_Shape[1], 1]) /255
lbls = []
for label in Labels:
lbls.append(Categorical([label], NClasses)[0])
return (Images, lbls, np.asarray(Names), np.asarray(Classes), np.asarray(Paths))
def Categorical(y, NClasses):
y = np.asarray(y, dtype='int32')
if not NClasses:
NClasses = np.max(y)+1
Y = np.zeros((len(y), NClasses))
for i in range(len(y)):
Y[i, y[i]] = 1.
return Y
def nextBatch(batchSize):
global start
end = start + batchSize
#print "Start: " + str(start)
#print "End: " + str(end)
if(end > len(TrainingData)):
X,Y = TrainingData[start:], TrainingLables[start:]
start = 0
return X, Y
(X, Y) = (TrainingData[start: end], TrainingLables[start: end])
start = end
#X = X.reshape(len(X), X[0].size)
if (end == len(TrainingData)):
start = 0
return X, Y
def nextTestBatch(batchSize):
global startT
end = startT + batchSize
#print "Start: " + str(start)
#print "End: " + str(end)
if(end > len(TestingData)):
X,Y = TestingData[startT:], TestingLables[startT:]
startT = 0
return X, Y
(X, Y) = (TestingData[startT: end], TestingLables[startT: end])
startT = end
if(end == len(TestingData)):
startT = 0
#X = X.reshape(len(X), X[0].size)
return X, Y