| | |
| | """ |
| | Generate HuggingFace-compatible metadata CSV files for the billiards dataset. |
| | Creates train.csv, validation.csv, and test.csv with image paths and annotations. |
| | """ |
| |
|
| | import csv |
| | import os |
| | from pathlib import Path |
| |
|
| |
|
| | def read_yolo_labels(label_path): |
| | """Read YOLO format labels from a text file.""" |
| | if not os.path.exists(label_path): |
| | return [] |
| |
|
| | with open(label_path, 'r') as f: |
| | lines = f.readlines() |
| |
|
| | annotations = [] |
| | for line in lines: |
| | parts = line.strip().split() |
| | if len(parts) == 5: |
| | class_id, x_center, y_center, width, height = parts |
| | annotations.append({ |
| | 'class_id': int(class_id), |
| | 'x_center': float(x_center), |
| | 'y_center': float(y_center), |
| | 'width': float(width), |
| | 'height': float(height) |
| | }) |
| | return annotations |
| |
|
| |
|
| | def create_metadata_csv(split_name, output_filename): |
| | """Create a metadata CSV file for a given split.""" |
| | data_dir = Path('data') |
| | images_dir = data_dir / split_name / 'images' |
| | labels_dir = data_dir / split_name / 'labels' |
| |
|
| | if not images_dir.exists(): |
| | print(f"Warning: {images_dir} does not exist") |
| | return |
| |
|
| | rows = [] |
| | image_files = sorted(images_dir.glob('*.png')) |
| |
|
| | for image_path in image_files: |
| | |
| | label_filename = image_path.stem + '.txt' |
| | label_path = labels_dir / label_filename |
| |
|
| | |
| | annotations = read_yolo_labels(label_path) |
| |
|
| | |
| | relative_image_path = str(image_path) |
| |
|
| | row = { |
| | 'image': relative_image_path, |
| | 'annotations': str(annotations) |
| | } |
| | rows.append(row) |
| |
|
| | |
| | if rows: |
| | with open(output_filename, 'w', newline='') as csvfile: |
| | fieldnames = ['image', 'annotations'] |
| | writer = csv.DictWriter(csvfile, fieldnames=fieldnames) |
| | writer.writeheader() |
| | writer.writerows(rows) |
| |
|
| | print(f"Created {output_filename} with {len(rows)} entries") |
| | else: |
| | print(f"No data found for {split_name}") |
| |
|
| |
|
| | def main(): |
| | """Generate metadata CSV files for all splits.""" |
| | |
| | splits = { |
| | 'train': 'train.csv', |
| | 'val': 'validation.csv', |
| | 'test': 'test.csv' |
| | } |
| |
|
| | for split_dir, output_file in splits.items(): |
| | create_metadata_csv(split_dir, output_file) |
| |
|
| | print("\nMetadata CSV files created successfully!") |
| | print("These files are compatible with HuggingFace's dataset viewer.") |
| |
|
| |
|
| | if __name__ == '__main__': |
| | main() |
| |
|