A high-accuracy face recognition (embedding) model exported to ONNX format, ready to run with
onnxruntime.
1import cv2
2import numpy as np
3import onnxruntime as ort
4
5def preprocess(img_path):
6 img = cv2.imread(img_path)
7 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
8 img = cv2.resize(img, (112, 112))
9 img = (img.astype(np.float32) - 127.5) / 128.0
10 return img[np.newaxis, ...] # shape: (1, 112, 112, 3)
11
12sess = ort.InferenceSession("arcface.onnx")
13input_name = sess.get_inputs()[0].name
14output_name = sess.get_outputs()[0].name
15
16emb1 = sess.run([output_name], {input_name: preprocess("face1.jpg")})[0][0]
17emb2 = sess.run([output_name], {input_name: preprocess("face2.jpg")})[0][0]
18
19# Normalize
20emb1 = emb1 / np.linalg.norm(emb1)
21emb2 = emb2 / np.linalg.norm(emb2)
22cosine_sim = np.dot(emb1, emb2)
23print("Cosine similarity:", cosine_sim)