-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_data.py
More file actions
77 lines (63 loc) · 2.64 KB
/
prepare_data.py
File metadata and controls
77 lines (63 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import shutil
from pathlib import Path
import random
def create_yolo_labels(image_path, ripeness_level):
"""Create YOLO format label for an image"""
# In YOLO format, we'll place the apple in the center of the image
# with a reasonable size (80% of image width and height)
return f"{ripeness_level} 0.5 0.5 0.8 0.8\n"
def prepare_dataset():
# Create necessary directories
os.makedirs('data/train/images', exist_ok=True)
os.makedirs('data/train/labels', exist_ok=True)
os.makedirs('data/val/images', exist_ok=True)
os.makedirs('data/val/labels', exist_ok=True)
# Define ripeness levels and their corresponding class indices
ripeness_levels = {
'20%': 0,
'40%': 1,
'60%': 2,
'80%': 3,
'100%': 4
}
# Source directory
source_dir = 'data/APPLE RIPENESS LEVELS IMAGE DATASET'
# Process each ripeness level
for ripeness, class_idx in ripeness_levels.items():
source_path = os.path.join(source_dir, ripeness)
if not os.path.exists(source_path):
print(f"Warning: Directory {source_path} not found")
continue
# Get all images in the directory
images = [f for f in os.listdir(source_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
# Randomly split into train and validation sets (80% train, 20% val)
random.shuffle(images)
split_idx = int(len(images) * 0.8)
train_images = images[:split_idx]
val_images = images[split_idx:]
# Process training images
for img in train_images:
# Copy image
src_img = os.path.join(source_path, img)
dst_img = os.path.join('data/train/images', f"{ripeness}_{img}")
shutil.copy2(src_img, dst_img)
# Create label
label = create_yolo_labels(dst_img, class_idx)
label_path = os.path.join('data/train/labels', f"{ripeness}_{os.path.splitext(img)[0]}.txt")
with open(label_path, 'w') as f:
f.write(label)
# Process validation images
for img in val_images:
# Copy image
src_img = os.path.join(source_path, img)
dst_img = os.path.join('data/val/images', f"{ripeness}_{img}")
shutil.copy2(src_img, dst_img)
# Create label
label = create_yolo_labels(dst_img, class_idx)
label_path = os.path.join('data/val/labels', f"{ripeness}_{os.path.splitext(img)[0]}.txt")
with open(label_path, 'w') as f:
f.write(label)
print("Dataset preparation completed!")
if __name__ == '__main__':
prepare_dataset()