-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathprepare_face_landmarks.py
77 lines (55 loc) · 2.13 KB
/
prepare_face_landmarks.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
77
import argparse
import glob
import os
import numpy as np
import PIL
import tqdm
try:
import pyspng
except ImportError:
pyspng = None
# test with tensorflow-gpu==2.8.0
import tensorflow as tf
from mtcnn import MTCNN
gpus = tf.config.experimental.list_physical_devices("GPU")
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
N_IMGS = 1
def get_file_ext(fname):
return os.path.splitext(fname)[1].lower()
def main(args):
detector = MTCNN()
detect_res_dir = os.path.join(args.data_dir, "detections")
os.makedirs(detect_res_dir, exist_ok=True)
print("\ndetect_res_dir: ", detect_res_dir, "\n")
sorted_f_list = sorted(list(glob.glob(os.path.join(args.data_dir, "*.png"))))
print("\nsorted_f_list: ", len(sorted_f_list), sorted_f_list[:5], "\n")
for i, f_path in tqdm.tqdm(enumerate(sorted_f_list), total=len(sorted_f_list)):
basename = os.path.splitext(os.path.basename(f_path))[0]
if pyspng is not None and get_file_ext(f_path) == ".png":
with open(f_path, "rb") as fin:
img = pyspng.load(fin.read())
else:
img = np.array(PIL.Image.open(f_path))
text_path = f"{detect_res_dir}/{basename}.txt"
result = detector.detect_faces(img)
try:
keypoints = result[0]["keypoints"]
with open(text_path, "w") as f:
for value in keypoints.values():
f.write(f"{value[0]}\t{value[1]}\n")
# print(f"File successfully written: {text_path}")
except:
if i == 0:
mode = "w"
else:
mode = "a"
with open(os.path.join(detect_res_dir, "fail_list.txt"), mode) as fail_f:
fail_f.write(f"{os.path.basename(f_path)}\n")
print("\n[fail] ", os.path.basename(f_path), "\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Get landmarks from images.")
parser.add_argument("--data_dir", type=str, default=None, help="folder for metfaces")
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
main(args)