Format: application/octet-stream
⬇ Download File (4.3 KB)
   1  {
   2   "cells": [
   3    {
   4     "cell_type": "code",
   5     "execution_count": 1,
   6     "id": "3319e413",
   7     "metadata": {},
   8     "outputs": [],
   9     "source": [
  10      "import cv2\n",
  11      "import datetime\n",
  12      "import imutils\n",
  13      "import numpy as np\n",
  14      "from centroidtracker import CentroidTracker"
  15     ]
  16    },
  17    {
  18     "cell_type": "code",
  19     "execution_count": 2,
  20     "id": "cac2512d",
  21     "metadata": {},
  22     "outputs": [],
  23     "source": [
  24      "protopath = \"MobileNetSSD_deploy.prototxt\"\n",
  25      "modelpath = \"MobileNetSSD_deploy.caffemodel\"\n",
  26      "detector = cv2.dnn.readNetFromCaffe(prototxt=protopath, caffeModel=modelpath)"
  27     ]
  28    },
  29    {
  30     "cell_type": "code",
  31     "execution_count": 3,
  32     "id": "5cfcec9b",
  33     "metadata": {},
  34     "outputs": [],
  35     "source": [
  36      "CLASSES = [\"background\", \"aeroplane\", \"bicycle\", \"bird\", \"boat\",\n",
  37      "           \"bottle\", \"bus\", \"car\", \"cat\", \"chair\", \"cow\", \"diningtable\",\n",
  38      "           \"dog\", \"horse\", \"motorbike\", \"person\", \"pottedplant\", \"sheep\",\n",
  39      "           \"sofa\", \"train\", \"tvmonitor\"]\n",
  40      "tracker = CentroidTracker(maxDisappeared=30, maxDistance=90)"
  41     ]
  42    },
  43    {
  44     "cell_type": "code",
  45     "execution_count": 4,
  46     "id": "3be15240",
  47     "metadata": {},
  48     "outputs": [],
  49     "source": [
  50      "def main():\n",
  51      "    cap = cv2.VideoCapture('data/sample3.mp4')\n",
  52      "\n",
  53      "    fps_start_time = datetime.datetime.now()\n",
  54      "    fps = 0\n",
  55      "    total_frames = 0\n",
  56      "\n",
  57      "    while True:\n",
  58      "        ret, frame = cap.read()\n",
  59      "        key = cv2.waitKey(1)\n",
  60      "        if key == ord('q') or not ret:\n",
  61      "            break\n",
  62      "        frame = imutils.resize(frame, width=900)\n",
  63      "        total_frames = total_frames + 1\n",
  64      "\n",
  65      "        (H, W) = frame.shape[:2]\n",
  66      "\n",
  67      "        blob = cv2.dnn.blobFromImage(frame, 1/255, (W, H), (0,0,0))\n",
  68      "        \n",
  69      "        detector.setInput(blob)\n",
  70      "        person_detections = detector.forward()\n",
  71      "        rects = []\n",
  72      "        for i in np.arange(0, person_detections.shape[2]):\n",
  73      "            confidence = person_detections[0, 0, i, 2]\n",
  74      "            if confidence > 0.2:\n",
  75      "                idx = int(person_detections[0, 0, i, 1])\n",
  76      "\n",
  77      "                if CLASSES[idx] != \"person\":\n",
  78      "                    continue\n",
  79      "\n",
  80      "                person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])\n",
  81      "                (startX, startY, endX, endY) = person_box.astype(\"int\")\n",
  82      "                rects.append(person_box)\n",
  83      "\n",
  84      "\n",
  85      "        objects = tracker.update(rects)\n",
  86      "        for (objectId, bbox) in objects.items():\n",
  87      "            x1, y1, x2, y2 = bbox\n",
  88      "            x1 = int(x1)\n",
  89      "            y1 = int(y1)\n",
  90      "            x2 = int(x2)\n",
  91      "            y2 = int(y2)\n",
  92      "\n",
  93      "            cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)\n",
  94      "            text = \"ID: {}\".format(objectId)\n",
  95      "            cv2.putText(frame, text, (x1, y1-5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1)\n",
  96      "\n",
  97      "        fps_end_time = datetime.datetime.now()\n",
  98      "        time_diff = fps_end_time - fps_start_time\n",
  99      "        if time_diff.seconds == 0:\n",
 100      "            fps = 0.0\n",
 101      "        else:\n",
 102      "            fps = (total_frames / time_diff.seconds)\n",
 103      "\n",
 104      "        fps_text = \"FPS: {:.2f}\".format(fps)\n",
 105      "\n",
 106      "        cv2.putText(frame, fps_text, (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1)\n",
 107      "        \n",
 108      "        cv2.imshow(\"Application\", frame)\n",
 109      "        \n",
 110      "        \n",
 111      "\n",
 112      "    cv2.destroyAllWindows()\n",
 113      "\n",
 114      "\n",
 115      "main()"
 116     ]
 117    },
 118    {
 119     "cell_type": "code",
 120     "execution_count": null,
 121     "id": "da6a796d",
 122     "metadata": {},
 123     "outputs": [],
 124     "source": []
 125    }
 126   ],
 127   "metadata": {
 128    "kernelspec": {
 129     "display_name": "Python 3",
 130     "language": "python",
 131     "name": "python3"
 132    },
 133    "language_info": {
 134     "codemirror_mode": {
 135      "name": "ipython",
 136      "version": 3
 137     },
 138     "file_extension": ".py",
 139     "mimetype": "text/x-python",
 140     "name": "python",
 141     "nbconvert_exporter": "python",
 142     "pygments_lexer": "ipython3",
 143     "version": "3.8.8"
 144    }
 145   },
 146   "nbformat": 4,
 147   "nbformat_minor": 5
 148  }
 149