Spaces:
Build error
Build error
Download face_recognition.py from roseyshi/Face_Detection: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/roseyshi/Face_Detection/resolve/main/face_recognition.py
- Command line
-
hf download hf://spaces/roseyshi/Face_Detection/face_recognition.py
-
curl -L -o face_recognition.py https://huggingface.co/spaces/roseyshi/Face_Detection/resolve/main/face_recognition.py
1.39 kB
| # backend/core/face_recognition.py | |
| import face_recognition | |
| import numpy as np | |
| import pickle | |
| import os | |
| class FaceRecognizer: | |
| def __init__(self, encodings_path='models/face_encodings.pkl'): | |
| self.known_face_encodings = [] | |
| self.known_face_names = [] | |
| self.load_encodings(encodings_path) | |
| def load_encodings(self, path): | |
| """Load pre-computed face encodings""" | |
| if os.path.exists(path): | |
| with open(path, 'rb') as f: | |
| data = pickle.load(f) | |
| self.known_face_encodings = data['encodings'] | |
| self.known_face_names = data['names'] | |
| def recognize_face(self, face_img): | |
| """Recognize a single face""" | |
| if face_img is None or face_img.size == 0: | |
| return None | |
| # Get face encoding | |
| face_locations = face_recognition.face_locations(face_img) | |
| if not face_locations: | |
| return None | |
| face_encoding = face_recognition.face_encodings(face_img, face_locations)[0] | |
| # Compare with known faces | |
| matches = face_recognition.compare_faces( | |
| self.known_face_encodings, | |
| face_encoding | |
| ) | |
| if True in matches: | |
| matched_index = matches.index(True) | |
| return self.known_face_names[matched_index] | |
| return None |