forked from EricSchles/neuralnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacial_recognition.py
More file actions
43 lines (35 loc) · 1.44 KB
/
facial_recognition.py
File metadata and controls
43 lines (35 loc) · 1.44 KB
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
from pybrain.datasets.supervised import SupervisedDataSet
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
import cv2
def loadImage(path):
im = cv2.imread(path)
return flatten(im)
def flatten(x):
result = []
for el in x:
if hasattr(el, "__iter__") and not isinstance(el, basestring):
result.extend(flatten(el))
else:
result.append(el)
return result
if __name__ == "__main__":
t = loadImage('pic/face_2copy.png')
net = buildNetwork(len(t), .03*len(t), 1)
ds = SupervisedDataSet(len(t), 1)
ds.addSample(loadImage('pic/face_2copy.png'),(2,))
ds.addSample(loadImage('pic/image_1copy.png'),(1,))
ds.addSample(loadImage('pic/image_2copy.png'),(1,))
ds.addSample(loadImage('pic/face_5copy.png'),(2,))
trainer = BackpropTrainer(net, ds)
error = 10
iteration = 0
while error > 0.0001:
error = trainer.train()
iteration += 1
print "Iteration: {0} Error {1}".format(iteration, error)
print "\nResult: ", net.activate(loadImage('pic/face_4copy.png'))
print "\nResult: ", net.activate(loadImage('pic/image_9copy.png'))
print "\nResult: ", net.activate(loadImage('pic/image_10copy.png'))
print "\nResult: ", net.activate(loadImage('pic/face_3copy.png'))
print "\nResult: ", net.activate(loadImage('pic/image_15copy.png'))