-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfrom_photo.py
62 lines (50 loc) · 2.07 KB
/
from_photo.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
# Tutorial: https://youtu.be/535acCxjHCI?list=PLQVvvaa0QuDcDqgpLLJJM15NpIGNfrKY5
import os
import cv2
import face_recognition
KNOWN_FACES_DIR = "known_faces"
UNKNOWN_FACES_DIR = "unknown_faces"
TOLERANCE = 0.5
FRAME_THICKNESS = 1
FONT_THICKNESS = 1
MODEL = "hog"
print("[*] Loading known faces...")
known_faces = []
known_names = []
for name in os.listdir(KNOWN_FACES_DIR):
for filename in os.listdir(f"{KNOWN_FACES_DIR}/{name}"):
image = face_recognition.load_image_file(f"{KNOWN_FACES_DIR}/{name}/{filename}")
encoding = face_recognition.face_encodings(image)[0]
known_faces.append(encoding)
known_names.append(name)
print("[*] Processing unknown faces...")
for filename in os.listdir(UNKNOWN_FACES_DIR):
print(f" {filename}")
image = face_recognition.load_image_file(f"{UNKNOWN_FACES_DIR}/{filename}")
locations = face_recognition.face_locations(image, model=MODEL)
encodings = face_recognition.face_encodings(image, locations)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
for face_encoding, face_location in zip(encodings, locations):
results = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE)
match = None
if True in results:
match = known_names[results.index(True)]
# print(f'Match found: {match}')
top_left = (face_location[3], face_location[0])
bottom_right = (face_location[1], face_location[2])
color = [0, 255, 0]
cv2.rectangle(image, top_left, bottom_right, color, FRAME_THICKNESS)
top_left = (face_location[3], face_location[2])
bottom_right = (face_location[1], face_location[2] + 22)
cv2.rectangle(image, top_left, bottom_right, color, cv2.FILLED)
cv2.putText(
image,
match,
(face_location[3] + 10, face_location[2] + 15),
cv2.FONT_HERSHEY_TRIPLEX,
0.35,
(0, 0, 0),
FONT_THICKNESS,
)
cv2.imshow(filename, image)
cv2.waitKey(10000)