David Kagramanyan commited on
Commit
9082623
1 Parent(s): 24e5fc7

added notebook

Browse files
Files changed (1) hide show
  1. label_studio2yolo.ipynb +112 -0
label_studio2yolo.ipynb ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "1bba141c-5345-4833-960e-59a5e65f08b8",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import os\n",
11
+ "import shutil\n",
12
+ "import random"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "markdown",
17
+ "id": "57ee26a6-ba9b-4228-847a-8fdb9dca42bd",
18
+ "metadata": {
19
+ "tags": []
20
+ },
21
+ "source": [
22
+ "https://harminder.dev/projects/ai-powered-property-surveillance/training/#create-a-data-configuration-file"
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "code",
27
+ "execution_count": 2,
28
+ "id": "f4ee3ad6-2555-403f-a68b-c2a102649fe0",
29
+ "metadata": {},
30
+ "outputs": [
31
+ {
32
+ "name": "stdout",
33
+ "output_type": "stream",
34
+ "text": [
35
+ "Dataset successfully split into train, val, and test sets.\n"
36
+ ]
37
+ }
38
+ ],
39
+ "source": [
40
+ "# Set the seed for reproducibility\n",
41
+ "\n",
42
+ "random.seed(42)\n",
43
+ "\n",
44
+ "# Paths\n",
45
+ "\n",
46
+ "base_path = 'yolo'\n",
47
+ "images_path = os.path.join(base_path, 'images')\n",
48
+ "labels_path = os.path.join(base_path, 'labels')\n",
49
+ "\n",
50
+ "# Split Ratios\n",
51
+ "test_ratio = 0.20\n",
52
+ "\n",
53
+ "# Create directories for train, val, and test sets\n",
54
+ "\n",
55
+ "for set_type in ['train', 'test']:\n",
56
+ " for content_type in ['images', 'labels']:\n",
57
+ " os.makedirs(os.path.join(base_path, set_type, content_type), exist_ok=True)\n",
58
+ "\n",
59
+ "# Get all image filenames\n",
60
+ "\n",
61
+ "all_files = [f for f in os.listdir(images_path) if os.path.isfile(os.path.join(images_path, f))]\n",
62
+ "random.shuffle(all_files)\n",
63
+ "\n",
64
+ "# Calculate split indices\n",
65
+ "\n",
66
+ "total_files = len(all_files)\n",
67
+ "train_end = int(total_files*test_ratio)\n",
68
+ "\n",
69
+ "# Split files\n",
70
+ "\n",
71
+ "test_files = all_files[:train_end]\n",
72
+ "train_files = all_files[train_end:]\n",
73
+ "\n",
74
+ "# Function to copy files\n",
75
+ "\n",
76
+ "def copy_files(files, set_type):\n",
77
+ " for file in files: # Copy image\n",
78
+ " shutil.copy(os.path.join(images_path, file), os.path.join(base_path, set_type, 'images')) # Copy corresponding label\n",
79
+ " label_file = file.rsplit('.', 1)[0] + '.txt'\n",
80
+ " shutil.copy(os.path.join(labels_path, label_file), os.path.join(base_path, set_type, 'labels'))\n",
81
+ "\n",
82
+ "# Copy files to respective directories\n",
83
+ "\n",
84
+ "copy_files(train_files, 'train')\n",
85
+ "copy_files(test_files, 'test')\n",
86
+ "\n",
87
+ "print(\"Dataset successfully split into train, val, and test sets.\")"
88
+ ]
89
+ }
90
+ ],
91
+ "metadata": {
92
+ "kernelspec": {
93
+ "display_name": "torch",
94
+ "language": "python",
95
+ "name": "torch"
96
+ },
97
+ "language_info": {
98
+ "codemirror_mode": {
99
+ "name": "ipython",
100
+ "version": 3
101
+ },
102
+ "file_extension": ".py",
103
+ "mimetype": "text/x-python",
104
+ "name": "python",
105
+ "nbconvert_exporter": "python",
106
+ "pygments_lexer": "ipython3",
107
+ "version": "3.11.5"
108
+ }
109
+ },
110
+ "nbformat": 4,
111
+ "nbformat_minor": 5
112
+ }