prepare_cityscapes_dataset.py 2.6 KB
Newer Older
L
lvmengsi 已提交
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
import os
import argparse
import functools
import glob
from PIL import Image
''' Based on https://github.com/junyanz/CycleGAN'''


def load_image(path):
    return Image.open(path).convert('RGB').resize((256, 256))


def propress_cityscapes(gtFine_dir, leftImg8bit_dir, output_dir, phase):
    save_dir = os.path.join(output_dir, phase)
    try:
        os.makedirs(save_dir)
    except Exception as e:
        print("{} makedirs".format(e))
        pass
    try:
        os.makedirs(os.path.join(save_dir, 'A'))
    except Exception as e:
        print("{} makedirs".format(e))
    try:
        os.makedirs(os.path.join(save_dir, 'B'))
    except Exception as e:
        print("{} makedirs".format(e))

    seg_expr = os.path.join(gtFine_dir, phase, "*", "*_color.png")
    seg_paths = glob.glob(seg_expr)
    seg_paths = sorted(seg_paths)

    photo_expr = os.path.join(leftImg8bit_dir, phase, "*", '*_leftImg8bit.png')
    photo_paths = glob.glob(photo_expr)
    photo_paths = sorted(photo_paths)

    assert len(seg_paths) == len(photo_paths), \
          "[%d] gtFine images NOT match [%d] leftImg8bit images. Aborting." % (len(segmap_paths), len(photo_paths))

    for i, (seg_path, photo_path) in enumerate(zip(seg_paths, photo_paths)):
        seg_image = load_image(seg_path)
        photo_image = load_image(photo_path)
        # save image
        save_path = os.path.join(save_dir, 'A', "%d_A.jpg" % i)
        photo_image.save(save_path, format='JPEG', subsampling=0, quality=100)
        save_path = os.path.join(save_dir, 'B', "%d_B.jpg" % i)
        seg_image.save(save_path, format='JPEG', subsampling=0, quality=100)

        if i % 10 == 0:
            print("preprocess %d ~ %d images." % (i, i + 10))


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    # yapf: disable
    parser.add_argument('--gtFine_dir',       type=str,     default=None,       help='Path to Cityscapes gtFine directory.')
    parser.add_argument('--leftImg8bit_dir',  type=str,     default=None,       help='Path to Cityscapes leftImg8bit_trainvaltest directory.')
    parser.add_argument('--output_dir',       type=str,     default=None,       help='Path to output Cityscapes directory.')
    # yapf: enable
    args = parser.parse_args()

    print('Preparing Cityscapes Dataset for val phase')
    propress_cityscapes(args.gtFine_dir, args.leftImg8bit_dir, args.output_dir,
                        'val')

    print('Preparing Cityscapes Dataset for train phase')
    propress_cityscapes(args.gtFine_dir, args.leftImg8bit_dir, args.output_dir,
                        'train')

    print("DONE!!!")