-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFace Recognition.py
80 lines (58 loc) · 2.45 KB
/
Face Recognition.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
#Face Detection+Recognition
import cv2
import face_recognition
# Load known faces and their names
def load_known_faces():
known_faces = []
known_names = []
# Adding paths to images of known people
image_files = {
"Person A": "Images/A._P._J._Abdul_Kalam.jpg",
"Person B": "Images/Lata Mangeshkar.jpg",
"Person C": "Images/Sachin_Tendulkar.jpg"
}
for name, image_file in image_files.items():
image = face_recognition.load_image_file(image_file)
encodings = face_recognition.face_encodings(image)
if encodings:
encoding = encodings[0]
known_faces.append(encoding)
known_names.append(name)
else:
print(f"No faces found in the image {image_file}. Skipping this file.")
return known_faces, known_names
def recognize_faces(frame, known_faces, known_names):
# Finding all face locations and face encodings in the frame
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_faces, face_encoding)
name = "Unknown"
# Checking if there is a match
if True in matches:
first_match_index = matches.index(True)
name = known_names[first_match_index]
names.append(name)
return face_locations, names
def main():
known_faces, known_names = load_known_faces()
# Initialize video capture (0 for default camera)
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
if not ret:
break
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_locations, names = recognize_faces(rgb_frame, known_faces, known_names)
for (top, right, bottom, left), name in zip(face_locations, names):
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and close windows
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()