Advertisement
Codymawerick

Untitled

Jun 17th, 2025
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. import face_recognition
  2. import os
  3. import configparser
  4.  
  5. class FaceRecognition:
  6.     def __init__(self):
  7.         config = configparser.ConfigParser()
  8.         config.read(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config', 'config.ini'))
  9.         self.known_face_encodings = []
  10.         self.known_face_names = []
  11.         faces_dir = config['Paths']['faces_dir']
  12.         for filename in os.listdir(os.path.join(os.path.dirname(os.path.dirname(__file__)), '..', faces_dir)):
  13.             if filename.endswith(('.jpg', '.png')):
  14.                 image = face_recognition.load_image_file(os.path.join(os.path.dirname(os.path.dirname(__file__)), '..', faces_dir, filename))
  15.                 encoding = face_recognition.face_encodings(image)
  16.                 if encoding:
  17.                     self.known_face_encodings.append(encoding[0])
  18.                     self.known_face_names.append(os.path.splitext(filename)[0])
  19.  
  20.     def recognize_face(self, frame):
  21.         rgb_frame = frame[:, :, ::-1]  # Конвертация BGR (OpenCV) в RGB (face_recognition)
  22.         face_locations = face_recognition.face_locations(rgb_frame)
  23.         face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
  24.  
  25.         worker_name = "Неизвестный"
  26.         face_recognized = False
  27.         for face_encoding in face_encodings:
  28.             matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding)
  29.             if True in matches:
  30.                 face_recognized = True
  31.                 worker_name = self.known_face_names[matches.index(True)]
  32.                 break
  33.         return face_locations, worker_name, face_recognized
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement