Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import face_recognition
- import os
- import configparser
- class FaceRecognition:
- def __init__(self):
- config = configparser.ConfigParser()
- config.read(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config', 'config.ini'))
- self.known_face_encodings = []
- self.known_face_names = []
- faces_dir = config['Paths']['faces_dir']
- for filename in os.listdir(os.path.join(os.path.dirname(os.path.dirname(__file__)), '..', faces_dir)):
- if filename.endswith(('.jpg', '.png')):
- image = face_recognition.load_image_file(os.path.join(os.path.dirname(os.path.dirname(__file__)), '..', faces_dir, filename))
- encoding = face_recognition.face_encodings(image)
- if encoding:
- self.known_face_encodings.append(encoding[0])
- self.known_face_names.append(os.path.splitext(filename)[0])
- def recognize_face(self, frame):
- rgb_frame = frame[:, :, ::-1] # Конвертация BGR (OpenCV) в RGB (face_recognition)
- face_locations = face_recognition.face_locations(rgb_frame)
- face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
- worker_name = "Неизвестный"
- face_recognized = False
- for face_encoding in face_encodings:
- matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding)
- if True in matches:
- face_recognized = True
- worker_name = self.known_face_names[matches.index(True)]
- break
- return face_locations, worker_name, face_recognized
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement