-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfacial_recognition.py
63 lines (50 loc) · 1.92 KB
/
facial_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
import face_recognition
import os
import cv2
import numpy as np
class FaceRecognition:
def __init__(self):
self.known_faces = []
self.known_face_names = []
def encode_faces(self):
for image in os.listdir('pictures'):
face_image = face_recognition.load_image_file(f'pictures/{image}')
face_encodings = face_recognition.face_encodings(face_image)[0]
self.known_faces.append(face_encodings)
self.known_face_names.append(image)
print(self.known_face_names)
def recognize_face(self, frame):
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(self.known_faces, face_encoding)
name = 'unknown'
confidence = 'unknown'
face_distances = face_recognition.face_distance(self.known_faces, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = self.known_face_names[best_match_index]
return name
face_names.append(name)
# fr = FaceRecognition()
# fr.encode_faces()
# video = cv2.VideoCapture('video3.mp4')
# if not video.isOpened():
# print("Error: Unable to open camera.")
# exit()
# while True:
# ret, frame = video.read()
# if not ret:
# print("Error: Unable to capture frame.")
# break
# name = fr.recognize_face(frame)
# if name:
# print(f"Detected: {name}")
# cv2.imshow('Frame', frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# video.release()
# cv2.destroyAllWindows()