|
| 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)) |
0 commit comments