|
| 1 | +import os |
| 2 | +import glob |
| 3 | +from PIL import Image |
| 4 | + |
| 5 | +help_msg = """ |
| 6 | +The dataset can be downloaded from https://cityscapes-dataset.com. |
| 7 | +Please download the datasets [gtFine_trainvaltest.zip] and [leftImg8bit_trainvaltest.zip] and unzip them. |
| 8 | +gtFine contains the semantics segmentations. Use --gtFine_dir to specify the path to the unzipped gtFine_trainvaltest directory. |
| 9 | +leftImg8bit contains the dashcam photographs. Use --leftImg8bit_dir to specify the path to the unzipped leftImg8bit_trainvaltest directory. |
| 10 | +The processed images will be placed at --output_dir. |
| 11 | +
|
| 12 | +Example usage: |
| 13 | +
|
| 14 | +python prepare_cityscapes_dataset.py --gitFine_dir ./gtFine/ --leftImg8bit_dir ./leftImg8bit --output_dir ./datasets/cityscapes/ |
| 15 | +""" |
| 16 | + |
| 17 | +def load_resized_img(path): |
| 18 | + return Image.open(path).convert('RGB').resize((256, 256)) |
| 19 | + |
| 20 | +def check_matching_pair(segmap_path, photo_path): |
| 21 | + segmap_identifier = os.path.basename(segmap_path).replace('_gtFine_color', '') |
| 22 | + photo_identifier = os.path.basename(photo_path).replace('_leftImg8bit', '') |
| 23 | + |
| 24 | + assert segmap_identifier == photo_identifier, \ |
| 25 | + "[%s] and [%s] don't seem to be matching. Aborting." % (segmap_path, photo_path) |
| 26 | + |
| 27 | + |
| 28 | +def process_cityscapes(gtFine_dir, leftImg8bit_dir, output_dir, phase): |
| 29 | + save_phase = 'test' if phase == 'val' else 'train' |
| 30 | + savedir = os.path.join(output_dir, save_phase) |
| 31 | + os.makedirs(savedir, exist_ok=True) |
| 32 | + os.makedirs(savedir + 'A', exist_ok=True) |
| 33 | + os.makedirs(savedir + 'B', exist_ok=True) |
| 34 | + print("Directory structure prepared at %s" % output_dir) |
| 35 | + |
| 36 | + segmap_expr = os.path.join(gtFine_dir, phase) + "/*/*_color.png" |
| 37 | + segmap_paths = glob.glob(segmap_expr) |
| 38 | + segmap_paths = sorted(segmap_paths) |
| 39 | + |
| 40 | + photo_expr = os.path.join(leftImg8bit_dir, phase) + "/*/*_leftImg8bit.png" |
| 41 | + photo_paths = glob.glob(photo_expr) |
| 42 | + photo_paths = sorted(photo_paths) |
| 43 | + |
| 44 | + assert len(segmap_paths) == len(photo_paths), \ |
| 45 | + "%d images that match [%s], and %d images that match [%s]. Aborting." % (len(segmap_paths), segmap_expr, len(photo_paths), photo_expr) |
| 46 | + |
| 47 | + for i, (segmap_path, photo_path) in enumerate(zip(segmap_paths, photo_paths)): |
| 48 | + check_matching_pair(segmap_path, photo_path) |
| 49 | + segmap = load_resized_img(segmap_path) |
| 50 | + photo = load_resized_img(photo_path) |
| 51 | + |
| 52 | + # data for pix2pix where the two images are placed side-by-side |
| 53 | + sidebyside = Image.new('RGB', (512, 256)) |
| 54 | + sidebyside.paste(segmap, (256, 0)) |
| 55 | + sidebyside.paste(photo, (0, 0)) |
| 56 | + savepath = os.path.join(savedir, "%d.jpg" % i) |
| 57 | + sidebyside.save(savepath, format='JPEG', subsampling=0, quality=100) |
| 58 | + |
| 59 | + # data for cyclegan where the two images are stored at two distinct directories |
| 60 | + savepath = os.path.join(savedir + 'A', "%d_A.jpg" % i) |
| 61 | + photo.save(savepath, format='JPEG', subsampling=0, quality=100) |
| 62 | + savepath = os.path.join(savedir + 'B', "%d_B.jpg" % i) |
| 63 | + segmap.save(savepath, format='JPEG', subsampling=0, quality=100) |
| 64 | + |
| 65 | + if i % (len(segmap_paths) // 10) == 0: |
| 66 | + print("%d / %d: last image saved at %s, " % (i, len(segmap_paths), savepath)) |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + import argparse |
| 79 | + parser = argparse.ArgumentParser() |
| 80 | + parser.add_argument('--gtFine_dir', type=str, required=True, |
| 81 | + help='Path to the Cityscapes gtFine directory.') |
| 82 | + parser.add_argument('--leftImg8bit_dir', type=str, required=True, |
| 83 | + help='Path to the Cityscapes leftImg8bit_trainvaltest directory.') |
| 84 | + parser.add_argument('--output_dir', type=str, required=True, |
| 85 | + default='./datasets/cityscapes', |
| 86 | + help='Directory the output images will be written to.') |
| 87 | + opt = parser.parse_args() |
| 88 | + |
| 89 | + print(help_msg) |
| 90 | + |
| 91 | + print('Preparing Cityscapes Dataset for val phase') |
| 92 | + process_cityscapes(opt.gtFine_dir, opt.leftImg8bit_dir, opt.output_dir, "val") |
| 93 | + print('Preparing Cityscapes Dataset for train phase') |
| 94 | + process_cityscapes(opt.gtFine_dir, opt.leftImg8bit_dir, opt.output_dir, "train") |
| 95 | + |
| 96 | + print('Done') |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments