-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.py
210 lines (188 loc) · 9.74 KB
/
convert.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
API for converting CDR dataset into different subsets
Lastest update: Jun. 30, 2021
"""
import os
import csv
import re
import shutil
import argparse
import cv2
class CDRConverter:
def __init__(self, datapath, csvpath, outpath, problem_txt):
self.datapath = os.path.join(datapath, 'isprgb_crop', 'with_gt')
self.csvpath = csvpath
self.outpath = outpath
with open(problem_txt, 'r') as f:
self.problemlist = f.readlines()
self.problemlist = [line.rstrip() for line in self.problemlist]
def _get_names(self, path, name):
# helper function to get input names
regex_str = "^%s"%(name) # start with name
regex = re.compile(regex_str)
inputs = os.listdir(path)
inputs = [os.path.join(path, f) for f in inputs if regex.match(f)]
return inputs
def _check_problem(self, name):
# check whether this crop is problematic
# convert files name to _T_
key_name = name.replace("_M_", "_T_").replace("_R_", "_T_")
if key_name in self.problemlist:
return True
return False
def _convert_helper(self, mode, folder_name, args):
with open(self.csvpath,'r') as f:
header = f.readline()
total, skip = 0, 0
for line in f:
row = line.strip().split(',')
name = row[0].split('/')
set_type = row[-1] # train/val/test
# skip images based on user's filters
if set_type != mode: continue
if args.type != 'all' and row[1] != args.type : continue
if args.reflection != 'all' and row[2] != args.reflection : continue
if args.ghost != 'all' and row[3] != args.ghost : continue
if args.motion != 'all' and row[4] != args.motion : continue
cam_fo = name[0] # camera folder
M = name[1] + '_M'
R = name[1] + '_R'
T = name[1] + '_T'
Mimages = self._get_names(os.path.join(self.datapath,cam_fo), M)
Rimages = self._get_names(os.path.join(self.datapath,cam_fo), R)
Timages = self._get_names(os.path.join(self.datapath,cam_fo), T)
total += len(Mimages)
# move all M images to new_path
for img in Mimages:
basename = os.path.basename(img)
new_folder = os.path.join(self.outpath, folder_name, 'M')
os.makedirs(new_folder, exist_ok=True)
new_name = os.path.join(new_folder, cam_fo + '_' + basename)
# remove if problematic
if self._check_problem(os.path.basename(new_name)):
print("skip %s" % (new_name))
continue
if args.crop32 or args.downsample_scale or args.remove_extreme:
img = cv2.imread(img, -1)
if args.downsample_scale:
# downsample
img = img[::args.downsample_scale, ::args.downsample_scale, :]
if args.crop32:
# crop image into 32's multiples
new_input_h = 32*(img.shape[0]//32)
new_input_w = 32*(img.shape[1]//32)
img = img[:new_input_h, :new_input_w, :]
if args.remove_extreme:
# remove crops with extreme height/width ratios
curr_h, curr_w = img.shape[:2]
long_side = max(curr_h, curr_w)
short_side = min(curr_h, curr_w)
if long_side / short_side > 4:
skip += 1
continue
cv2.imwrite(new_name, img)
else:
shutil.copy(img, new_name)
# move all R images to new_path
for img in Rimages:
basename = os.path.basename(img)
new_folder = os.path.join(self.outpath, folder_name, 'R')
os.makedirs(new_folder, exist_ok=True)
new_name = os.path.join(new_folder, cam_fo + '_' + basename)
# remove if problematic
if self._check_problem(os.path.basename(new_name)):
print("skip %s" % (new_name))
continue
if args.crop32 or args.downsample_scale or args.remove_extreme:
img = cv2.imread(img, -1)
if args.downsample_scale:
# downsample
img = img[::args.downsample_scale, ::args.downsample_scale, :]
if args.crop32:
# crop image into 32's multiples
new_input_h = 32*(img.shape[0]//32)
new_input_w = 32*(img.shape[1]//32)
img = img[:new_input_h, :new_input_w, :]
if args.remove_extreme:
# remove crops with extreme height/width ratios
curr_h, curr_w = img.shape[:2]
long_side = max(curr_h, curr_w)
short_side = min(curr_h, curr_w)
if long_side / short_side > 4: continue
cv2.imwrite(new_name, img)
else:
shutil.copy(img, new_name)
# move all T images to new_path
for img in Timages:
basename = os.path.basename(img)
new_folder = os.path.join(self.outpath, folder_name, 'T')
os.makedirs(new_folder, exist_ok=True)
new_name = os.path.join(new_folder, cam_fo + '_' + basename)
# remove if problematic
if self._check_problem(os.path.basename(new_name)):
print("skip %s" % (new_name))
continue
if args.crop32 or args.downsample_scale or args.remove_extreme:
img = cv2.imread(img, -1)
if args.downsample_scale:
# downsample
img = img[::args.downsample_scale, ::args.downsample_scale, :]
if args.crop32:
# crop image into 32's multiples
new_input_h = 32*(img.shape[0]//32)
new_input_w = 32*(img.shape[1]//32)
img = img[:new_input_h, :new_input_w, :]
if args.remove_extreme:
# remove crops with extreme height/width ratios
curr_h, curr_w = img.shape[:2]
long_side = max(curr_h, curr_w)
short_side = min(curr_h, curr_w)
if long_side / short_side > 4: continue
cv2.imwrite(new_name, img)
else:
shutil.copy(img, new_name)
print("generated %d triplets"%(total-skip))
def convert(self, args):
# 0 for trainset, 1 for valset, 2 for test set
if args.train:
print("generating trainset...")
self._convert_helper('0', 'train', args)
if args.val:
print("generating valset...")
self._convert_helper('1', 'val', args)
if args.test:
print("generating testset...")
self._convert_helper('2', 'test', args)
def create_parser():
parser = argparse.ArgumentParser(add_help=True)
# paths
parser.add_argument('--datapath', type=str, required=True,
help='path to dataset')
parser.add_argument('--csvpath', type=str, required=True,
help='path to csv')
parser.add_argument('--output', type=str, required=True,
help='path to store the converted (sub) dataset')
parser.add_argument('--problem_txt', type=str, required=True,
help='txt file storing problematic crops')
# dataset choices
parser.add_argument('--train', action='store_true', help='generate trainset')
parser.add_argument('--val', action='store_true', help='generate valset')
parser.add_argument('--test', action='store_true', help='generate testset')
parser.add_argument('--crop32', action='store_true', help='some methods require image size of a multiple of 32, this option help crop the image')
parser.add_argument('--downsample_scale', type=int, help='downsample x N, N must be integer')
parser.add_argument('--remove_extreme', action='store_true', help='remove the crops with extreme height/width ratio')
# scene choices
parser.add_argument('--type', type=str, default="all", choices=['BRBT', 'SRST', 'BRST', 'all'], help='scene type')
parser.add_argument('--reflection', type=str, default="all", choices=['strong', 'medium', 'weak', 'all'], help='reflection type')
parser.add_argument('--ghost', type=str, default="all", choices=['0', '1', 'all'], help='generate subset with ghost effect')
parser.add_argument('--motion', type=str, default="all", choices=['0', '1', 'all'], help='generate subset with motion blur')
return parser
def parse_args(parser):
args = parser.parse_args()
return args
if __name__ == "__main__":
parser = create_parser()
args = parse_args(parser)
downloader = CDRConverter(args.datapath, args.csvpath, args.output, args.problem_txt)
downloader.convert(args)
print("Done! Your subset(s) are ready!")