{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ab3f404a-49d4-49a8-ba40-ae311d74a561",
   "metadata": {},
   "source": [
    "# Extract file name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c3a3fc0f-8269-474f-9d78-31f97081b9da",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "# Load COCO annotations\n",
    "with open('_annotations.coco.json', 'r') as f:\n",
    "    data = json.load(f)\n",
    "\n",
    "# Create a dictionary to group annotations by image_id\n",
    "image_to_annotations = {}\n",
    "for ann in data['annotations']:\n",
    "    if ann['image_id'] not in image_to_annotations:\n",
    "        image_to_annotations[ann['image_id']] = []\n",
    "    image_to_annotations[ann['image_id']].append(ann)\n",
    "\n",
    "# Create a dictionary to map image_id to file_name\n",
    "image_id_to_name = {img['id']: img['file_name'] for img in data['images']}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "adda2476-6414-4377-ab80-1464977bd5b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Category corrison has these single class #images: 199\n",
      "Category normal welm has these single class #images: 92\n",
      "Category abnormal welm has these single class #images: 169\n",
      "Category slight crack has these single class #images: 17\n",
      "Category scale has these single class #images: 12\n"
     ]
    }
   ],
   "source": [
    "# For each image, check if all annotations belong to the same category\n",
    "single_class_images = {}\n",
    "for img_id, annotations in image_to_annotations.items():\n",
    "    categories = [ann['category_id'] for ann in annotations]\n",
    "    unique_categories = set(categories)\n",
    "    if len(unique_categories) == 1:\n",
    "        cat_id = categories[0]\n",
    "        if cat_id not in single_class_images:\n",
    "            single_class_images[cat_id] = []\n",
    "        single_class_images[cat_id].append(image_id_to_name[img_id])\n",
    "\n",
    "# The single_class_images dictionary now contains a list of file names for each category\n",
    "# where each file has instances from only that category\n",
    "for cat_id, filenames in single_class_images.items():\n",
    "    category_name = next(cat['name'] for cat in data['categories'] if cat['id'] == cat_id)\n",
    "    print(f\"Category {category_name} has these single class #images: {len(filenames)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90059e6f-3ee4-4769-bc2a-81d8b3d7eb0b",
   "metadata": {},
   "source": [
    "# Build Data Structure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "01f032ce-799f-4583-a128-20b96d5e094c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied images for category corrison to ./image_dataset/corrison\n",
      "Copied images for category normal welm to ./image_dataset/normal welm\n",
      "Copied images for category abnormal welm to ./image_dataset/abnormal welm\n",
      "Copied images for category slight crack to ./image_dataset/slight crack\n",
      "Copied images for category scale to ./image_dataset/scale\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import shutil\n",
    "import json\n",
    "\n",
    "# Create a folder for each class and copy corresponding images\n",
    "source_directory = './image/coco(json_label)/images'  # Original directory containing all images\n",
    "\n",
    "for cat_id, filenames in single_class_images.items():\n",
    "    category_name = next(cat['name'] for cat in data['categories'] if cat['id'] == cat_id)\n",
    "    destination_directory = f'./image_dataset/{category_name}'\n",
    "\n",
    "    # Create the destination directory if it doesn't exist\n",
    "    if not os.path.exists(destination_directory):\n",
    "        os.makedirs(destination_directory)\n",
    "\n",
    "    # Copy each file\n",
    "    for filename in filenames:\n",
    "        source_path = os.path.join(source_directory, filename)\n",
    "        destination_path = os.path.join(destination_directory, filename)\n",
    "        shutil.copy(source_path, destination_path)\n",
    "\n",
    "    print(f\"Copied images for category {category_name} to {destination_directory}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5b82565-2084-405f-9e29-13ffe343289e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yt38",
   "language": "python",
   "name": "yt38"
  },
  "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.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
