-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize.py
215 lines (177 loc) · 6.61 KB
/
recognize.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
import argparse
import pickle
from collections import Counter
from pathlib import Path
import face_recognition
from PIL import Image, ImageDraw
import cv2
import os
import time
DEFAULT_ENCODINGS_PATH = Path("output/encodings.pkl")
BOUNDING_BOX_COLOR = "blue"
TEXT_COLOR = "white"
# Create directories if they don't already exist
Path("training").mkdir(exist_ok=True)
Path("output").mkdir(exist_ok=True)
Path("validation").mkdir(exist_ok=True)
parser = argparse.ArgumentParser(description="Recognize faces in an image")
parser.add_argument("--train", action="store_true", help="Train on input data")
parser.add_argument(
"--validate", action="store_true", help="Validate trained model"
)
parser.add_argument(
"--test", action="store_true", help="Test the model with an unknown image"
)
parser.add_argument(
"-m",
action="store",
default="hog",
choices=["hog", "cnn"],
help="Which model to use for training: hog (CPU), cnn (GPU)",
)
parser.add_argument(
"-f", action="store", help="Path to an image with an unknown face"
)
parser.add_argument("--live", action="store_true", help="Use live video feed for recognition")
args = parser.parse_args()
def encode_known_faces(
model: str = "hog", encodings_location: Path = DEFAULT_ENCODINGS_PATH
) -> None:
"""
Loads images in the training directory and builds a dictionary of their
names and encodings.
"""
names = []
encodings = []
for filepath in Path("training").glob("*/*"):
# wait_for_file(filepath)
print(filepath)
name = filepath.parent.name
image = face_recognition.load_image_file(filepath)
face_locations = face_recognition.face_locations(image, model=model)
face_encodings = face_recognition.face_encodings(image, face_locations)
for encoding in face_encodings:
names.append(name)
encodings.append(encoding)
name_encodings = {"names": names, "encodings": encodings}
with encodings_location.open(mode="wb") as f:
pickle.dump(name_encodings, f)
def recognize_faces(
image_location: str,
model: str = "hog",
encodings_location: Path = DEFAULT_ENCODINGS_PATH,
) -> None:
"""
Given an unknown image, get the locations and encodings of any faces and
compares them against the known encodings to find potential matches.
"""
with encodings_location.open(mode="rb") as f:
loaded_encodings = pickle.load(f)
input_image = face_recognition.load_image_file(image_location)
input_face_locations = face_recognition.face_locations(
input_image, model=model
)
input_face_encodings = face_recognition.face_encodings(
input_image, input_face_locations
)
pillow_image = Image.fromarray(input_image)
draw = ImageDraw.Draw(pillow_image)
for bounding_box, unknown_encoding in zip(
input_face_locations, input_face_encodings
):
name = _recognize_face(unknown_encoding, loaded_encodings)
if not name:
name = "Unknown"
# print(name)
_display_face(draw, bounding_box, name)
del draw
pillow_image.show()
def _recognize_face(unknown_encoding, loaded_encodings):
"""
Given an unknown encoding and all known encodings, find the known
encoding with the most matches.
"""
boolean_matches = face_recognition.compare_faces(
loaded_encodings["encodings"], unknown_encoding
)
votes = Counter(
name
for match, name in zip(boolean_matches, loaded_encodings["names"])
if match
)
if votes:
return votes.most_common(1)[0][0]
def _display_face(draw, bounding_box, name):
"""
Draws bounding boxes around faces, a caption area, and text captions.
"""
top, right, bottom, left = bounding_box
draw.rectangle(((left, top), (right, bottom)), outline=BOUNDING_BOX_COLOR)
text_left, text_top, text_right, text_bottom = draw.textbbox(
(left, bottom), name
)
draw.rectangle(
((text_left, text_top), (text_right, text_bottom)),
fill=BOUNDING_BOX_COLOR,
outline=BOUNDING_BOX_COLOR,
)
draw.text(
(text_left, text_top),
name,
fill=TEXT_COLOR,
)
def validate(model: str = "hog"):
"""
Runs recognize_faces on a set of images with known faces to validate
known encodings.
"""
for filepath in Path("validation").rglob("*"):
if filepath.is_file():
recognize_faces(
image_location=str(filepath.absolute()), model=model
)
def recognize_faces_live(model: str = "hog", encodings_location: Path = DEFAULT_ENCODINGS_PATH,
resize_factor: float = 0.5, skip_frames: int = 2) -> None:
"""
Recognize faces from live video feed using the webcam.
"""
with encodings_location.open(mode="rb") as f:
loaded_encodings = pickle.load(f)
video_capture = cv2.VideoCapture(0) # Use the default webcam (you can change the index if you have multiple)
frame_count = 0
while True:
ret, frame = video_capture.read()
# Resize frame for faster processing
frame = cv2.resize(frame, None, fx=resize_factor, fy=resize_factor)
frame_count += 1
if frame_count % skip_frames == 0:
input_face_locations = face_recognition.face_locations(frame, model=model)
input_face_encodings = face_recognition.face_encodings(frame, input_face_locations)
for bounding_box, unknown_encoding in zip(input_face_locations, input_face_encodings):
name = _recognize_face(unknown_encoding, loaded_encodings)
if not name:
name = "Unknown"
_display_face_cv(frame, bounding_box, name)
print(name)
cv2.imshow("Video Feed", frame)
# Break the loop if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
def _display_face_cv(frame, bounding_box, name):
top, right, bottom, left = bounding_box
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
# Update your if __name__ == "__main__" block
if __name__ == "__main__":
if args.train:
encode_known_faces(model=args.m)
if args.validate:
validate(model=args.m)
if args.test:
recognize_faces(image_location=args.f, model=args.m)
if args.live:
recognize_faces_live(model=args.m)