def parse_folder(folder, labels_file,
        train_file=None, percent_train=None,
        val_file=None, percent_val=None,
        test_file=None, percent_test=None,
        min_per_category=2,
        max_per_category=None,
        ):
    create_labels = (percent_train > 0)
    labels = []

    ### Read the labels from labels_file

    if not create_labels:
        with open(labels_file) as infile:
            for line in infile:
                line = line.strip()
                if line:
                    labels.append(line)

    ### Verify that at least two category folders exist

    folder_is_url = utils.is_url(folder)
    if folder_is_url:
        if not folder.endswith('/'):
            folder += '/'
        subdirs, _ = parse_web_listing(folder)
    else:
        if os.path.exists(folder) and os.path.isdir(folder):
            subdirs = []
            for filename in os.listdir(folder):
                subdir = os.path.join(folder, filename)
                if os.path.isdir(subdir):
                    subdirs.append(subdir)
        else:
            logger.error('folder does not exist')
            return False

    subdirs.sort()

    if len(subdirs) < 2:
        logger.error('folder must contain at least two subdirectories')
        return False

    ### Parse the folder

    train_count = 0
    val_count = 0
    test_count = 0

    if percent_train:
        train_outfile = open(train_file, 'w')
    if percent_val:
        val_outfile = open(val_file, 'w')
    if percent_test:
        test_outfile = open(test_file, 'w')

    subdir_index = 0
    label_index = 0
    for subdir in subdirs:
        # Use the directory name as the label
        label_name = subdir
        if folder_is_url:
            label_name = unescape(label_name)
        else:
            label_name = os.path.basename(label_name)
        label_name = label_name.replace('_',' ')
        if label_name.endswith('/'):
            # Remove trailing slash
            label_name = label_name[0:-1]

        if create_labels:
            labels.append(label_name)
            label_index = len(labels)-1
        else:
            found = False
            for i, l in enumerate(labels):
                if label_name == l:
                    found = True
                    label_index = i
                    break
            if not found:
                logger.warning('Category "%s" not found in labels_file. Skipping.' % label_name)
                continue

        logger.debug('Category - %s' % label_name)

        lines = []

        ### Read all images in the folder

        if folder_is_url:
            urls, _ = web_listing_all_files(folder + subdir, max_count=max_per_category)
            for url in urls:
                lines.append('%s %d' % (url, label_index))
        else:
            for dirpath, dirnames, filenames in os.walk(os.path.join(folder, subdir), followlinks=True):
                for filename in filenames:
                    if filename.lower().endswith(utils.image.SUPPORTED_EXTENSIONS):
                        lines.append('%s %d' % (os.path.join(folder, subdir, dirpath, filename), label_index))
                        if max_per_category is not None and len(lines) >= max_per_category:
                            break
                if max_per_category is not None and len(lines) >= max_per_category:
                    logger.warning('Reached maximum limit for this category')
                    break

        ### Split up the lines

        train_lines = []
        val_lines = []
        test_lines = []

        required_categories = 0
        if percent_train > 0:
            required_categories += 1
        if percent_val > 0:
            required_categories += 1
        if percent_test > 0:
            required_categories += 1

        if not lines or len(lines) < required_categories or len(lines) < min_per_category:
            logger.warning('Not enough images for this category')
            labels.pop()
        else:
            random.shuffle(lines)
            a, b = three_way_split_indices(len(lines), percent_val, percent_test)
            train_lines = lines[:a]
            val_lines = lines[a:b]
            test_lines = lines[b:]

        if train_lines:
            train_outfile.write('\n'.join(train_lines) + '\n')
            train_count += len(train_lines)
        if val_lines:
            val_outfile.write('\n'.join(val_lines) + '\n')
            val_count += len(val_lines)
        if test_lines:
            test_outfile.write('\n'.join(test_lines) + '\n')
            test_count += len(test_lines)

        subdir_index += 1
        logger.debug('Progress: %0.2f' % (float(subdir_index)/len(subdirs)))

    if percent_train:
        train_outfile.close()
    if percent_val:
        val_outfile.close()
    if percent_test:
        test_outfile.close()

    if create_labels:
        if len(labels) < 2:
            logger.error('Did not find two valid categories')
            return False
        else:
            with open(labels_file, 'w') as labels_outfile:
                labels_outfile.write('\n'.join(labels) + '\n')

    logger.info('Found %d images in %d categories.' % (train_count + val_count + test_count, len(labels)))
    logger.info('Selected %d for training.' % train_count)
    logger.info('Selected %d for validation.' % val_count)
    logger.info('Selected %d for testing.' % test_count)
    return True
Parses a folder of images into three textfiles
dataset/ETHPy150Open NVIDIA/DIGITS/tools/parse_folder.py/parse_folder