{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1bba141c-5345-4833-960e-59a5e65f08b8", "metadata": {}, "outputs": [], "source": [ "import os\n", "import shutil\n", "import random" ] }, { "cell_type": "markdown", "id": "57ee26a6-ba9b-4228-847a-8fdb9dca42bd", "metadata": { "tags": [] }, "source": [ "https://harminder.dev/projects/ai-powered-property-surveillance/training/#create-a-data-configuration-file" ] }, { "cell_type": "code", "execution_count": 2, "id": "f4ee3ad6-2555-403f-a68b-c2a102649fe0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dataset successfully split into train, val, and test sets.\n" ] } ], "source": [ "# Set the seed for reproducibility\n", "\n", "random.seed(42)\n", "\n", "# Paths\n", "\n", "base_path = 'yolo'\n", "images_path = os.path.join(base_path, 'images')\n", "labels_path = os.path.join(base_path, 'labels')\n", "\n", "# Split Ratios\n", "test_ratio = 0.20\n", "\n", "# Create directories for train, val, and test sets\n", "\n", "for set_type in ['train', 'test']:\n", " for content_type in ['images', 'labels']:\n", " os.makedirs(os.path.join(base_path, set_type, content_type), exist_ok=True)\n", "\n", "# Get all image filenames\n", "\n", "all_files = [f for f in os.listdir(images_path) if os.path.isfile(os.path.join(images_path, f))]\n", "random.shuffle(all_files)\n", "\n", "# Calculate split indices\n", "\n", "total_files = len(all_files)\n", "train_end = int(total_files*test_ratio)\n", "\n", "# Split files\n", "\n", "test_files = all_files[:train_end]\n", "train_files = all_files[train_end:]\n", "\n", "# Function to copy files\n", "\n", "def copy_files(files, set_type):\n", " for file in files: # Copy image\n", " shutil.copy(os.path.join(images_path, file), os.path.join(base_path, set_type, 'images')) # Copy corresponding label\n", " label_file = file.rsplit('.', 1)[0] + '.txt'\n", " shutil.copy(os.path.join(labels_path, label_file), os.path.join(base_path, set_type, 'labels'))\n", "\n", "# Copy files to respective directories\n", "\n", "copy_files(train_files, 'train')\n", "copy_files(test_files, 'test')\n", "\n", "print(\"Dataset successfully split into train, val, and test sets.\")" ] } ], "metadata": { "kernelspec": { "display_name": "torch", "language": "python", "name": "torch" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }