Skip to content

Commit 424044b

Browse files
authored
Update with more comments and description
1 parent 49a91c8 commit 424044b

File tree

1 file changed

+74
-19
lines changed

1 file changed

+74
-19
lines changed

tracking.ipynb

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": 3,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -13,22 +13,20 @@
1313
]
1414
},
1515
{
16-
"cell_type": "code",
17-
"execution_count": 2,
16+
"cell_type": "markdown",
1817
"metadata": {},
19-
"outputs": [],
2018
"source": [
21-
"maxLost = 60 # maximum number of lost"
19+
"##### Object Tracking Class"
2220
]
2321
},
2422
{
2523
"cell_type": "code",
26-
"execution_count": 3,
24+
"execution_count": 4,
2725
"metadata": {},
2826
"outputs": [],
2927
"source": [
3028
"class Tracker:\n",
31-
" def __init__(self, maxLost = maxLost):\n",
29+
" def __init__(self, maxLost = 30): # maxLost: maximum object lost counted when the object is being tracked\n",
3230
" self.nextObjectID = 0 # ID of next object\n",
3331
" self.objects = OrderedDict() # stores ID:Locations\n",
3432
" self.lost = OrderedDict() # stores ID:Lost_count\n",
@@ -41,8 +39,7 @@
4139
" \n",
4240
" self.nextObjectID += 1\n",
4341
" \n",
44-
" def removeObject(self, objectID):\n",
45-
" # remove tracker data after object is lost\n",
42+
" def removeObject(self, objectID): # remove tracker data after object is lost\n",
4643
" del self.objects[objectID]\n",
4744
" del self.lost[objectID]\n",
4845
" \n",
@@ -109,28 +106,86 @@
109106
" return self.objects\n"
110107
]
111108
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"##### Loading Object Detector Model\n",
114+
"\n",
115+
"Here, the Face Detection Caffe Model is used.\n",
116+
"\n",
117+
"The files are taken from the following link:\n",
118+
"https://github.com/opencv/opencv_3rdparty/tree/dnn_samples_face_detector_20170830\n"
119+
]
120+
},
112121
{
113122
"cell_type": "code",
114-
"execution_count": 4,
123+
"execution_count": 5,
115124
"metadata": {},
116125
"outputs": [],
117126
"source": [
118-
"tracker = Tracker()\n",
119-
"caffemodel = {\"prototxt\":\"./deploy.prototxt\", \"model\":\"./res10_300x300_ssd_iter_140000.caffemodel\", \"acc_threshold\":0.50}\n",
127+
"caffemodel = {\"prototxt\":\"./deploy.prototxt\",\n",
128+
" \"model\":\"./res10_300x300_ssd_iter_140000.caffemodel\",\n",
129+
" \"acc_threshold\":0.50 # neglected detections with probability less than acc_threshold value\n",
130+
" }\n",
131+
"\n",
120132
"net = cv.dnn.readNetFromCaffe(caffemodel[\"prototxt\"], caffemodel[\"model\"])"
121133
]
122134
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"##### Instantiate the Tracker Class"
140+
]
141+
},
123142
{
124143
"cell_type": "code",
125-
"execution_count": null,
144+
"execution_count": 6,
145+
"metadata": {},
146+
"outputs": [],
147+
"source": [
148+
"maxLost = 60 # maximum number of object losts counted when the object is being tracked\n",
149+
"tracker = Tracker(maxLost = maxLost)"
150+
]
151+
},
152+
{
153+
"cell_type": "markdown",
154+
"metadata": {},
155+
"source": [
156+
"##### Initiate opencv video capture object\n",
157+
"\n",
158+
"The `video_src` can take two values:\n",
159+
"1. If `video_src=0`: OpenCV accesses the camera connected through USB\n",
160+
"2. If `video_src='video_file_path'`: OpenCV will access the video file at the given path (can be MP4, AVI, etc format)"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 7,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
"video_src = 0\n",
170+
"cap = cv.VideoCapture(video_src) "
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"metadata": {},
176+
"source": [
177+
"##### Start object detection and tracking"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": 8,
126183
"metadata": {
127184
"scrolled": true
128185
},
129186
"outputs": [],
130187
"source": [
131-
"cap = cv.VideoCapture(0)\n",
132-
"\n",
133-
"(H, W) = (None, None)\n",
188+
"(H, W) = (None, None) # input image height and width for the network\n",
134189
"\n",
135190
"while(True):\n",
136191
" \n",
@@ -147,9 +202,9 @@
147202
" blob = cv.dnn.blobFromImage(image, 1.0, (W, H), (104.0, 177.0, 123.0))\n",
148203
" \n",
149204
" net.setInput(blob)\n",
150-
" detections = net.forward()\n",
205+
" detections = net.forward() # detect objects using object detection model\n",
151206
" \n",
152-
" detections_bbox = [] # bounding box for detections\n",
207+
" detections_bbox = [] # bounding box for detections\n",
153208
" \n",
154209
" for i in range(0, detections.shape[2]):\n",
155210
" if detections[0, 0, i, 2] > caffemodel[\"acc_threshold\"]:\n",
@@ -160,7 +215,7 @@
160215
" (startX, startY, endX, endY) = box.astype(\"int\")\n",
161216
" cv.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 2) \n",
162217
" \n",
163-
" objects = tracker.update(detections_bbox)\n",
218+
" objects = tracker.update(detections_bbox) # update tracker based on the newly detected objects\n",
164219
" \n",
165220
" for (objectID, centroid) in objects.items():\n",
166221
" text = \"ID {}\".format(objectID)\n",

0 commit comments

Comments
 (0)