{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "856591ff-dff0-4f5a-b1c5-968bee640a52",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "import json\n",
    "import numpy as np\n",
    "import os\n",
    "from collections import defaultdict\n",
    "\n",
    "import natsort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a403e3bd-1486-4afd-8ba2-e1d29a71d505",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def draw_bboxes(image, annotations):\n",
    "    for annotation in annotations:\n",
    "        label = annotation[\"label\"]\n",
    "        points = np.array(annotation[\"points\"]).astype(int)\n",
    "        color = (0, 255, 0)\n",
    "        cv2.rectangle(image, tuple(points[0]), tuple(points[1]), color, 2)\n",
    "        cv2.putText(image, label, tuple(points[0]), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)\n",
    "\n",
    "def visualize_dataset_bbox(images_folder, labels_folder, output_video):\n",
    "    height, width = None, None\n",
    "    video_writer = None\n",
    "    first_frame = True\n",
    "    \n",
    "    for img_path in natsort.natsorted(os.listdir(images_folder)):\n",
    "        img_full_path = os.path.join(images_folder, img_path)\n",
    "        label_path = os.path.join(labels_folder, img_path.rsplit(\".\", 1)[0] + \".json\")\n",
    "\n",
    "        if os.path.isfile(img_full_path) and os.path.isfile(label_path):\n",
    "            with open(label_path, \"r\") as f:\n",
    "                data = json.load(f)\n",
    "\n",
    "            annotations = data[\"shapes\"]\n",
    "            image = cv2.imread(img_full_path)\n",
    "            draw_bboxes(image, annotations)\n",
    "\n",
    "            if first_frame:\n",
    "                height, width = data[\"imageHeight\"], data[\"imageWidth\"]\n",
    "                video_writer = cv2.VideoWriter(output_video, cv2.VideoWriter_fourcc(*\"mp4v\"), 1, (width, height))\n",
    "                first_frame = False\n",
    "\n",
    "            video_writer.write(image)\n",
    "\n",
    "    if video_writer:\n",
    "        video_writer.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "351639e6-322c-436a-aeb0-b246d805ffd7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "images_folder = './dataset/images'\n",
    "labels_folder = './dataset/labels'\n",
    "output_video = 'output_video.mp4'\n",
    "\n",
    "visualize_dataset_bbox(images_folder, labels_folder, output_video)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2599d3a1-c918-4bff-8678-90e856091103",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mmyolo",
   "language": "python",
   "name": "mmyolo"
  },
  "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.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
