{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3319e413",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import datetime\n",
    "import imutils\n",
    "import numpy as np\n",
    "from centroidtracker import CentroidTracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cac2512d",
   "metadata": {},
   "outputs": [],
   "source": [
    "protopath = \"MobileNetSSD_deploy.prototxt\"\n",
    "modelpath = \"MobileNetSSD_deploy.caffemodel\"\n",
    "detector = cv2.dnn.readNetFromCaffe(prototxt=protopath, caffeModel=modelpath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5cfcec9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "CLASSES = [\"background\", \"aeroplane\", \"bicycle\", \"bird\", \"boat\",\n",
    "           \"bottle\", \"bus\", \"car\", \"cat\", \"chair\", \"cow\", \"diningtable\",\n",
    "           \"dog\", \"horse\", \"motorbike\", \"person\", \"pottedplant\", \"sheep\",\n",
    "           \"sofa\", \"train\", \"tvmonitor\"]\n",
    "tracker = CentroidTracker(maxDisappeared=30, maxDistance=90)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3be15240",
   "metadata": {},
   "outputs": [],
   "source": [
    "def main():\n",
    "    cap = cv2.VideoCapture('data/sample3.mp4')\n",
    "\n",
    "    fps_start_time = datetime.datetime.now()\n",
    "    fps = 0\n",
    "    total_frames = 0\n",
    "\n",
    "    while True:\n",
    "        ret, frame = cap.read()\n",
    "        key = cv2.waitKey(1)\n",
    "        if key == ord('q') or not ret:\n",
    "            break\n",
    "        frame = imutils.resize(frame, width=900)\n",
    "        total_frames = total_frames + 1\n",
    "\n",
    "        (H, W) = frame.shape[:2]\n",
    "\n",
    "        blob = cv2.dnn.blobFromImage(frame, 1/255, (W, H), (0,0,0))\n",
    "        \n",
    "        detector.setInput(blob)\n",
    "        person_detections = detector.forward()\n",
    "        rects = []\n",
    "        for i in np.arange(0, person_detections.shape[2]):\n",
    "            confidence = person_detections[0, 0, i, 2]\n",
    "            if confidence > 0.2:\n",
    "                idx = int(person_detections[0, 0, i, 1])\n",
    "\n",
    "                if CLASSES[idx] != \"person\":\n",
    "                    continue\n",
    "\n",
    "                person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])\n",
    "                (startX, startY, endX, endY) = person_box.astype(\"int\")\n",
    "                rects.append(person_box)\n",
    "\n",
    "\n",
    "        objects = tracker.update(rects)\n",
    "        for (objectId, bbox) in objects.items():\n",
    "            x1, y1, x2, y2 = bbox\n",
    "            x1 = int(x1)\n",
    "            y1 = int(y1)\n",
    "            x2 = int(x2)\n",
    "            y2 = int(y2)\n",
    "\n",
    "            cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)\n",
    "            text = \"ID: {}\".format(objectId)\n",
    "            cv2.putText(frame, text, (x1, y1-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1)\n",
    "\n",
    "        fps_end_time = datetime.datetime.now()\n",
    "        time_diff = fps_end_time - fps_start_time\n",
    "        if time_diff.seconds == 0:\n",
    "            fps = 0.0\n",
    "        else:\n",
    "            fps = (total_frames / time_diff.seconds)\n",
    "\n",
    "        fps_text = \"FPS: {:.2f}\".format(fps)\n",
    "\n",
    "        cv2.putText(frame, fps_text, (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1)\n",
    "        \n",
    "        cv2.imshow(\"Application\", frame)\n",
    "        \n",
    "        \n",
    "\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da6a796d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
