-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathprepare_dataset.py
85 lines (78 loc) · 2.84 KB
/
prepare_dataset.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
78
79
80
81
82
83
84
85
from util import (get_data_from_id,
read_kpt_file)
import glob
import os
import numpy as np
from skimage.io import (imread,
imsave)
from skimage.transform import resize
root_dir = os.environ['DIR_3DFAW']
def prepare_train():
ids = glob.glob("%s/train_img/*.jpg" % root_dir)
ids = [os.path.basename(id_).replace(".jpg","") for id_ in ids ]
y_keypts, z_keypts = get_keypts_from_ids(ids, "train")
np.savez(file="%s/train" % root_dir,
y_keypts=y_keypts,
z_keypts=z_keypts)
def get_keypts_from_ids(ids, mode):
y_keypts = []
z_keypts = []
x_keypts = []
meta = []
for k, id_ in enumerate(ids):
print("%i / %i" % (k, len(ids)))
_,b,c = get_data_from_id(root=root_dir, mode=mode, id_=id_)
# a is f64, let's make it uint8 to save some space.
#a = (a*256.).astype("uint8")
#imgs.append(a)
y_keypts.append(b.astype("float32"))
z_keypts.append(c.astype("float32"))
#imgs = np.asarray(imgs)
y_keypts = np.asarray(y_keypts)
z_keypts = np.asarray(z_keypts)
return y_keypts, z_keypts
def prepare_valid():
ids = []
with open("%s/list_valid_test.txt" % root_dir) as f:
for line in f:
line = line.rstrip().split(",")
if line[1] == "valid":
ids.append(line[0])
y_keypts, z_keypts = get_keypts_from_ids(ids, "valid")
np.savez(file="%s/valid" % root_dir,
y_keypts=y_keypts,
z_keypts=z_keypts,
ids=ids)
def prepare_test():
ids = []
orientations = []
with open("%s/list_valid_test.txt" % root_dir) as f:
for line in f:
line = line.rstrip().split(",")
if line[1] == "test":
ids.append(line[0])
orientations.append(line[2])
y_keypts, z_keypts = get_keypts_from_ids(ids, "valid") # yes, valid
np.savez(file="%s/test" % root_dir,
y_keypts=y_keypts,
z_keypts=z_keypts,
ids=ids,
orientations=orientations)
def prepare_valid_imgs_downsized():
ids = glob.glob("%s/valid_img/*.jpg" % root_dir)
ids = [os.path.basename(id_).replace(".jpg","") for id_ in ids]
output_folder = "%s/valid_img_cropped_80x80" % root_dir
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for id_ in ids:
kpts = read_kpt_file("%s/valid_lm/%s_lm.csv" % (root_dir, id_))
img = imread("%s/valid_img/%s.jpg" % (root_dir, id_))
img = img[ int(np.min(kpts[:,1])):int(np.max(kpts[:,1])),
int(np.min(kpts[:,0])):int(np.max(kpts[:,0]))]
img = resize(img, (80, 80))
imsave(arr=img, fname="%s/%s.jpg" % (output_folder, id_))
if __name__ == '__main__':
prepare_train()
prepare_valid()
prepare_test()
prepare_valid_imgs_downsized()