|
| 1 | + |
| 2 | +# Copyright 2018 The TensorFlow Authors All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ============================================================================== |
| 16 | + |
| 17 | +""" Offline data generation for the Cityscapes dataset.""" |
| 18 | + |
| 19 | +import os |
| 20 | +from absl import app |
| 21 | +from absl import flags |
| 22 | +from absl import logging |
| 23 | +import numpy as np |
| 24 | +import cv2 |
| 25 | +import os, glob |
| 26 | + |
| 27 | +import alignment |
| 28 | +from alignment import compute_overlap |
| 29 | +from alignment import align |
| 30 | + |
| 31 | + |
| 32 | +SKIP = 2 |
| 33 | +WIDTH = 416 |
| 34 | +HEIGHT = 128 |
| 35 | +SUB_FOLDER = 'train' |
| 36 | +INPUT_DIR = '/usr/local/google/home/anelia/struct2depth/CITYSCAPES_FULL/' |
| 37 | +OUTPUT_DIR = '/usr/local/google/home/anelia/struct2depth/CITYSCAPES_Processed/' |
| 38 | + |
| 39 | +def crop(img, segimg, fx, fy, cx, cy): |
| 40 | + # Perform center cropping, preserving 50% vertically. |
| 41 | + middle_perc = 0.50 |
| 42 | + left = 1 - middle_perc |
| 43 | + half = left / 2 |
| 44 | + a = img[int(img.shape[0]*(half)):int(img.shape[0]*(1-half)), :] |
| 45 | + aseg = segimg[int(segimg.shape[0]*(half)):int(segimg.shape[0]*(1-half)), :] |
| 46 | + cy /= (1 / middle_perc) |
| 47 | + |
| 48 | + # Resize to match target height while preserving aspect ratio. |
| 49 | + wdt = int((float(HEIGHT)*a.shape[1]/a.shape[0])) |
| 50 | + x_scaling = float(wdt)/a.shape[1] |
| 51 | + y_scaling = float(HEIGHT)/a.shape[0] |
| 52 | + b = cv2.resize(a, (wdt, HEIGHT)) |
| 53 | + bseg = cv2.resize(aseg, (wdt, HEIGHT)) |
| 54 | + |
| 55 | + # Adjust intrinsics. |
| 56 | + fx*=x_scaling |
| 57 | + fy*=y_scaling |
| 58 | + cx*=x_scaling |
| 59 | + cy*=y_scaling |
| 60 | + |
| 61 | + # Perform center cropping horizontally. |
| 62 | + remain = b.shape[1] - WIDTH |
| 63 | + cx /= (b.shape[1] / WIDTH) |
| 64 | + c = b[:, int(remain/2):b.shape[1]-int(remain/2)] |
| 65 | + cseg = bseg[:, int(remain/2):b.shape[1]-int(remain/2)] |
| 66 | + |
| 67 | + return c, cseg, fx, fy, cx, cy |
| 68 | + |
| 69 | + |
| 70 | +def run_all(): |
| 71 | + dir_name=INPUT_DIR + '/leftImg8bit_sequence/' + SUB_FOLDER + '/*' |
| 72 | + print('Processing directory', dir_name) |
| 73 | + for location in glob.glob(INPUT_DIR + '/leftImg8bit_sequence/' + SUB_FOLDER + '/*'): |
| 74 | + location_name = os.path.basename(location) |
| 75 | + print('Processing location', location_name) |
| 76 | + files = sorted(glob.glob(location + '/*.png')) |
| 77 | + files = [file for file in files if '-seg.png' not in file] |
| 78 | + # Break down into sequences |
| 79 | + sequences = {} |
| 80 | + seq_nr = 0 |
| 81 | + last_seq = '' |
| 82 | + last_imgnr = -1 |
| 83 | + |
| 84 | + for i in range(len(files)): |
| 85 | + seq = os.path.basename(files[i]).split('_')[1] |
| 86 | + nr = int(os.path.basename(files[i]).split('_')[2]) |
| 87 | + if seq!=last_seq or last_imgnr+1!=nr: |
| 88 | + seq_nr+=1 |
| 89 | + last_imgnr = nr |
| 90 | + last_seq = seq |
| 91 | + if not seq_nr in sequences: |
| 92 | + sequences[seq_nr] = [] |
| 93 | + sequences[seq_nr].append(files[i]) |
| 94 | + |
| 95 | + for (k,v) in sequences.items(): |
| 96 | + print('Processing sequence', k, 'with', len(v), 'elements...') |
| 97 | + output_dir = OUTPUT_DIR + '/' + location_name + '_' + str(k) |
| 98 | + if not os.path.isdir(output_dir): |
| 99 | + os.mkdir(output_dir) |
| 100 | + files = sorted(v) |
| 101 | + triplet = [] |
| 102 | + seg_triplet = [] |
| 103 | + ct = 1 |
| 104 | + |
| 105 | + # Find applicable intrinsics. |
| 106 | + for j in range(len(files)): |
| 107 | + osegname = os.path.basename(files[j]).split('_')[1] |
| 108 | + oimgnr = os.path.basename(files[j]).split('_')[2] |
| 109 | + applicable_intrinsics = INPUT_DIR + '/camera/' + SUB_FOLDER + '/' + location_name + '/' + location_name + '_' + osegname + '_' + oimgnr + '_camera.json' |
| 110 | + # Get the intrinsics for one of the file of the sequence. |
| 111 | + if os.path.isfile(applicable_intrinsics): |
| 112 | + f = open(applicable_intrinsics, 'r') |
| 113 | + lines = f.readlines() |
| 114 | + f.close() |
| 115 | + lines = [line.rstrip() for line in lines] |
| 116 | + |
| 117 | + fx = float(lines[11].split(': ')[1].replace(',', '')) |
| 118 | + fy = float(lines[12].split(': ')[1].replace(',', '')) |
| 119 | + cx = float(lines[13].split(': ')[1].replace(',', '')) |
| 120 | + cy = float(lines[14].split(': ')[1].replace(',', '')) |
| 121 | + |
| 122 | + for j in range(0, len(files), SKIP): |
| 123 | + img = cv2.imread(files[j]) |
| 124 | + segimg = cv2.imread(files[j].replace('.png', '-seg.png')) |
| 125 | + |
| 126 | + smallimg, segimg, fx_this, fy_this, cx_this, cy_this = crop(img, segimg, fx, fy, cx, cy) |
| 127 | + triplet.append(smallimg) |
| 128 | + seg_triplet.append(segimg) |
| 129 | + if len(triplet)==3: |
| 130 | + cmb = np.hstack(triplet) |
| 131 | + align1, align2, align3 = align(seg_triplet[0], seg_triplet[1], seg_triplet[2]) |
| 132 | + cmb_seg = np.hstack([align1, align2, align3]) |
| 133 | + cv2.imwrite(os.path.join(output_dir, str(ct).zfill(10) + '.png'), cmb) |
| 134 | + cv2.imwrite(os.path.join(output_dir, str(ct).zfill(10) + '-fseg.png'), cmb_seg) |
| 135 | + f = open(os.path.join(output_dir, str(ct).zfill(10) + '_cam.txt'), 'w') |
| 136 | + f.write(str(fx_this) + ',0.0,' + str(cx_this) + ',0.0,' + str(fy_this) + ',' + str(cy_this) + ',0.0,0.0,1.0') |
| 137 | + f.close() |
| 138 | + del triplet[0] |
| 139 | + del seg_triplet[0] |
| 140 | + ct+=1 |
| 141 | + |
| 142 | +# Create file list for training. Be careful as it collects and includes all files recursively. |
| 143 | +fn = open(OUTPUT_DIR + '/' + SUB_FOLDER + '.txt', 'w') |
| 144 | +for f in glob.glob(OUTPUT_DIR + '/*/*.png'): |
| 145 | + if '-seg.png' in f or '-fseg.png' in f: |
| 146 | + continue |
| 147 | + folder_name = f.split('/')[-2] |
| 148 | + img_name = f.split('/')[-1].replace('.png', '') |
| 149 | + fn.write(folder_name + ' ' + img_name + '\n') |
| 150 | +fn.close() |
| 151 | + |
| 152 | + |
| 153 | +def main(_): |
| 154 | + run_all() |
| 155 | + |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + app.run(main) |
0 commit comments