{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Processing Samples: 100%|██████████| 4931/4931 [6:09:48<00:00, 4.50s/it] " ] }, { "name": "stdout", "output_type": "stream", "text": [ "Dataset preparation complete.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "import os\n", "import json\n", "import requests\n", "from tqdm import tqdm\n", "\n", "# Load the JSON file\n", "json_file_path = \"Train-v2.json\" # Update this with the actual file path\n", "with open(json_file_path, 'r') as f:\n", " data = json.load(f)\n", "\n", "# Create directories for images, labels, and segments\n", "images_dir = \"images/train\"\n", "labels_dir = \"labels/train\"\n", "segments_dir = \"segments/train\"\n", "os.makedirs(images_dir, exist_ok=True)\n", "os.makedirs(labels_dir, exist_ok=True)\n", "os.makedirs(segments_dir, exist_ok=True)\n", "\n", "# Helper function to download images or segmentations\n", "def download_image(url, output_path):\n", " response = requests.get(url)\n", " if response.status_code == 200:\n", " with open(output_path, 'wb') as f:\n", " f.write(response.content)\n", "\n", "# Prepare YOLO labels\n", "category_id_mapping = {cat['id']: i-1 for i, cat in enumerate(data['dataset']['task_attributes']['categories'], 1)} # Mapping category_id to YOLO class id\n", "category_names = {i-1: cat['name'] for i, cat in enumerate(data['dataset']['task_attributes']['categories'], 1)} # YOLO class names\n", "\n", "# Iterate through the dataset samples\n", "for sample in tqdm(data['dataset']['samples'], desc=\"Processing Samples\"):\n", " label_status = sample['labels'].get('ground-truth', {}).get('label_status', 'SKIPPED')\n", " \n", " if label_status == \"LABELED\":\n", " image_url = sample['attributes']['image']['url']\n", " image_name = sample['name']\n", " image_output_path = os.path.join(images_dir, image_name)\n", "\n", " # Download the image\n", " download_image(image_url, image_output_path)\n", "\n", " # Download segmentation bitmap (if any)\n", " segmentation_url = sample['labels']['ground-truth']['attributes'].get('segmentation_bitmap', {}).get('url')\n", " if segmentation_url:\n", " seg_output_path = os.path.join(segments_dir, image_name)\n", " download_image(segmentation_url, seg_output_path)\n", "\n", " # Write annotations to label files in YOLO format\n", " label_output_path = os.path.join(labels_dir, os.path.splitext(image_name)[0] + \".txt\")\n", " annotations = sample['labels']['ground-truth']['attributes']['annotations']\n", "\n", " with open(label_output_path, 'w') as f:\n", " for annotation in annotations:\n", " category_id = annotation['category_id']\n", " yolo_class_id = category_id_mapping[category_id]\n", " # YOLO format typically expects: class_id, x_center, y_center, width, height\n", " # Since we have segmentation, we may only record class_id here for simplicity.\n", " # Modify this part to handle polygon or bounding box coordinates if necessary.\n", " f.write(f\"{yolo_class_id}\\n\")\n", "\n", "# Create YAML file for YOLO segmentation\n", "yaml_content = {\n", " 'path': '.', # Root path\n", " 'train': 'images/train', # Path to training images\n", " 'val': '', # No validation set\n", " 'names': category_names\n", "}\n", "\n", "yaml_file_path = \"dataset.yaml\"\n", "with open(yaml_file_path, 'w') as yaml_file:\n", " yaml_file.write(f\"path: {yaml_content['path']}\\n\")\n", " yaml_file.write(f\"train: {yaml_content['train']}\\n\")\n", " yaml_file.write(f\"val: {yaml_content['val']}\\n\")\n", " yaml_file.write(\"names:\\n\")\n", " for i, name in yaml_content['names'].items():\n", " yaml_file.write(f\" {i}: {name}\\n\")\n", "\n", "print(\"Dataset preparation complete.\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "sgrs", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 2 }