-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedding.py
281 lines (216 loc) · 8.47 KB
/
embedding.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from __future__ import annotations
from abc import ABC, abstractmethod
from pathlib import Path
import numpy as np
import cv2
from sklearn import svm
class EmbedderContext:
_state = None #current state of state machine
_svm = None
_embedder = None
_labels = [] #labels used in svm
_embeddings = [] #features used in our svm
_reqUpdate = True #determines if we need to normalize our data aigan
_resultProb = 0.0
profileDir = Path("./profiles/")
_profiles = []
_currentProfile = None
def __init__(self, embedderPath) -> None:
if not self.profileDir.exists():
self.profileDir.mkdir()
print("Loading embedder model... ")
self.embedder = cv2.dnn.readNetFromTorch(embedderPath)
print("Loading vector machine...")
self.svm = svm.SVC(kernel="rbf", probability=True)
#first state is loading profiles
self.setState(LoadProfilesState())
self.process()
def setState(self, state: EmbedderState):
self._state = state
self._state.ctx = self
def process(self, frame=None):
self._state.process(frame)
@property
def resultProb(self):
"""Returns the probability of the current frame being the current face, estimated by the Analyse State"""
return self._resultProb
@property
def uLabels(self):
"""Returns labels without dublicates"""
return np.unique(self._labels)
@property
def svm(self):
"""Returns the svm instance we are training"""
return self._svm
@svm.setter
def svm(self, instance):
if instance:
self._svm = instance
else:
print("SVM cannot be null")
@property
def embedderPath(self):
"""Returns the path to the embedder model"""
return self._embedderPath
@property
def embedder(self):
"""Returns the embedder instance used to extract features"""
return self._embedder
@embedder.setter
def embedder(self, instance):
if instance:
self._embedder = instance
else:
print("Embedder cannot be null")
@property
def profiles(self):
"""Returns array of profiles currently loaded"""
return self._profiles
@property
def currentProfile(self):
"""Returns the current profile recognized"""
return self._currentProfile
@currentProfile.setter
def currentProfile(self, name):
"""Sets the current profile to the one with the given name"""
if not self._currentProfile == None and name == self._currentProfile.name:
return
if not self.__getProfileByName(name):
newprofile = Profile(name, self.profileDir.joinpath(name))
self.addProfile(newprofile)
self._currentProfile = newprofile
#since we added new profiles, also set the flag to retrain our model
self._reqUpdate = True
print("Profile {} added and selected".format(name))
else:
self._currentProfile = self.__getProfileByName(name)
print("Profile {} selected".format(name))
self.setState(CollectingSamplesState())
def addProfile(self, profile):
if not self.__getProfileByName(profile.name):
self.profiles.append(profile)
else:
print("Profile already exists")
def __getProfileByName(self, name):
return next((d for d in self.profiles if d.name == name), None)
class EmbedderState(ABC):
@property
def ctx(self) -> EmbedderContext:
return self._ctx
@ctx.setter
def ctx(self, context: EmbedderContext) -> None:
self._ctx = context
@abstractmethod
def process(self, frame) -> None:
pass
class LoadProfilesState(EmbedderState):
"""
This state loads profiles according to the folders found in the profiles directory.
Also instantly recognizes how many samples these Profiles alsready have collected.
Next State: CollectingSamplesState
"""
def process(self, frame) -> None:
print("Loading profiles...")
try:
for path in self.ctx.profileDir.iterdir():
if path.is_dir():
images = [img for img in self.ctx.profileDir.joinpath(path.name).iterdir()]
profile = Profile(path.name, path, images)
self.ctx.addProfile(profile)
except:
print("Could not load profiles")
profileCount = len(self.ctx.profiles)
print("Loaded {} profiles".format(profileCount))
self.ctx.setState(CollectingSamplesState())
class CollectingSamplesState(EmbedderState):
"""
Collects missing samples if profile has sufficient data to use for training.
Next State: NormaliseState
"""
def process(self, frame) -> None:
if not self.ctx.currentProfile.hasEnoughData():
try:
count = self.ctx.currentProfile.addFrame(frame)
print("Collected {}/{} samples".format(count,
self.ctx.currentProfile.sampleSize))
except:
print("Error while writing frame")
else:
print("Enough samples collected.")
#skip normalise states if we dont have new samples
if (self.ctx._reqUpdate):
self.ctx.setState(NormaliseState())
else:
self.ctx.setState(TrainSvmState())
class NormaliseState(EmbedderState):
"""
This state will turn collected samples into 128-d vector using embeddor model which is then used for training.
Next state: TrainSvmState
"""
def process(self, frame) -> None:
print("Normalising samples...")
dataset = []
for idx, profile in enumerate(self.ctx.profiles):
for fid, file in enumerate(profile.data):
#extract frame from sample.png
roiFrame = cv2.imread(str(file))
frameBlob = cv2.dnn.blobFromImage(roiFrame, 1 / 255.0, (96, 96), (0, 0, 0))
#give sample into model to convert
self.ctx.embedder.setInput(frameBlob)
#retrieve the 128-d vec result of the sample
frameVec = self.ctx.embedder.forward().flatten()
dataset.append((idx, frameVec))
print("Normalised sample {}[{}/{}]".format(profile.name, fid+1, profile.sampleSize))
(Y, X) = zip(*dataset)
self.ctx._labels = Y
self.ctx._embeddings = X
self.ctx._reqUpdate = False
self.ctx.setState(TrainSvmState())
class TrainSvmState(EmbedderState):
"""
This state will train the SVM with the collected and normalised sampledata/profile labels.
Next state: AnalyseState
"""
def process(self, frame) -> None:
if (len(self.ctx.uLabels) >= 2):
#using support vector machines (svm) provided by sklearn to train svm instance with our data
self.ctx.svm.fit(self.ctx._embeddings, self.ctx._labels)
print("SVM data fitted")
self.ctx.setState(AnalyseState())
else:
print("Not enough profiles to train SVM")
class AnalyseState(EmbedderState):
"""
This state will analyse the current frame and try to identify the current face.
"""
def process(self, frame) -> None:
#get the 128-d fec of current frame
frameBlob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (96, 96), (0, 0, 0))
self.ctx.embedder.setInput(frameBlob)
frameVec = self.ctx.embedder.forward()
#run it through our model
predicitons = self.ctx.svm.predict_proba(frameVec)[0]
guessedIndex = np.argmax(predicitons)
self.ctx.currentProfile = self.ctx.profiles[guessedIndex].name
self.ctx._resultProb = predicitons[guessedIndex]
class Profile:
def __init__(self, name, dir, data=[], sampleSize=10):
self.name = name
self.data = data
self.sampleSize = sampleSize
self.dir = dir
if not dir.exists():
dir.mkdir()
def __repr__(self):
return "({}, {}, {})".format(self.name, self.dir, len(self.data))
def hasEnoughData(self):
return len(self.data) >= self.sampleSize
def addFrame(self, frame):
val = len(self.data)
if self.hasEnoughData():
return val
name = "sample{}.png".format(val)
path = self.dir.joinpath(name + "/")
cv2.imwrite(str(path), frame)
self.data.append(path)
return val