{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"! pip install -q pycocotools","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:39:34.083271Z","iopub.execute_input":"2023-06-18T21:39:34.083753Z","iopub.status.idle":"2023-06-18T21:39:47.204339Z","shell.execute_reply.started":"2023-06-18T21:39:34.083721Z","shell.execute_reply":"2023-06-18T21:39:47.202978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom PIL import Image, ImageDraw, ImageFont\nfrom pycocotools.coco import COCO","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-18T21:47:52.708580Z","iopub.execute_input":"2023-06-18T21:47:52.708993Z","iopub.status.idle":"2023-06-18T21:47:52.715536Z","shell.execute_reply.started":"2023-06-18T21:47:52.708963Z","shell.execute_reply":"2023-06-18T21:47:52.713813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file = (\n    \"/kaggle/input/dlsprint2/badlad/labels/coco_format/\"\n    \"train/badlad-train-coco.json\"\n)\n\ntrain_dir = Path(\"/kaggle/input/dlsprint2/badlad/images/train\")\n\ntrain_annotations = COCO(annotation_file=train_file)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:39:47.217172Z","iopub.execute_input":"2023-06-18T21:39:47.217515Z","iopub.status.idle":"2023-06-18T21:39:54.315387Z","shell.execute_reply.started":"2023-06-18T21:39:47.217468Z","shell.execute_reply":"2023-06-18T21:39:54.314169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_ids = train_annotations.getCatIds()\nprint(f\"Number of Unique Categories: {len(cat_ids)}\")\nprint(\"Category IDs:\")\nprint(cat_ids) \n\ncats = train_annotations.loadCats(cat_ids)\ncat_names = [cat[\"name\"] for cat in cats]\nprint(\"Categories Names:\")\nprint(cat_names)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:39:54.318213Z","iopub.execute_input":"2023-06-18T21:39:54.318706Z","iopub.status.idle":"2023-06-18T21:39:54.325817Z","shell.execute_reply.started":"2023-06-18T21:39:54.318665Z","shell.execute_reply":"2023-06-18T21:39:54.324831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for cat_id, cat_name in zip(cat_ids, cat_names):\n    img_ids = train_annotations.getImgIds(catIds=[cat_id])\n    print(f\"Number of Images Containing {cat_name}: {len(img_ids)}\")","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:39:54.327412Z","iopub.execute_input":"2023-06-18T21:39:54.327899Z","iopub.status.idle":"2023-06-18T21:39:54.391610Z","shell.execute_reply.started":"2023-06-18T21:39:54.327859Z","shell.execute_reply":"2023-06-18T21:39:54.390363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_id = 10\nann_ids = train_annotations.getAnnIds(imgIds=[img_id])\nanns = train_annotations.loadAnns(ann_ids)\nimg_file = train_annotations.loadImgs([img_id])[0][\"file_name\"]\nimg = Image.open(train_dir/img_file)\n\nplt.figure(figsize=(15, 15))\nplt.axis(\"off\")\nplt.imshow(np.asarray(img))\ntrain_annotations.showAnns(anns, draw_bbox=False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:48:18.655640Z","iopub.execute_input":"2023-06-18T21:48:18.656117Z","iopub.status.idle":"2023-06-18T21:48:19.580634Z","shell.execute_reply.started":"2023-06-18T21:48:18.656083Z","shell.execute_reply":"2023-06-18T21:48:19.579587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_names = []\nbboxes = []\nfor ann in anns:\n    cat_id, bbox = ann[\"category_id\"], ann[\"bbox\"]\n    cat_names.append(train_annotations.loadCats([cat_id])[0][\"name\"])\n    bboxes.append(bbox)","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:52:36.301657Z","iopub.execute_input":"2023-06-18T21:52:36.302101Z","iopub.status.idle":"2023-06-18T21:52:36.309166Z","shell.execute_reply.started":"2023-06-18T21:52:36.302069Z","shell.execute_reply":"2023-06-18T21:52:36.307659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_bbox(img, bboxes, cat_names):\n    draw = ImageDraw.Draw(img)\n    font = ImageFont.load_default()\n    \n    for box, cat_name in zip(bboxes, cat_names):\n        x1, y1, w, h = box\n        x2, y2 = x1 + w, y1 + h\n        \n        draw.rectangle((x1, y1, x2, y2), outline = \"violet\")\n        draw.text(\n            (x1 + 10, y1 - 10),\n            text=cat_name,\n            fill=\"green\",\n            font=font,\n        )\n    return img","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:57:16.238983Z","iopub.execute_input":"2023-06-18T21:57:16.239393Z","iopub.status.idle":"2023-06-18T21:57:16.247744Z","shell.execute_reply.started":"2023-06-18T21:57:16.239363Z","shell.execute_reply":"2023-06-18T21:57:16.246510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plot_bbox(img, bboxes, cat_names)\nimg","metadata":{"execution":{"iopub.status.busy":"2023-06-18T21:57:57.287637Z","iopub.execute_input":"2023-06-18T21:57:57.288047Z","iopub.status.idle":"2023-06-18T21:57:57.712704Z","shell.execute_reply.started":"2023-06-18T21:57:57.288011Z","shell.execute_reply":"2023-06-18T21:57:57.711750Z"},"trusted":true},"execution_count":null,"outputs":[]}]}