/
frecog.py
executable file
·214 lines (161 loc) · 5.69 KB
/
frecog.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
#!/usr/bin/python2.7
import cv2
import os
import numpy as np
import sys
FACE_CASCADE_PATH = "cascades/haarcascade_frontalface_alt.xml"
EYES_CASCADE_PATH = "cascades/haarcascade_eye_tree_eyeglasses.xml"
TRAINING_PATH = "training.txt"
IDENTITIES_PATH = "identities.txt"
Historic_x = 0
Historic_y = 0
Historic_w = 0
Historic_h = 0
###############################################################################
#
# Functions
#
###############################################################################
def read_csv(training_path):
"""
Load the training file, and read each image into the program as a matrix.
Arguments:
training_path: The path to the training file.
scale_size: The size to scale the images to.
"""
trainingFILE = open(training_path, "r")
indexes = []
images = []
for line in trainingFILE:
image_path = line.strip().split(";")[0]
subjectid = line.strip().split(";")[1]
image = cv2.imread(image_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
if (image) is not None:
image = cv2.resize(image, (150,150))
cv2.equalizeHist( image, image)
indexes.append(int(subjectid))
images.append(image)
return indexes, images
def read_identities(identity_path):
identityFile = open(identity_path, "r")
identities = {}
for line in identityFile:
id = line.strip().split(";")[0]
name = line.strip().split(";")[1]
identities[id] = name
return identities
def detect_faces(image):
"""
Find the location of a face, draw a sqare around it, and send the frame to
the facial reconition engine.
Arguments:
image: The image matrix to detect a face from.
"""
faces = []
detected = face_cascade.detectMultiScale(image,scaleFactor= 1.2,
minNeighbors= 2,
minSize=(100,100))
try:
if (detected).all():
for (x,y,w,h) in detected:
faces.append((x,y,w,h))
except AttributeError:
# No face was detected
pass
return faces
def detect_eyes(image):
"""
Find the location of eyes to properly align the face.
Arguments:
image: The image matrix to detect a face from.
"""
eyes = []
detected = eyes_cascade.detectMultiScale(image,scaleFactor= 1.2,
minNeighbors= 2,
minSize=(5,5))
try:
if (detected).all():
for (x,y,w,h) in detected:
eyes.append((x,y,w,h))
except AttributeError:
# No face was detected
pass
return eyes
def learnFace(image, id):
"""
Add the face to the model, the training folder, and the training roster.
Arguments:
image: The face image matrix.
id: The subject id of the person.
"""
numberOfImage = len([name for name in os.listdir('data/'+str(id)+"/") if os.path.isfile(name)])
cv2.imwrite("data/"+str(id)+"/"+str(numberOfImage+1)+".bmp", image)
trainingFile = open(TRAINING_PATH, "a")
trainingFile.write("data/"+str(id)+"/"+str(numberOfImage+1)+".bmp;"+str(id)+"\n")
trainingFile.close()
images.append(image)
indexes.append(id)
faceRecog.train(images, np.array(indexes))
### Load facial recognition
indexes, images = read_csv(TRAINING_PATH)
identities = read_identities(IDENTITIES_PATH)
faceRecog = cv2.createEigenFaceRecognizer()
faceRecog.train(images, np.array(indexes))
### Load facial detection
webcamCapture=cv2.VideoCapture()
webcamCapture.open(0)
face_cascade = cv2.CascadeClassifier(FACE_CASCADE_PATH)
eyes_cascade = cv2.CascadeClassifier(EYES_CASCADE_PATH)
faces = []
i = 0
while True:
retval,retimage=webcamCapture.read() # Load the image
image=retimage.copy()
## Format the raw webcam image.
image = cv2.flip(image,1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#cv2.medianBlur(image, 3)
cv2.equalizeHist( image)
if i%1==0:
faces = detect_faces(image)
if len(faces) > 0:
for (x,y,w,h) in faces:
threshold = 20
if x > Historic_x - threshold and x < Historic_x + threshold:
x = Historic_x
else:
Historic_x = x
if y > Historic_y - threshold and y < Historic_y + threshold:
y = Historic_y
else:
Historic_y = y
if h > Historic_h - threshold and h < Historic_h + threshold:
h = Historic_h
else:
Historic_h = h
if w > Historic_w - threshold and w < Historic_w + threshold:
w = Historic_w
else:
Historic_w = w
## Faces were detected...
face = image[y:y+h,x:x+w]
face = cv2.resize(face, (150,150))
face = cv2.equalizeHist(face)
cv2.rectangle(image, (x,y), (x+w,y+h), 255)
prediction_label = faceRecog.predict(face)
if prediction_label[1] > 3000:
try:
cv2.putText(image,
(identities[str(prediction_label[0])] + " - " +
str(prediction_label[1])),
(x,y-10),
cv2.FONT_HERSHEY_PLAIN, 0.8, 255)
print(identities[str(prediction_label[0])])
except KeyError:
cv2.putText(image, "NAME ERROR" ,
(x,y-10),
cv2.FONT_HERSHEY_PLAIN, 0.8, 255)
print("NAME ERROR")
cv2.imshow("Image_Window", image)
cv2.waitKey(1)
i+=1