samedi 26 janvier 2019

How to split and copy files from multiple folder in train, test and validation folder

I have a dataset that is composed by 130 folders containing 32 photos each.

From each folder, I want to copy randomly that photos (26 for training, 3 for testing and 3 for validation) to the the respective subfolder (001, 002, 003...) in train, validation and test folder. So I'll have something like this:

  • Train set

    • 001 (folder contain 26 photo)
    • 002
    • 003
    • ....
  • Validation set

    • 001 (folder contain 3 photos)
    • 002
    • 003
    • ....
  • Train set

    • 001 (folder contain 3 photos)
    • 002
    • 003
    • ....

This is the code:

import random
import shutil

n_photo_train = 26
n_photo_validation = 3
n_photo_test = 3

for idx in range(130):

    source = '/Users/john/photodb_original/{d:03d}'.format(d=(idx + 1))
    dest_train = '/Users/john/photodb_sets/Train/{d:03d}'.format(d=(idx + 1))
    dest_validation = '/Users/john/photodb_sets/Validation/{d:03d}'.format(d=(idx + 1))
    dest_test = '/Users/john/photodb_sets/Test/{d:03d}'.format(d=(idx + 1))

    files = random.choice(os.listdir(source))
    photo_train = files[:n_photo_train]
    photo_test = files[26:29]
    photo_train = files[29:]

    shutil.copyfile(os.path.join(source, photo_train), dest_train)
    shutil.copyfile(os.path.join(source, photo_test), dest_validation)
    shutil.copyfile(os.path.join(source, photo_train), dest_test)

I get this error: IsADirectoryError: [Errno 21] Is a directory: '/Users/john/photodb_original/001/'




Aucun commentaire:

Enregistrer un commentaire