Goyamproject/React_native_app
1
1import os
2import shutil
3import random
4from glob import glob
5from tqdm import tqdm
6
7SOURCE_ROOT = r"C:\Users\charu\Documents\goyam\roboflow\train"
8SOURCE_IMAGES = os.path.join(SOURCE_ROOT, "images")
9SOURCE_LABELS = os.path.join(SOURCE_ROOT, "labels")
10
11
12DEST_DIR = r"C:\Users\charu\Documents\goyam\roboflow\final_split"
13
14TRAIN_RATIO = 0.8
15
16
17def split_dataset():
18
19 if os.path.exists(DEST_DIR):
20 print(f"⚠️ Warning: Destination folder already exists: {DEST_DIR}")
21 print(" (Ideally, delete it before running this to avoid mixing old data!)")
22
23 for split in ['train', 'val']:
24 os.makedirs(os.path.join(DEST_DIR, split, 'images'), exist_ok=True)
25 os.makedirs(os.path.join(DEST_DIR, split, 'labels'), exist_ok=True)
26
27
28 print(f"🔍 Scanning images in: {SOURCE_IMAGES}")
29
30
31 unique_images = set()
32
33 # Check all extensions
34 exts = ['*.jpg', '*.jpeg', '*.png', '*.JPG', '*.JPEG', '*.PNG']
35 for ext in exts:
36 files = glob(os.path.join(SOURCE_IMAGES, ext))
37 for f in files:
38 unique_images.add(f)
39
40 all_images = list(unique_images)
41
42
43 random.shuffle(all_images)
44
45 total_count = len(all_images)
46 train_count = int(total_count * TRAIN_RATIO)
47
48 if total_count == 0:
49 print(" Error: No images found!")
50 return
51
52 print(f"Found {total_count} unique images.")
53 print(f" -> Training: {train_count}")
54 print(f" -> Validation: {total_count - train_count}")
55
56 # 3. Copy Files
57 print("Organizing files...")
58 for i, img_path in enumerate(tqdm(all_images)):
59 split = 'train' if i < train_count else 'val'
60
61 filename = os.path.basename(img_path)
62 name_no_ext = os.path.splitext(filename)[0]
63
64 dest_img_path = os.path.join(DEST_DIR, split, 'images', filename)
65
66 # Check label (Look for .txt)
67 src_txt_path = os.path.join(SOURCE_LABELS, name_no_ext + ".txt")
68 dest_txt_path = os.path.join(DEST_DIR, split, 'labels', name_no_ext + ".txt")
69
70 shutil.copy(img_path, dest_img_path)
71
72 if os.path.exists(src_txt_path):
73 shutil.copy(src_txt_path, dest_txt_path)
74
75 print(f"\nDone! Your dataset is ready at:")
76 print(f" {DEST_DIR}")
77
78if __name__ == "__main__":
79 split_dataset()