-
Notifications
You must be signed in to change notification settings - Fork 2
/
resize_sk.py
138 lines (107 loc) · 3.78 KB
/
resize_sk.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import argparse
import os
import numpy as np
import pandas as pd
import multiprocessing as mp
from PIL import Image, ImageOps
DESIRED_SIZE = 256
def config():
parser = argparse.ArgumentParser(description='SHREC meta')
parser.add_argument('--data_dir',
type=str,
required=True,
help='data folder path')
parser.add_argument('--dataset',
required=True,
choices=['13', '14'],
help='dataset')
args = parser.parse_args()
return args
def transform_im(im):
if im.mode == 'RGBA':
im = im.convert('RGB')
# resize
old_size = np.asarray(im.size)
ratio = float(DESIRED_SIZE) / max(old_size)
new_size = map(int, old_size * ratio)
im = im.resize(new_size, Image.ANTIALIAS)
return im
def process_im(path):
im = Image.open(path)
im = transform_im(im)
# saving
im_dir = os.path.dirname(path)
fname = os.path.basename(path)
if 'SHREC13' in path:
split = im_dir.split(os.path.sep)[-1]
clsname = im_dir.split(os.path.sep)[-2]
if split == 'test':
save_dir = os.path.join(*im_dir.split(os.path.sep)[:-3])
elif split == 'train':
save_dir = os.path.join(*im_dir.split(os.path.sep)[:-4])
save_dir = os.path.join(
os.path.sep, save_dir,
'SHREC13_SBR_SKETCHES_RESIZED', clsname, split)
elif 'SHREC14' in path:
split = im_dir.split(os.path.sep)[-1]
clsname = im_dir.split(os.path.sep)[-2]
save_dir = os.path.join(*im_dir.split(os.path.sep)[:-4])
save_dir = os.path.join(
os.path.sep, save_dir,
'SHREC14LSSTB_SKETCHES_RESIZED', clsname, split)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
im.save(os.path.join(save_dir, fname), quality=95)
def process_idx(idx):
for i in idx:
process_im(i)
def worker(q, idx):
q.put(process_idx(idx))
def domulti(ncpus, paths):
n = len(paths)
q = mp.Queue()
processes = []
for i in range(ncpus):
lower = int((i) * n / (ncpus))
upper = int((i + 1) * n / (ncpus))
processes.append(mp.Process(target=worker,
args=(q, paths[lower:upper])))
for p in processes:
p.start()
for p in processes:
p.join()
def transform_path(df):
paths = df.index
new_paths = []
for p in paths:
dataset = p.split(os.path.sep)[0]
tmp = os.path.join(*p.split(os.path.sep)[-3:])
if dataset == 'SHREC13':
new_paths.append(
os.path.join(dataset, 'SHREC13_SBR_SKETCHES_RESIZED', tmp))
elif dataset == 'SHREC14':
new_paths.append(
os.path.join(dataset, 'SHREC14LSSTB_SKETCHES_RESIZED', tmp))
df.index = new_paths
return df
def main():
args = config()
if args.dataset == '13':
df = pd.read_hdf(os.path.join('labels', 'SHREC13', 'sk_orig.hdf5'))
if args.dataset == '14':
df = pd.read_hdf(os.path.join('labels', 'SHREC14', 'sk_orig.hdf5'))
paths = df.index.values
all_paths = []
for p in paths:
all_paths.append(os.path.join(args.data_dir, p))
domulti(10, all_paths)
df_resized = transform_path(df)
if args.dataset == '13':
df_resized.to_hdf(os.path.join('labels', 'SHREC13', 'sk_resized.hdf5'), 'sk')
if args.dataset == '14':
df_resized.to_hdf(os.path.join('labels', 'SHREC14', 'sk_resized.hdf5'), 'sk')
df_part = pd.read_hdf(os.path.join('labels', 'PART-SHREC14', 'sk_orig.hdf5'))
df_part_resized = transform_path(df_part)
df_part_resized.to_hdf(os.path.join('labels', 'PART-SHREC14', 'sk_resized.hdf5'), 'sk')
if __name__ == '__main__':
main()