-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstart_face_recon_app.py
179 lines (162 loc) · 6.36 KB
/
start_face_recon_app.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
#!/usr/bin/env python
import sys
import os
import numpy as np
from face_recognition_system.videocamera import VideoCamera
from face_recognition_system.detectors import FaceDetector
import face_recognition_system.operations as op
import cv2
from cv2 import __version__
def get_images(frame, faces_coord, shape):
""" Perfrom transformation on original and face images.
This function draws the countour around the found face given by faces_coord
and also cuts the face from the original image. Returns both images.
"""
if shape == "rectangle":
faces_img = op.cut_face_rectangle(frame, faces_coord)
frame = op.draw_face_rectangle(frame, faces_coord)
faces_img = op.normalize_intensity(faces_img)
faces_img = op.resize(faces_img)
return (frame, faces_img)
def add_person(people_folder, shape):
""" Funtion to add pictures of a person
"""
person_name = input('What is the name of the new person: ').lower()
folder = people_folder + person_name
if not os.path.exists(folder):
input("I will now take 20 pictures. Press ENTER when ready.")
os.mkdir(folder)
video = VideoCamera()
detector = FaceDetector('face_recognition_system/frontal_face.xml')
counter = 1
timer = 0
cv2.namedWindow('Video Feed', cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('Saved Face', cv2.WINDOW_NORMAL)
while counter < 21:
frame = video.get_frame()
face_coord = detector.detect(frame)
if len(face_coord):
frame, face_img = get_images(frame, face_coord, shape)
# save a face every second, we start from an offset '5' because
# the first frame of the camera gets very high intensity
# readings.
if timer % 100 == 5:
cv2.imwrite(folder + '/' + str(counter) + '.jpg',
face_img[0])
print ('Images Saved:' + str(counter))
counter += 1
cv2.imshow('Saved Face', face_img[0])
cv2.imshow('Video Feed', frame)
cv2.waitKey(50)
timer += 5
else:
print ("This name already exists.")
sys.exit()
def recognize_people(people_folder, shape):
""" Start recognizing people in a live stream with your webcam
"""
try:
people = [person for person in os.listdir(people_folder)]
except:
print ("Have you added at least one person to the system?")
sys.exit()
print ("This are the people in the Recognition System:")
for person in people:
print ("-" + person)
print (30 * '-')
print (" POSSIBLE RECOGNIZERS TO USE")
print (30 * '-')
print ("1. EigenFaces")
print ("2. FisherFaces")
print ("3. LBPHFaces")
print (30 * '-')
choice = check_choice()
detector = FaceDetector('face_recognition_system/frontal_face.xml')
if choice == 1:
recognizer = cv2.face.createEigenFaceRecognizer()
threshold = 4000
elif choice == 2:
recognizer = cv2.face.createFisherFaceRecognizer()
threshold = 300
elif choice == 3:
recognizer = cv2.face.createLBPHFaceRecognizer()
threshold = 80
images = []
labels = []
labels_people = {}
for i, person in enumerate(people):
labels_people[i] = person
for image in os.listdir(people_folder + person):
images.append(cv2.imread(people_folder + person + '/' + image, 0))
labels.append(i)
try:
recognizer.train(images, np.array(labels))
except:
print ("\nOpenCV Error: Do you have at least two people in the database?\n")
sys.exit()
video = VideoCamera()
while True:
frame = video.get_frame()
faces_coord = detector.detect(frame, False)
if len(faces_coord):
frame, faces_img = get_images(frame, faces_coord, shape)
for i, face_img in enumerate(faces_img):
if __version__ == "3.1.0":
collector = cv2.face.MinDistancePredictCollector()
recognizer.predict(face_img, collector)
conf = collector.getDist()
pred = collector.getLabel()
else:
pred, conf = recognizer.predict(face_img)
print ("Prediction: " + str(pred))
print ('Confidence: ' + str(round(conf)))
print ('Threshold: ' + str(threshold))
if conf < threshold:
cv2.putText(frame, labels_people[pred].capitalize(),
(faces_coord[i][0], faces_coord[i][1] - 2),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
else:
cv2.putText(frame, "Unknown",
(faces_coord[i][0], faces_coord[i][1]),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1,
cv2.LINE_AA)
cv2.putText(frame, "ESC to exit", (5, frame.shape[0] - 5),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255, 255), 1, cv2.LINE_AA)
cv2.imshow('Video', frame)
if cv2.waitKey(100) & 0xFF == 27:
sys.exit()
def check_choice():
""" Check if choice is good
"""
is_valid = 0
while not is_valid:
try:
choice = int(input('Enter your choice [1-3] : '))
if choice in [1, 2, 3]:
is_valid = 1
else:
print ("'%d' is not an option.\n" % choice)
except ValueError:
print ("%s is not an option.\n" % str(error).split(": ")[1])
return choice
if __name__ == '__main__':
print (30 * '-')
print (" POSSIBLE ACTIONS")
print (30 * '-')
print ("1. Add person to the recognizer system")
print ("2. Start recognizer")
print ("3. Exit")
print (30 * '-')
CHOICE = check_choice()
PEOPLE_FOLDER = "face_recognition_system/people/"
SHAPE = "rectangle"
if CHOICE == 1:
if not os.path.exists(PEOPLE_FOLDER):
os.makedirs(PEOPLE_FOLDER)
add_person(PEOPLE_FOLDER, SHAPE)
elif CHOICE == 2:
os.system("sudo modprobe bcm2835-v4l2") # Required for Raspi-Camera Module
recognize_people(PEOPLE_FOLDER, SHAPE)
elif CHOICE == 3:
sys.exit()