forked from CyberKrish2210/Attendance-Project-Flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recognizer.py
51 lines (40 loc) · 1.81 KB
/
face_recognizer.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
import cv2
import face_recognition
import os
def detect_and_match_face(video_path, image_locations):
# Load the video
cap = cv2.VideoCapture(video_path)
# Initialize face recognition
known_face_encodings = []
# Load images and encode faces from the list of image locations
for image_location in image_locations:
image = face_recognition.load_image_file(image_location)
encoding = face_recognition.face_encodings(image)[0]
known_face_encodings.append(encoding)
while True:
ret, frame = cap.read()
if not ret:
break
# Find face locations and encodings in the current frame
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
# Loop through each face in the current frame
for face_encoding in face_encodings:
# Check if the face matches any known face
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
# If a match is found, return the index of the first match
if True in matches:
first_match_index = matches.index(True)
return first_match_index
# Release the video capture
cap.release()
# If no face is found, return -1
return -1
# Example usage
'''video_path = r'C:/Users/yashp/PycharmProjects/Face_Recognition_Flask/uploaded_video.mp4'
image_locations = ['C:/Users/yashp/PycharmProjects/Face_Recognition_Flask/images/Yash.jpeg', 'C:/Users/yashp/PycharmProjects/Face_Recognition_Flask/images/YashT.jpeg']
index = detect_and_match_face(video_path, image_locations)
print(f'It is {image_locations[index]}')
# Print the index value
print(f'Index of matched face: {index}')
'''