Skip to content

Commit b14d5df

Browse files
committed
add cropping scripts
1 parent bd67ace commit b14d5df

32 files changed

+1920
-41
lines changed

lib/dataset/crop/DAVIS/readme.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
# Preprocessing MSRA10K
2-
3-
### Download raw images and annotations
4-
5-
6-
### Crop & Generate data info (~20 min)
1+
# Preprocessing DAVIS
72

83
````shell
9-
#python par_crop.py -h
104
python par_crop.py --enable_mask --num_threads 24
115
python gen_json.py
126
````

lib/dataset/crop/DAVIS/visual.py

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding:utf-8 -*-
2+
# ! ./usr/bin/env python
3+
# __author__ = 'zzp'
4+
5+
import json
6+
import numpy as np
7+
from os import listdir
8+
from os.path import join
9+
10+
basepath = '/data/share/RGBT210/'
11+
save = dict()
12+
13+
14+
def genjson():
15+
videos = listdir(basepath)
16+
17+
for v in videos:
18+
save[v] = dict()
19+
save[v]['name'] = v # video name
20+
21+
# save img names
22+
v_in_path = join(basepath, v, 'infrared')
23+
v_rgb_path = join(basepath, v, 'visible')
24+
temp1 = listdir(v_in_path)
25+
temp2 = listdir(v_rgb_path)
26+
temp1.sort()
27+
temp2.sort()
28+
save[v]['infrared_imgs'] = temp1 # infrared file names
29+
save[v]['visible_imgs'] = temp2 # infrared file names
30+
31+
# read gt
32+
v_in_gt_path = join(basepath, v, 'init.txt')
33+
v_rgb_gt_path = join(basepath, v, 'init.txt')
34+
v_in_gts = np.loadtxt(v_in_gt_path, delimiter=',')
35+
v_rgb_gts = np.loadtxt(v_rgb_gt_path, delimiter=',')
36+
37+
v_in_gts[:, 0:2] = v_in_gts[:, 0:2] - 1 # to python 0 index
38+
v_rgb_gts[:, 0:2] = v_rgb_gts[:, 0:2] - 1 # to python 0 index
39+
40+
v_in_init = v_in_gts[0]
41+
v_rgb_init = v_rgb_gts[0]
42+
43+
# save int and gt
44+
save[v]['infrared_init'] = v_in_init.tolist()
45+
save[v]['visible_init'] = v_rgb_init.tolist()
46+
save[v]['infrared_gt'] = v_in_gts.tolist()
47+
save[v]['visible_gt'] = v_rgb_gts.tolist()
48+
49+
json.dump(save, open('/data/zpzhang/datasets/dataset/RGBT210.json', 'w'), indent=4, sort_keys=True)
50+
51+
52+
if __name__ == '__main__':
53+
genjson()
54+
55+
56+

lib/dataset/crop/RGBT210/gen_json.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from os.path import join
2+
from os import listdir
3+
import json
4+
import cv2
5+
import numpy as np
6+
from pprint import pprint
7+
8+
print('loading json (raw RGBT234 info), please wait 20 seconds~')
9+
RGBT210 = json.load(open('/data/zpzhang/datasets/dataset/RGBT210.json', 'r'))
10+
RGBT210_base_path = '/data/share/RGBT210'
11+
12+
def check_size(frame_sz, bbox):
13+
min_ratio = 0.1
14+
max_ratio = 0.75
15+
# only accept objects >10% and <75% of the total frame
16+
area_ratio = np.sqrt((bbox[2]-bbox[0])*(bbox[3]-bbox[1])/float(np.prod(frame_sz)))
17+
ok = (area_ratio > min_ratio) and (area_ratio < max_ratio)
18+
return ok
19+
20+
21+
def check_borders(frame_sz, bbox):
22+
dist_from_border = 0.05 * (bbox[2] - bbox[0] + bbox[3] - bbox[1])/2
23+
ok = (bbox[0] > dist_from_border) and (bbox[1] > dist_from_border) and \
24+
((frame_sz[0] - bbox[2]) > dist_from_border) and \
25+
((frame_sz[1] - bbox[3]) > dist_from_border)
26+
return ok
27+
28+
29+
snippets = dict()
30+
31+
n_videos = 0
32+
33+
34+
for v_name in list(RGBT210.keys()):
35+
video = RGBT210[v_name]
36+
n_videos += 1
37+
in_frames = video['infrared_imgs']
38+
rgb_frames = video['visible_imgs']
39+
snippet = dict()
40+
snippets[video['name']] = dict()
41+
42+
# read a image to get im size
43+
im_temp_path = join(RGBT210_base_path, video['name'], 'visible', rgb_frames[0])
44+
im_temp = cv2.imread(im_temp_path)
45+
frame_sz = [im_temp.shape[1], im_temp.shape[0]]
46+
47+
in_gts = video['infrared_gt']
48+
rgb_gts = video['visible_gt']
49+
50+
for f, in_frame in enumerate(in_frames):
51+
in_bbox = in_gts[f] # (x,y,w,h)
52+
rgb_bbox = rgb_gts[f] # (x,y,w,h)
53+
54+
bboxs = [[in_bbox[0], in_bbox[1], in_bbox[0]+in_bbox[2], in_bbox[1]+in_bbox[3]],
55+
[rgb_bbox[0], rgb_bbox[1], rgb_bbox[0]+rgb_bbox[2], rgb_bbox[1]+rgb_bbox[3]]] #(xmin, ymin, xmax, ymax)
56+
57+
imgs = [in_frames[f], rgb_frames[f]] # image name may be different in visible and rgb imgs
58+
59+
snippet['{:06d}'.format(f)] = [imgs, bboxs]
60+
61+
snippets[video['name']]['{:02d}'.format(0)] = snippet.copy()
62+
63+
json.dump(snippets, open('/data/share/SMALLSIAM/RGBT210/all.json', 'w'), indent=4, sort_keys=True)
64+
print('done!')

lib/dataset/crop/RGBT210/par_crop.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
from os.path import join, isdir, exists
2+
from os import listdir, mkdir, makedirs
3+
import cv2
4+
import numpy as np
5+
import glob
6+
from concurrent import futures
7+
import sys
8+
import time
9+
10+
11+
RGBT234_base_path = '/data/share/RGBT210'
12+
13+
# Print iterations progress (thanks StackOverflow)
14+
def printProgress(iteration, total, prefix='', suffix='', decimals=1, barLength=100):
15+
"""
16+
Call in a loop to create terminal progress bar
17+
@params:
18+
iteration - Required : current iteration (Int)
19+
total - Required : total iterations (Int)
20+
prefix - Optional : prefix string (Str)
21+
suffix - Optional : suffix string (Str)
22+
decimals - Optional : positive number of decimals in percent complete (Int)
23+
barLength - Optional : character length of bar (Int)
24+
"""
25+
formatStr = "{0:." + str(decimals) + "f}"
26+
percents = formatStr.format(100 * (iteration / float(total)))
27+
filledLength = int(round(barLength * iteration / float(total)))
28+
bar = '' * filledLength + '-' * (barLength - filledLength)
29+
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
30+
if iteration == total:
31+
sys.stdout.write('\x1b[2K\r')
32+
sys.stdout.flush()
33+
34+
35+
def crop_hwc(image, bbox, out_sz, padding=(0, 0, 0)):
36+
a = (out_sz-1) / (bbox[2]-bbox[0])
37+
b = (out_sz-1) / (bbox[3]-bbox[1])
38+
c = -a * bbox[0]
39+
d = -b * bbox[1]
40+
mapping = np.array([[a, 0, c],
41+
[0, b, d]]).astype(np.float)
42+
crop = cv2.warpAffine(image, mapping, (out_sz, out_sz), borderMode=cv2.BORDER_CONSTANT, borderValue=padding)
43+
return crop
44+
45+
46+
def pos_s_2_bbox(pos, s):
47+
return [pos[0]-s/2, pos[1]-s/2, pos[0]+s/2, pos[1]+s/2]
48+
49+
50+
def crop_like_SiamFC(image, bbox, context_amount=0.5, exemplar_size=127, instanc_size=255, padding=(0, 0, 0)):
51+
target_pos = [(bbox[2]+bbox[0])/2., (bbox[3]+bbox[1])/2.]
52+
target_size = [bbox[2]-bbox[0], bbox[3]-bbox[1]] # width, height
53+
wc_z = target_size[1] + context_amount * sum(target_size)
54+
hc_z = target_size[0] + context_amount * sum(target_size)
55+
s_z = np.sqrt(wc_z * hc_z)
56+
scale_z = exemplar_size / s_z
57+
d_search = (instanc_size - exemplar_size) / 2
58+
pad = d_search / scale_z
59+
s_x = s_z + 2 * pad
60+
61+
z = crop_hwc(image, pos_s_2_bbox(target_pos, s_z), exemplar_size, padding)
62+
x = crop_hwc(image, pos_s_2_bbox(target_pos, s_x), instanc_size, padding)
63+
return z, x
64+
65+
66+
def crop_img(im, bbox, instanc_size):
67+
avg_chans = np.mean(im, axis=(0, 1))
68+
z, x = crop_like_SiamFC(im, bbox, instanc_size=instanc_size, padding=avg_chans)
69+
return z, x
70+
71+
72+
eps = 1e-5
73+
def crop_video(video, crop_path, instanc_size):
74+
video_crop_base_path = join(crop_path, video)
75+
if not exists(video_crop_base_path): makedirs(video_crop_base_path)
76+
77+
video_base_path = join(RGBT234_base_path, video)
78+
79+
# infrared gt
80+
in_gts_path = join(video_base_path, 'init.txt')
81+
try:
82+
in_gts = np.loadtxt(open(in_gts_path, "rb"), delimiter=',')
83+
except:
84+
in_gts = np.loadtxt(open(in_gts_path, "rb"), delimiter=' ')
85+
86+
# rgb gt
87+
rgb_gts_path = join(video_base_path, 'init.txt')
88+
try:
89+
rgb_gts = np.loadtxt(open(rgb_gts_path, "rb"), delimiter=',')
90+
except:
91+
rgb_gts = np.loadtxt(open(rgb_gts_path, "rb"), delimiter=' ')
92+
93+
in_jpgs = sorted(glob.glob(join(video_base_path, 'infrared', '*.jpg')))
94+
rgb_jpgs = sorted(glob.glob(join(video_base_path, 'visible', '*.jpg')))
95+
96+
97+
for idx, img_path in enumerate(in_jpgs):
98+
in_im = cv2.imread(img_path)
99+
rgb_im = cv2.imread(rgb_jpgs[idx])
100+
in_gt = in_gts[idx]
101+
rgb_gt = rgb_gts[idx]
102+
in_bbox = [int(g) for g in in_gt] # (x,y,w,h)
103+
104+
if abs(in_bbox[2]) < eps or abs(in_bbox[3]) < eps:
105+
continue
106+
107+
in_bbox = [in_bbox[0], in_bbox[1], in_bbox[0]+in_bbox[2], in_bbox[1]+in_bbox[3]] # (xmin, ymin, xmax, ymax)
108+
rgb_bbox = [int(g) for g in rgb_gt] # (x,y,w,h)
109+
rgb_bbox = [rgb_bbox[0], rgb_bbox[1], rgb_bbox[0] + rgb_bbox[2], rgb_bbox[1] + rgb_bbox[3]] # (xmin, ymin, xmax, ymax)
110+
111+
in_z, in_x = crop_img(in_im, in_bbox, instanc_size)
112+
rgb_z, rgb_x = crop_img(rgb_im, rgb_bbox, instanc_size)
113+
114+
cv2.imwrite(join(video_crop_base_path, '{:06d}.{:02d}.in.z.jpg'.format(int(idx), 0)), in_z)
115+
cv2.imwrite(join(video_crop_base_path, '{:06d}.{:02d}.in.x.jpg'.format(int(idx), 0)), in_x)
116+
117+
cv2.imwrite(join(video_crop_base_path, '{:06d}.{:02d}.rgb.z.jpg'.format(int(idx), 0)), rgb_z)
118+
cv2.imwrite(join(video_crop_base_path, '{:06d}.{:02d}.rgb.x.jpg'.format(int(idx), 0)), rgb_x)
119+
120+
121+
def main(instanc_size=271, num_threads=24):
122+
crop_path = '/data/share/SMALLSIAM/RGBT210/crop{:d}'.format(instanc_size)
123+
if not exists(crop_path): makedirs(crop_path)
124+
125+
videos = sorted(listdir(RGBT234_base_path))
126+
n_videos = len(videos)
127+
128+
with futures.ProcessPoolExecutor(max_workers=num_threads) as executor:
129+
fs = [executor.submit(crop_video, video, crop_path, instanc_size) for video in videos]
130+
for i, f in enumerate(futures.as_completed(fs)):
131+
# Write progress to error so that it can be seen
132+
printProgress(i, n_videos, prefix='RGBT210', suffix='Done ', barLength=40)
133+
134+
135+
if __name__ == '__main__':
136+
since = time.time()
137+
main(int(sys.argv[1]), int(sys.argv[2]))
138+
time_elapsed = time.time() - since
139+
print('Total complete in {:.0f}m {:.0f}s'.format(
140+
time_elapsed // 60, time_elapsed % 60))

lib/dataset/crop/RGBT210/readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Preprocessing RGBT234 (train and val)
2+
3+
4+
### Crop & Generate data info (20 min)
5+
6+
````sh
7+
python RGBT234_genjson.py
8+
python par_crop.py 511 24
9+
python gen_json.py
10+
````
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding:utf-8 -*-
2+
# ! ./usr/bin/env python
3+
# __author__ = 'zzp'
4+
5+
import json
6+
import numpy as np
7+
from os import listdir
8+
from os.path import join
9+
10+
basepath = '/data/zpzhang/datasets/dataset/RGBT234/'
11+
save = dict()
12+
13+
14+
def genjson():
15+
videos = listdir(basepath)
16+
17+
for v in videos:
18+
save[v] = dict()
19+
save[v]['name'] = v # video name
20+
21+
# save img names
22+
v_in_path = join(basepath, v, 'infrared')
23+
v_rgb_path = join(basepath, v, 'visible')
24+
temp1 = listdir(v_in_path)
25+
temp2 = listdir(v_rgb_path)
26+
temp1.sort()
27+
temp2.sort()
28+
save[v]['infrared_imgs'] = temp1 # infrared file names
29+
save[v]['visible_imgs'] = temp2 # infrared file names
30+
31+
# read gt
32+
v_in_gt_path = join(basepath, v, 'infrared.txt')
33+
v_rgb_gt_path = join(basepath, v, 'visible.txt')
34+
v_in_gts = np.loadtxt(v_in_gt_path, delimiter=',')
35+
v_rgb_gts = np.loadtxt(v_rgb_gt_path, delimiter=',')
36+
37+
v_in_gts[:, 0:2] = v_in_gts[:, 0:2] - 1 # to python 0 index
38+
v_rgb_gts[:, 0:2] = v_rgb_gts[:, 0:2] - 1 # to python 0 index
39+
40+
v_in_init = v_in_gts[0]
41+
v_rgb_init = v_rgb_gts[0]
42+
43+
# save int and gt
44+
save[v]['infrared_init'] = v_in_init.tolist()
45+
save[v]['visible_init'] = v_rgb_init.tolist()
46+
save[v]['infrared_gt'] = v_in_gts.tolist()
47+
save[v]['visible_gt'] = v_rgb_gts.tolist()
48+
49+
json.dump(save, open('/data/zpzhang/datasets/dataset/RGBT234.json', 'w'), indent=4, sort_keys=True)
50+
51+
52+
if __name__ == '__main__':
53+
genjson()
54+
55+
56+

0 commit comments

Comments
 (0)