{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Slicing with SAHI" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/obss/sahi/blob/main/demo/slicing.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 0. Preparation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Be sure current working directory is 'demo/' folder:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "os.getcwd()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Import required functions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "from PIL import Image, ImageDraw\n", "\n", "from sahi.slicing import slice_coco\n", "from sahi.utils.file import load_json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Slicing COCO Dataset into Grids" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize original image and annotations:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "coco_dict = load_json(\"demo_data/terrain2_coco.json\")\n", "\n", "f, axarr = plt.subplots(1, 1, figsize=(12, 12))\n", "# read image\n", "img_ind = 0\n", "img = Image.open(\"demo_data/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert(\"RGBA\")\n", "# iterate over all annotations\n", "for ann_ind in range(len(coco_dict[\"annotations\"])):\n", " # convert coco bbox to pil bbox\n", " xywh = coco_dict[\"annotations\"][ann_ind][\"bbox\"]\n", " xyxy = [xywh[0], xywh[1], xywh[0] + xywh[2], xywh[1] + xywh[3]]\n", " # visualize bbox over image\n", " ImageDraw.Draw(img, \"RGBA\").rectangle(xyxy, width=5)\n", "axarr.imshow(img)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To slice a COCO dataset annoations an images, we need to specify slice parameters. In this example we will slice images into 256x256 grids overlap ratio of 0.2:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "coco_dict, coco_path = slice_coco(\n", " coco_annotation_file_path=\"demo_data/terrain2_coco.json\",\n", " image_dir=\"demo_data/\",\n", " output_coco_annotation_file_name=\"sliced_coco.json\",\n", " ignore_negative_samples=False,\n", " output_dir=\"demo_data/sliced/\",\n", " slice_height=256,\n", " slice_width=256,\n", " overlap_height_ratio=0.2,\n", " overlap_width_ratio=0.2,\n", " min_area_ratio=0.1,\n", " verbose=True,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize sliced images:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f, axarr = plt.subplots(4, 5, figsize=(13, 13))\n", "img_ind = 0\n", "for ind1 in range(4):\n", " for ind2 in range(5):\n", " img = Image.open(\"demo_data/sliced/\" + coco_dict[\"images\"][img_ind][\"file_name\"])\n", " axarr[ind1, ind2].imshow(img)\n", " img_ind += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Visualize sliced annotations on sliced images:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "f, axarr = plt.subplots(4, 5, figsize=(13, 13))\n", "img_ind = 0\n", "for row_ind in range(4):\n", " for column_ind in range(5):\n", " # read image\n", " img = Image.open(\"demo_data/sliced/\" + coco_dict[\"images\"][img_ind][\"file_name\"]).convert(\"RGBA\")\n", " # iterate over all annotations\n", " for ann_ind in range(len(coco_dict[\"annotations\"])):\n", " # find annotations that belong the selected image\n", " if coco_dict[\"annotations\"][ann_ind][\"image_id\"] == coco_dict[\"images\"][img_ind][\"id\"]:\n", " # convert coco bbox to pil bbox\n", " xywh = coco_dict[\"annotations\"][ann_ind][\"bbox\"]\n", " xyxy = [xywh[0], xywh[1], xywh[0] + xywh[2], xywh[1] + xywh[3]]\n", " # visualize bbox over image\n", " ImageDraw.Draw(img, \"RGBA\").rectangle(xyxy, width=5)\n", " axarr[row_ind, column_ind].imshow(img)\n", " img_ind += 1" ] } ], "metadata": { "kernelspec": { "display_name": "Python3.8 (sahi)", "language": "python", "name": "sahi" }, "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }