-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface recognize.py
More file actions
128 lines (97 loc) · 4.64 KB
/
Copy pathface recognize.py
File metadata and controls
128 lines (97 loc) · 4.64 KB
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
"""
Real-time Face Recognition System
Uses webcam to detect and recognize faces from a known faces dataset
"""
import face_recognition
import cv2
import numpy as np
import os
def load_known_faces():
known_face_encodings = []
known_face_names = []
for file in os.listdir("known_faces"):
if not file.lower().endswith((".jpg", ".jpeg", ".png")):
continue
image_path = os.path.join("known_faces", file)
image = face_recognition.load_image_file(image_path)
encodings = face_recognition.face_encodings(image)
if len(encodings) == 0:
print(f"No face found in {file}")
continue
known_face_encodings.append(encodings[0])
name = os.path.splitext(file)[0]
known_face_names.append(name)
return known_face_encodings, known_face_names
FRAME_SCALE = 0.25
TOLERANCE = 0.5
def main():
# Initialize variables for face detection and recognition
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
# Open webcam (0 = default camera)
video_capture = cv2.VideoCapture(0)
if not video_capture.isOpened():
print("Cannot access webcam")
return
known_face_encodings, known_face_names = load_known_faces()
if len(known_face_encodings) == 0:
print("No known faces found.")
return
# Main loop - continuously capture and process video frames
while True:
success, frame = video_capture.read()
if not success:
print("Failed to read frame.")
break
frame = cv2.flip(frame, 1)
if process_this_frame:
small_frame = cv2.resize(frame, (0, 0), fx=FRAME_SCALE, fy=FRAME_SCALE)
small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(small_frame)
# print(face_locations)
# If at least one face is detected
if len(face_locations) > 0:
# Generate encodings for all detected faces
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
# Loop through each detected face encoding
for face_encoding in face_encodings:
# Compare detected face against all known faces (tolerance 0.5 = sensitivity)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance = TOLERANCE)
# print(matches)
recognized_name = 'Unknown'
# Method 1 (commented): Get first matching face index
# if True in matches:
# first_match_index = matches.index(True)
# recognized_name = known_face_names[first_match_index]
# Method 2 (better): Calculate face distances and find best match
# Compute Euclidean distances between detected face and all known faces
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
recognized_name = known_face_names[best_match_index]
face_names.append(recognized_name)
# Draw rectangles and labels on the ORIGINAL frame (not the small one)
# Scale coordinates back up by 4x because we resized the frame to 1/4
for (top,right,bottom,left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw green rectangle around the face
frame = cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 3)
# Draw filled rectangle behind the name label
frame = cv2.rectangle(frame, (left-2, top-35), (right+2, top), (125, 220, 0), cv2.FILLED)
cv2.putText(frame, name, (left+5, top-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (20, 20, 255),2)
# process_this_frame = not process_this_frame
# Display the resulting video feed
cv2.imshow('cam', frame)
# Exit loop when 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()