Skip to content

Instantly share code, notes, and snippets.

@BernardoGO

BernardoGO/split.py

Created Apr 22, 2020
Embed
What would you like to do?
from glob import glob
import shutil
import random
import os
classes = glob("500x500/*")
outputPath = "output/"
test_percentage = 0.1
n_class = len(classes)
samples2 = []
total = 0
tt = 0
for clss in range(0, len(classes)):
imgPaths = glob(classes[clss]+"/*")
tstSam = int(len(imgPaths)*test_percentage)
print("Class {}:\n\tPath: {}\n\tTotal Count: {}\n\tSampled Imgs: {}".format(clss,classes[clss],len(imgPaths),tstSam))
smp = random.sample(imgPaths, tstSam)
for pt in smp:
os.makedirs(outputPath+os.path.basename(classes[clss]), exist_ok=True)
os.rename(pt, outputPath+os.path.basename(classes[clss])+"/"+os.path.basename(pt))
samples2.append(smp)
tt += len(imgPaths)
total += tstSam
print("-"*64 + "\nTotal: {}/{}".format(total,tt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.