forked from ffletcherr/face-recognition-liveness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_facebank.py
76 lines (65 loc) · 2.07 KB
/
create_facebank.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
import csv
import os
import sys
import urllib
from glob import glob
from pathlib import Path
import cv2
import numpy as np
import onnxruntime
from tqdm import tqdm
from facetools import FaceDetection, IdentityVerification
# Create the parser
parser = argparse.ArgumentParser(description="Argument for creating facebank csv file")
# Add the arguments
parser.add_argument(
"--images", metavar="path", type=str, help="the path to the images folder"
)
parser.add_argument(
"--checkpoint",
metavar="path",
type=str,
help="the path to the resnet vggface2 onnx checkpoint",
)
parser.add_argument(
"--output", metavar="path", type=str, help="the path to the output csv file"
)
args = parser.parse_args()
input_path = args.images
checkpoint_path = args.checkpoint
csv_path = args.output
if not os.path.isdir(input_path):
print(f"The path [{input_path}] is not a directory")
sys.exit()
images_list = (
glob(os.path.join(input_path, "*.jpg"))
+ glob(os.path.join(input_path, "*.png"))
+ glob(os.path.join(input_path, "*.jpeg"))
)
if not len(images_list):
print(f"There is not any images in the [{input_path}] path")
sys.exit()
faceDetector = FaceDetection()
if not Path(checkpoint_path).is_file():
print("Downloading the Inception resnet v1 vggface2 onnx checkpoint")
urllib.request.urlretrieve(
"https://github.com/ffletcherr/face-recognition-liveness/releases/download/v0.1/InceptionResnetV1_vggface2.onnx",
Path(checkpoint_path).absolute().as_posix(),
)
resnet = onnxruntime.InferenceSession(
checkpoint_path, providers=["CPUExecutionProvider"]
)
f = open(csv_path, "w")
writer = csv.writer(f)
for image_path in tqdm(images_list):
image = cv2.imread(image_path)
faces, boxes = faceDetector(image)
if not len(faces):
continue
face_arr = faces[0]
face_arr = np.moveaxis(face_arr, -1, 0)
input_arr = np.expand_dims((face_arr - 127.5) / 128.0, 0)
embeddings = resnet.run(["output"], {"input": input_arr.astype(np.float32)})[0]
writer.writerow(embeddings.flatten().tolist())
f.close()