""" run this file to generate the yolo format dataset """ import os import yaml import shutil import random # the path of the dataset data_path = '/home/carey/Projects/GoldenAssistant/SystemCode/core/yolo/heroes/dataset' train_ratio = 0.8 val_ratio = 0.1 test_ratio = 0.1 raw_data_path = '.' dataset_augment = True raw_image_path = os.path.join(raw_data_path, 'images') raw_label_path = os.path.join(raw_data_path, 'labels') new_image_path = os.path.join(data_path, 'images') new_label_path = os.path.join(data_path, 'labels') new_image_train_path = os.path.join(data_path, 'images/train') new_label_train_path = os.path.join(data_path, 'labels/train') new_image_val_path = os.path.join(data_path, 'images/val') new_label_val_path = os.path.join(data_path, 'labels/val') new_image_test_path = os.path.join(data_path, 'images/test') new_label_test_path = os.path.join(data_path, 'labels/test') # create the new folders if os.path.exists(data_path): shutil.rmtree(data_path) os.makedirs(new_image_path, exist_ok=True) os.makedirs(new_label_path, exist_ok=True) os.makedirs(new_image_train_path, exist_ok=True) os.makedirs(new_label_train_path, exist_ok=True) os.makedirs(new_image_val_path, exist_ok=True) os.makedirs(new_label_val_path, exist_ok=True) os.makedirs(new_image_test_path, exist_ok=True) os.makedirs(new_label_test_path, exist_ok=True) # get all the image files image_files = [os.path.join(raw_image_path, f) for f in os.listdir(raw_image_path) if f.endswith('.jpg')] # get all the label files label_files = [os.path.join(raw_label_path, f) for f in os.listdir(raw_label_path) if f.endswith('.txt')] if dataset_augment: augment_images = [(os.path.join('images_augmented', 'images', f), os.path.join('images_augmented', 'labels', f.replace('.jpg', '.txt'))) for f in os.listdir(os.path.join('images_augmented', 'images')) if f.endswith('.jpg')] # augment_labels = [os.path.join('images_augmented', 'labels', f) for f in os.listdir(os.path.join('images_augmented', 'labels')) if f.endswith('.txt')] image_files += augment_images # get the total number of the dataset total_num = len(image_files) # get the number of the train, val, test dataset train_num = int(total_num * train_ratio) val_num = int(total_num * val_ratio) test_num = total_num - train_num - val_num # train_files train_images = random.sample(image_files, k=train_num) val_images = random.sample(list(set(image_files) - set(train_images)), k=val_num) test_images = list(set(image_files) - set(train_images) - set(val_images)) # copy the train images and labels for image in train_images: if type(image) == tuple: shutil.copy(image[0], new_image_train_path) shutil.copy(image[1], new_label_train_path) continue shutil.copy(image, new_image_train_path) shutil.copy(image.replace('.jpg', '.txt').replace('images', 'labels'), new_label_train_path) # copy the val images and labels for image in val_images: if type(image) == tuple: shutil.copy(image[0], new_image_val_path) shutil.copy(image[1], new_label_val_path) continue shutil.copy(image, new_image_val_path) shutil.copy(image.replace('.jpg', '.txt').replace('images', 'labels'), new_label_val_path) # copy the test images and labels for image in test_images: if type(image) == tuple: shutil.copy(image[0], new_image_test_path) shutil.copy(image[1], new_label_test_path) continue shutil.copy(image, new_image_test_path) shutil.copy(image.replace('.jpg', '.txt').replace('images', 'labels'), new_label_test_path) # load names with open('datasets.yaml', 'r') as f: label_info = yaml.load(f, Loader=yaml.FullLoader) # generate yolo format yaml file data = dict( path=f'{os.path.abspath(data_path)}', train='images/train', val=f'images/val', test=f'images/test', names=label_info['names'] ) with open(f'{data_path}/dataset.yaml', 'w') as f: yaml.dump(data, f, allow_unicode=True)