-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (45 loc) · 1.88 KB
/
main.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
import face_recognition
import cv2
import os
KNOWN_FACES_DIR = "images/known_faces"
TOLERANCE = 0.6
FRAME_THICKNESS = 2
FONT_THICKNESS = 1
MODEL = "cnn"
video = cv2.VideoCapture(0)
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("Loading finished.\n")
print("Starting video capture.")
print("Detection started.\n")
while True:
ret, frame = video.read()
frame = cv2.flip(frame, 1)
locations = face_recognition.face_locations(frame, model=MODEL)
encodings = face_recognition.face_encodings(frame, locations)
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)]
top_left = (face_location[3], face_location[0])
bottom_right = (face_location[1], face_location[2])
color = [0, 255, 0]
cv2.rectangle(frame, top_left, bottom_right, color, FRAME_THICKNESS)
top_left = (face_location[3], face_location[2])
bottom_right = (face_location[1], face_location[2]+20)
cv2.rectangle(frame, top_left, bottom_right, color, cv2.FILLED)
cv2.putText(frame, match, (face_location[3]+10, face_location[2]+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), FONT_THICKNESS)
cv2.imshow(filename, frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print("Video capture stopped. Please wait a few moments for the process to finish.")
video.release()
cv2.destroyAllWindows()