Untitled
unknown
plain_text
a year ago
1.6 kB
12
Indexable
import cv2 as cv
from mtcnn.mtcnn import MTCNN
from keras_facenet import FaceNet
import numpy as np
import pickle
detector = MTCNN()
embedder = FaceNet()
model_path = 'svm_model_160x160.pkl'
with open(model_path, 'rb') as f:
model = pickle.load(f)
with open('label_encoder.pkl', 'rb') as f:
encoder = pickle.load(f)
def get_embedding(face_img):
face_img = face_img.astype('float32')
face_img = np.expand_dims(face_img, axis=0)
yhat = embedder.embeddings(face_img)
return yhat[0]
image_path = 'TestFolder/Test/dd07a081161bb345ea0a14.jpg'
frame = cv.imread(image_path)
frame = cv.resize(frame, (1280, 600))
rgb_frame = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
results = detector.detect_faces(rgb_frame)
for result in results:
x, y, width, height = result['box']
x, y, width, height = abs(x), abs(y), abs(width), abs(height)
face_img = frame[y:y + height, x:x + width]
face_img = cv.resize(face_img, (160, 160))
test_im = get_embedding(face_img)
test_im = [test_im]
ypreds = model.predict(test_im)
proba = model.predict_proba(test_im)
label = encoder.inverse_transform(ypreds)[0]
cv.rectangle(frame, (x, y), (x + width, y + height), (0, 255, 0), 2)
prob = proba[0][ypreds[0]]
if prob < 0.6:
label_text = "KhongTheNhanDang"
else:
label_text = f"{label} ({prob * 100:.2f}%)"
cv.putText(frame, label_text, (x, y - 10), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv.imshow('Image', frame)
cv.waitKey(0)
cv.destroyAllWindows()Editor is loading...
Leave a Comment