Commit ·
915a8e8
1
Parent(s): 1aff156
feat: add data preparation script for YOLOv8
Browse files- prepare_cheerios_yolov8.py +126 -0
prepare_cheerios_yolov8.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
import shutil
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from scipy.spatial.transform import Rotation as R
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
# --- Configuration ---
|
| 9 |
+
SOURCE_DATA_DIR = Path('/home/pik/dev/hackwhyd')
|
| 10 |
+
YOLO_DIR = SOURCE_DATA_DIR / 'YOLOv8'
|
| 11 |
+
DEST_DATA_DIR = YOLO_DIR / 'data' / 'cheerios'
|
| 12 |
+
CLASS_NAME = 'cheerios'
|
| 13 |
+
CLASS_ID = 0
|
| 14 |
+
|
| 15 |
+
# --- Load Camera Intrinsics ---
|
| 16 |
+
k_matrix = np.load(SOURCE_DATA_DIR / 'k_matrix.npy')
|
| 17 |
+
|
| 18 |
+
# --- Image Dimensions (assuming all images are the same size) ---
|
| 19 |
+
IMG_WIDTH = 1920
|
| 20 |
+
IMG_HEIGHT = 1080
|
| 21 |
+
|
| 22 |
+
# --- Create Destination Directories ---
|
| 23 |
+
DEST_IMAGES_DIR = DEST_DATA_DIR / 'images'
|
| 24 |
+
DEST_LABELS_DIR = DEST_DATA_DIR / 'labels'
|
| 25 |
+
DEST_IMAGES_DIR.mkdir(parents=True, exist_ok=True)
|
| 26 |
+
DEST_LABELS_DIR.mkdir(parents=True, exist_ok=True)
|
| 27 |
+
|
| 28 |
+
def project_to_2d(p, q, k_matrix):
|
| 29 |
+
# (x,y,z) -> (y,-z,x) is the transformation from camera to view (the frame of reference you do projections in)
|
| 30 |
+
l = 15
|
| 31 |
+
r = R.from_quat(q)
|
| 32 |
+
x = p + r.apply(np.array([l, 0, 0]))
|
| 33 |
+
y = p + r.apply(np.array([0, l, 0]))
|
| 34 |
+
z = p + r.apply(np.array([0, 0, l]))
|
| 35 |
+
x = np.array([x[1], -x[2], x[0]])
|
| 36 |
+
y = np.array([y[1], -y[2], y[0]])
|
| 37 |
+
z = np.array([z[1], -z[2], z[0]])
|
| 38 |
+
x = np.dot(k_matrix, x)
|
| 39 |
+
y = np.dot(k_matrix, y)
|
| 40 |
+
z = np.dot(k_matrix, z)
|
| 41 |
+
x /= x[2]
|
| 42 |
+
y /= y[2]
|
| 43 |
+
z /= z[2]
|
| 44 |
+
return x, y, z
|
| 45 |
+
|
| 46 |
+
def process_dataset_split(split_name):
|
| 47 |
+
"""Processes a dataset split (e.g., 'train', 'val')."""
|
| 48 |
+
source_split_dir = SOURCE_DATA_DIR / split_name
|
| 49 |
+
source_labels_dir = source_split_dir / 'labels'
|
| 50 |
+
source_images_dir = source_split_dir / 'images'
|
| 51 |
+
|
| 52 |
+
if not source_labels_dir.exists():
|
| 53 |
+
print(f"Warning: Label directory not found for split '{split_name}': {source_labels_dir}")
|
| 54 |
+
return
|
| 55 |
+
|
| 56 |
+
for label_file in sorted(source_labels_dir.glob('*.txt')):
|
| 57 |
+
# --- Read Original Label ---
|
| 58 |
+
with open(label_file, 'r') as f:
|
| 59 |
+
original_line = f.readline().strip()
|
| 60 |
+
|
| 61 |
+
parts = original_line.split()
|
| 62 |
+
|
| 63 |
+
p = np.asarray([float(parts[3]), float(parts[4]), float(parts[5])])
|
| 64 |
+
q = np.asarray([float(parts[6]), float(parts[7]), float(parts[8]), float(parts[9])])
|
| 65 |
+
|
| 66 |
+
x, y, z = project_to_2d(p, q, k_matrix)
|
| 67 |
+
|
| 68 |
+
x_coords = [x[0], y[0], z[0]]
|
| 69 |
+
y_coords = [x[1], y[1], z[1]]
|
| 70 |
+
|
| 71 |
+
x_min = min(x_coords)
|
| 72 |
+
y_min = min(y_coords)
|
| 73 |
+
x_max = max(x_coords)
|
| 74 |
+
y_max = max(y_coords)
|
| 75 |
+
|
| 76 |
+
x_center = (x_min + x_max) / 2
|
| 77 |
+
y_center = (y_min + y_max) / 2
|
| 78 |
+
width = x_max - x_min
|
| 79 |
+
height = y_max - y_min
|
| 80 |
+
|
| 81 |
+
# Normalize
|
| 82 |
+
x_center /= IMG_WIDTH
|
| 83 |
+
y_center /= IMG_HEIGHT
|
| 84 |
+
width /= IMG_WIDTH
|
| 85 |
+
height /= IMG_HEIGHT
|
| 86 |
+
|
| 87 |
+
# --- Write New Label File ---
|
| 88 |
+
dest_label_file = DEST_LABELS_DIR / label_file.name
|
| 89 |
+
with open(dest_label_file, 'w') as f:
|
| 90 |
+
f.write(f"{CLASS_ID} {x_center} {y_center} {width} {height}\n")
|
| 91 |
+
|
| 92 |
+
# --- Copy Image ---
|
| 93 |
+
image_name = label_file.stem + '.jpg'
|
| 94 |
+
source_image_path = source_images_dir / image_name
|
| 95 |
+
dest_image_path = DEST_IMAGES_DIR / image_name
|
| 96 |
+
if source_image_path.exists():
|
| 97 |
+
shutil.copy(source_image_path, dest_image_path)
|
| 98 |
+
else:
|
| 99 |
+
print(f"Warning: Image not found: {source_image_path}")
|
| 100 |
+
|
| 101 |
+
# --- Process Train and Validation Sets ---
|
| 102 |
+
print("Processing train set...")
|
| 103 |
+
process_dataset_split('train')
|
| 104 |
+
print("Processing val set...")
|
| 105 |
+
process_dataset_split('val')
|
| 106 |
+
|
| 107 |
+
# --- Create cheerios.yaml ---
|
| 108 |
+
yaml_path = YOLO_DIR / 'cheerios_yolov8.yaml'
|
| 109 |
+
|
| 110 |
+
yaml_content = f"""
|
| 111 |
+
path: {DEST_DATA_DIR.resolve()}
|
| 112 |
+
train: images
|
| 113 |
+
val: images
|
| 114 |
+
|
| 115 |
+
# number of classes
|
| 116 |
+
nc: 1
|
| 117 |
+
|
| 118 |
+
# class names
|
| 119 |
+
names: ['{CLASS_NAME}']
|
| 120 |
+
"""
|
| 121 |
+
with open(yaml_path, 'w') as f:
|
| 122 |
+
f.write(yaml_content)
|
| 123 |
+
|
| 124 |
+
print("\nDataset preparation complete.")
|
| 125 |
+
print(f"New dataset created at: {DEST_DATA_DIR}")
|
| 126 |
+
print(f"Config file created at: {yaml_path}")
|