File size: 1,531 Bytes
a6161e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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