{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# =====================================================================\n",
    "# [Cell 1] 설정 – 경로 지정\n",
    "# =====================================================================\n",
    "import json\n",
    "from pathlib import Path\n",
    "\n",
    "# (1) 원본 COCO JSON (이미 변환해 두신 파일)\n",
    "SRC_JSON = Path(\"/data/ai-hub/fire-dataset/106.화재_발생_예측_데이터(연기_동영상)/01.데이터/fire_dataset_val.json\")\n",
    "\n",
    "# (2) 정리된(이미지 존재 확인 후) JSON 저장 경로\n",
    "CLEAN_JSON = Path(\"/data/ai-hub/fire-dataset/106.화재_발생_예측_데이터(연기_동영상)/01.데이터/fire_dataset_val_clean.json\")\n",
    "\n",
    "print(\"원본 JSON:\", SRC_JSON)\n",
    "print(\"클린 JSON:\", CLEAN_JSON)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# =====================================================================\n",
    "# [Cell 2] 클린업 로직 – 이미지 존재 여부 대조 후 필터링\n",
    "# =====================================================================\n",
    "import json\n",
    "\n",
    "# 1) JSON 불러오기\n",
    "with open(SRC_JSON, \"r\", encoding=\"utf-8\") as f:\n",
    "    coco = json.load(f)\n",
    "\n",
    "# 2) 이미지 경로 존재 여부 확인\n",
    "valid_image_ids = set()\n",
    "clean_images = []\n",
    "missing = 0\n",
    "\n",
    "for img in coco[\"images\"]:\n",
    "    img_path = Path(img[\"file_name\"])\n",
    "    if img_path.exists():\n",
    "        clean_images.append(img)\n",
    "        valid_image_ids.add(img[\"id\"])\n",
    "    else:\n",
    "        missing += 1\n",
    "\n",
    "print(f\"✓ images total: {len(coco['images'])}, missing files: {missing}\")\n",
    "\n",
    "# 3) 해당 image_id 만 쓰는 annotations 필터링\n",
    "clean_anns = [ann for ann in coco[\"annotations\"]\n",
    "              if ann[\"image_id\"] in valid_image_ids]\n",
    "\n",
    "print(f\"✓ annotations total: {len(coco['annotations'])}, after filter: {len(clean_anns)}\")\n",
    "\n",
    "# 4) 카테고리·licenses·info 는 그대로 복사\n",
    "out_coco = {\n",
    "    \"info\"       : coco.get(\"info\", {}),\n",
    "    \"licenses\"   : coco.get(\"licenses\", []),\n",
    "    \"categories\" : coco.get(\"categories\", []),\n",
    "    \"images\"     : clean_images,\n",
    "    \"annotations\": clean_anns\n",
    "}\n",
    "\n",
    "# 5) 저장\n",
    "CLEAN_JSON.parent.mkdir(parents=True, exist_ok=True)\n",
    "with open(CLEAN_JSON, \"w\", encoding=\"utf-8\") as f:\n",
    "    json.dump(out_coco, f, ensure_ascii=False, indent=2)\n",
    "\n",
    "print(\"✅ Clean JSON saved →\", CLEAN_JSON)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "250520_mmyolo",
   "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.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
