Spaces:
Sleeping
Sleeping
Download FaceNet.py from Hoanha2101/FPTCheckInFastAPI: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/Hoanha2101/FPTCheckInFastAPI/resolve/main/FaceNet.py
- Command line
-
hf download hf://spaces/Hoanha2101/FPTCheckInFastAPI/FaceNet.py
-
curl -L -o FaceNet.py https://huggingface.co/spaces/Hoanha2101/FPTCheckInFastAPI/resolve/main/FaceNet.py
1.53 kB
| import numpy as np | |
| import tensorflow as tf | |
| from keras_facenet import FaceNet | |
| import PIL | |
| import pickle | |
| model_FV = FaceNet().model | |
| def img_to_encoding(image_path, model): | |
| img = tf.keras.preprocessing.image.load_img(image_path, target_size=(400,400)) | |
| img = np.around(np.array(img) / 255.0, decimals=12) | |
| x_train = np.expand_dims(img, axis=0) | |
| embedding = model.predict_on_batch(x_train) | |
| return embedding / np.linalg.norm(embedding, ord=2) | |
| def verifyFace(encoding, vector_compare, model): | |
| dist = np.linalg.norm(encoding - vector_compare, ord=2) | |
| door_open = dist < 0.7 | |
| return dist, encoding, door_open | |
| # import numpy as np | |
| # import insightface | |
| # import cv2 | |
| # # Khởi tạo model – chạy được cả trên CPU và GPU | |
| # model_FV = insightface.app.FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider']) | |
| # model_FV.prepare(ctx_id=0) # ctx_id=0 nếu có GPU, -1 nếu CPU | |
| # def img_to_encoding(image_path, model): | |
| # img = cv2.imread(image_path) | |
| # if img is None: | |
| # raise ValueError("Không thể đọc ảnh từ đường dẫn.") | |
| # faces = model.get(img) | |
| # if not faces: | |
| # raise ValueError("Không tìm thấy khuôn mặt trong ảnh.") | |
| # embedding = faces[0]['embedding'] | |
| # return embedding / np.linalg.norm(embedding) | |
| # def verifyFace(encoding, vector_compare, model): | |
| # dist = np.linalg.norm(encoding - vector_compare, ord=2) | |
| # door_open = dist < 0.7 # Ngưỡng tùy chỉnh được | |
| # return dist, encoding, door_open | |