-
-
Save atultiwari/e84474ff40e11cb92de92a268123cbe2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import glob | |
import os | |
import shutil | |
def copyfiles(fil, root_dir): | |
basename = os.path.basename(fil) | |
filename = os.path.splitext(basename)[0] | |
# copy image | |
src = fil | |
dest = os.path.join(root_dir, image_dir, f"{filename}.jpg") | |
shutil.copyfile(src, dest) | |
# copy annotations | |
src = os.path.join(label_dir, f"{filename}.txt") | |
dest = os.path.join(root_dir, label_dir, f"{filename}.txt") | |
if os.path.exists(src): | |
shutil.copyfile(src, dest) | |
label_dir = "labels/" | |
image_dir = "images/" | |
lower_limit = 0 | |
files = glob.glob(os.path.join(image_dir, '*.jpg')) | |
random.shuffle(files) | |
folders = {"train": 0.8, "val": 0.1, "test": 0.1} | |
check_sum = sum([folders[x] for x in folders]) | |
assert check_sum == 1.0, "Split proportion is not equal to 1.0" | |
for folder in folders: | |
os.mkdir(folder) | |
temp_label_dir = os.path.join(folder, label_dir) | |
os.mkdir(temp_label_dir) | |
temp_image_dir = os.path.join(folder, image_dir) | |
os.mkdir(temp_image_dir) | |
limit = round(len(files) * folders[folder]) | |
for fil in files[lower_limit:lower_limit + limit]: | |
copyfiles(fil, folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment