{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wINm_lPYZhlO"
      },
      "source": [
        "# Convert label me annotations to COCO JSON format"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jlmasaZNtJ6C"
      },
      "source": [
        "Given the images and their corresponding annotated JSON files exported from the \"labelme\" tool. Our goal is to generate COCO format JSON files."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "M-LDdP-NtT3F"
      },
      "source": [
        "We will use an open source library called labelme2coco to convert all the JSON files from \"labelme\" tool to COCO JSON format."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RojnXi7lfLSA"
      },
      "source": [
        "Put the annotated JSON files and their corresponding images in the same folder and create another folder for storing the output COCO JSON file and then use the labelme2coco tool to export the output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Ud4OzlNMtHv7"
      },
      "outputs": [],
      "source": [
        "# install the library and RESTART runtime\n",
        "!pip install labelme2coco"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nWgcQzpHt4hq"
      },
      "outputs": [],
      "source": [
        "# import the library\n",
        "import labelme2coco\n",
        "import json"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "executionInfo": {
          "elapsed": 56,
          "status": "ok",
          "timestamp": 1660342052001,
          "user": {
            "displayName": "",
            "userId": ""
          },
          "user_tz": 420
        },
        "id": "3Wt3WELFXO5o"
      },
      "outputs": [],
      "source": [
        "# Import a sample annotation file exported from \"labelme\" tool and its corresponding image\n",
        "!curl -O https://raw.githubusercontent.com/tensorflow/models/master/official/projects/\\\n",
        "waste_identification_ml/pre_processing/config/sample_json/ffdeb4cd-43ba-4ca0-a1e6-aa5824005f44.json\n",
        "\n",
        "# Import the corresponding image file mentioned in the annotation file\n",
        "!curl -O https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/\\\n",
        "pre_processing/config/sample_images/ffdeb4cd-43ba-4ca0-a1e6-aa5824005f44.jpg"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9A8aivRPXXqF",
        "outputId": "047b6300-5e58-4d0d-c993-a3a29ce98966"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "dict_keys(['version', 'flags', 'shapes', 'imagePath', 'imageData', 'imageHeight', 'imageWidth'])"
            ]
          },
          "execution_count": 65,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# check the content of the file exported from the \"labelme\" tool\n",
        "with open('ffdeb4cd-43ba-4ca0-a1e6-aa5824005f44.json') as json_file:\n",
        "    data = json.load(json_file)\n",
        "data.keys()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2wQKjve3XpG7"
      },
      "outputs": [],
      "source": [
        "%%bash\n",
        "\n",
        "# set input directory that contains labelme annotations and image files\n",
        "mkdir labelme_folder\n",
        "\n",
        "# set output dir\n",
        "mkdir export_dir\n",
        "\n",
        "# put all the annotation files exported from \"labelme\" tool and their corresponding images!ls in the labelme_folder\n",
        "mv ffdeb4cd-43ba-4ca0-a1e6-aa5824005f44.json labelme_folder/\n",
        "mv ffdeb4cd-43ba-4ca0-a1e6-aa5824005f44.jpg labelme_folder/"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "i5G7dOOycExg",
        "outputId": "f235fe9c-7ad8-439a-c89d-26a331ecc31c"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "There are 1 listed files in folder .\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "Converting labelme annotations to COCO format: 100%|██████████| 1/1 [00:00\u003c00:00, 171.26it/s]\n"
          ]
        }
      ],
      "source": [
        "input = '/content/labelme_folder/'\n",
        "output = '/content/export_dir/'\n",
        "\n",
        "# it will combine all the JSON files and convert them to COCO JSON format\n",
        "labelme2coco.convert(input, output)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "S6jqV6PtbOtA",
        "outputId": "15e02124-6558-4427-ec88-5f0bccbbb33e"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "dict_keys(['images', 'annotations', 'categories'])\n",
            "[{'id': 0, 'name': 'plastics_HDPE_flexible_color_SAchets-\u0026-pouch_pouch', 'supercategory': 'plastics_HDPE_flexible_color_SAchets-\u0026-pouch_pouch'}]\n",
            "[{'height': 2048, 'width': 2592, 'id': 1, 'file_name': 'ffdeb4cd-43ba-4ca0-a1e6-aa5824005f44.jpg'}]\n",
            "[832, 255, 729, 697]\n"
          ]
        }
      ],
      "source": [
        "# check the content of the output COCO JSON format\n",
        "with open('export_dir/dataset.json') as json_file:\n",
        "    data = json.load(json_file)\n",
        "print(data.keys())\n",
        "print(data['categories'])\n",
        "print(data['images'])\n",
        "print(data['annotations'][0]['bbox'])"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "labelme_to_coco.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
