|
| 1 | +# coding: utf8 |
| 2 | +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserve. |
| 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 | +import glob |
| 17 | +import os.path |
| 18 | +import argparse |
| 19 | +import warnings |
| 20 | + |
| 21 | + |
| 22 | +def parse_args(): |
| 23 | + parser = argparse.ArgumentParser( |
| 24 | + description='PaddleSeg generate file list on cityscapes or your customized dataset.' |
| 25 | + ) |
| 26 | + parser.add_argument('dataset_root', help='dataset root directory', type=str) |
| 27 | + parser.add_argument( |
| 28 | + '--type', |
| 29 | + help='dataset type: \n' |
| 30 | + '- cityscapes \n' |
| 31 | + '- custom(default)', |
| 32 | + default="custom", |
| 33 | + type=str) |
| 34 | + parser.add_argument( |
| 35 | + '--separator', |
| 36 | + dest='separator', |
| 37 | + help='file list separator', |
| 38 | + default=" ", |
| 39 | + type=str) |
| 40 | + parser.add_argument( |
| 41 | + '--folder', |
| 42 | + help='the folder names of images and labels', |
| 43 | + type=str, |
| 44 | + nargs=2, |
| 45 | + default=['images', 'labels']) |
| 46 | + parser.add_argument( |
| 47 | + '--second_folder', |
| 48 | + help='the second-level folder names of train set, validation set, test set', |
| 49 | + type=str, |
| 50 | + nargs='*', |
| 51 | + default=['train', 'val', 'test']) |
| 52 | + parser.add_argument( |
| 53 | + '--format', |
| 54 | + help='data format of images and labels, e.g. jpg or png.', |
| 55 | + type=str, |
| 56 | + nargs=2, |
| 57 | + default=['jpg', 'png']) |
| 58 | + parser.add_argument( |
| 59 | + '--postfix', |
| 60 | + help='postfix of images or labels', |
| 61 | + type=str, |
| 62 | + nargs=2, |
| 63 | + default=['', '']) |
| 64 | + |
| 65 | + return parser.parse_args() |
| 66 | + |
| 67 | + |
| 68 | +def get_files(image_or_label, dataset_split, args): |
| 69 | + dataset_root = args.dataset_root |
| 70 | + postfix = args.postfix |
| 71 | + format = args.format |
| 72 | + folder = args.folder |
| 73 | + |
| 74 | + pattern = '*%s.%s' % (postfix[image_or_label], format[image_or_label]) |
| 75 | + |
| 76 | + search_files = os.path.join(dataset_root, folder[image_or_label], |
| 77 | + dataset_split, pattern) |
| 78 | + search_files2 = os.path.join(dataset_root, folder[image_or_label], |
| 79 | + dataset_split, "*", pattern) # 包含子目录 |
| 80 | + search_files3 = os.path.join(dataset_root, folder[image_or_label], |
| 81 | + dataset_split, "*", "*", pattern) # 包含三级目录 |
| 82 | + search_files4 = os.path.join(dataset_root, folder[image_or_label], |
| 83 | + dataset_split, "*", "*", "*", |
| 84 | + pattern) # 包含四级目录 |
| 85 | + search_files5 = os.path.join(dataset_root, folder[image_or_label], |
| 86 | + dataset_split, "*", "*", "*", "*", |
| 87 | + pattern) # 包含五级目录 |
| 88 | + |
| 89 | + filenames = glob.glob(search_files) |
| 90 | + filenames2 = glob.glob(search_files2) |
| 91 | + filenames3 = glob.glob(search_files3) |
| 92 | + filenames4 = glob.glob(search_files4) |
| 93 | + filenames5 = glob.glob(search_files5) |
| 94 | + |
| 95 | + filenames = filenames + filenames2 + filenames3 + filenames4 + filenames5 |
| 96 | + |
| 97 | + return sorted(filenames) |
| 98 | + |
| 99 | + |
| 100 | +def generate_list(args): |
| 101 | + dataset_root = args.dataset_root |
| 102 | + separator = args.separator |
| 103 | + |
| 104 | + for dataset_split in args.second_folder: |
| 105 | + print("Creating {}.txt...".format(dataset_split)) |
| 106 | + image_files = get_files(0, dataset_split, args) |
| 107 | + label_files = get_files(1, dataset_split, args) |
| 108 | + if not image_files: |
| 109 | + img_dir = os.path.join(dataset_root, args.folder[0], dataset_split) |
| 110 | + warnings.warn("No images in {} !!!".format(img_dir)) |
| 111 | + num_images = len(image_files) |
| 112 | + |
| 113 | + if not label_files: |
| 114 | + label_dir = os.path.join(dataset_root, args.folder[1], |
| 115 | + dataset_split) |
| 116 | + warnings.warn("No labels in {} !!!".format(label_dir)) |
| 117 | + num_label = len(label_files) |
| 118 | + |
| 119 | + if num_images != num_label and num_label > 0: |
| 120 | + raise Exception( |
| 121 | + "Number of images = {} number of labels = {} \n" |
| 122 | + "Either number of images is equal to number of labels, " |
| 123 | + "or number of labels is equal to 0.\n" |
| 124 | + "Please check your dataset!".format(num_images, num_label)) |
| 125 | + |
| 126 | + file_list = os.path.join(dataset_root, dataset_split + '.txt') |
| 127 | + with open(file_list, "w") as f: |
| 128 | + for item in range(num_images): |
| 129 | + left = image_files[item].replace(dataset_root, '', 1) |
| 130 | + if left[0] == os.path.sep: |
| 131 | + left = left.lstrip(os.path.sep) |
| 132 | + |
| 133 | + try: |
| 134 | + right = label_files[item].replace(dataset_root, '', 1) |
| 135 | + if right[0] == os.path.sep: |
| 136 | + right = right.lstrip(os.path.sep) |
| 137 | + line = left + separator + right + '\n' |
| 138 | + except: |
| 139 | + line = left + '\n' |
| 140 | + |
| 141 | + f.write(line) |
| 142 | + print(line) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == '__main__': |
| 146 | + args = parse_args() |
| 147 | + generate_list(args) |
0 commit comments