Skip to content

Commit

Permalink
added more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav274 committed Jul 26, 2021
1 parent ac0bba1 commit 7595f1b
Showing 1 changed file with 118 additions and 85 deletions.
203 changes: 118 additions & 85 deletions tutorials/object_detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 1,
"id": "62e83a0b",
"metadata": {},
"outputs": [],
Expand All @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 2,
"id": "b78a43e3",
"metadata": {},
"outputs": [],
Expand All @@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 3,
"id": "1bc99e88",
"metadata": {},
"outputs": [],
Expand All @@ -61,29 +61,47 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 4,
"id": "130b8561",
"metadata": {},
"outputs": [],
"source": [
"cursor.execute('UPLOAD INFILE \"../data/ua_detrac/ua_detrac.mp4\" PATH \"video.mp4\";')\n",
"response = cursor.fetch_one()"
]
},
{
"cell_type": "markdown",
"id": "6450f505",
"metadata": {},
"source": [
"### Visualize Video"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "bef7026f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response Object:\n",
"@status: -1\n",
"@batch: Batch Object:\n",
"@dataframe: error\n",
"0 [Errno 13] Permission denied: '/tmp/test_video...\n",
"@batch_size: 1\n",
"@identifier_column: id\n",
"@metrics: None\n"
]
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "19baca4d84dc4c58ae918e0ba18414bb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Video(value=b'\\x00\\x00\\x00 ftypisom\\x00\\x00\\x02\\x00isomiso2avc1mp41\\x00\\x00\\x00\\x08free\\x00\\x19K[mdat\\x00\\x00\\"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"cursor.execute('UPLOAD INFILE \"../data/ua_detrac/ua_detrac.mp4\" PATH \"test_video.mp4\";')\n",
"response = cursor.fetch_one()\n",
"print(response)"
"from ipywidgets import Video\n",
"Video.from_file(\"../data/ua_detrac/ua_detrac.mp4\", embed=True)"
]
},
{
Expand All @@ -96,29 +114,13 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 6,
"id": "b18cfe2a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response Object:\n",
"@status: -1\n",
"@batch: Batch Object:\n",
"@dataframe: error\n",
"0 list index out of range\n",
"@batch_size: 1\n",
"@identifier_column: id\n",
"@metrics: None\n"
]
}
],
"outputs": [],
"source": [
"cursor.execute('LOAD DATA INFILE \"test_video.mp4\" INTO MyVideo;')\n",
"response = cursor.fetch_one()\n",
"print(response)"
"response = cursor.fetch_one()"
]
},
{
Expand All @@ -131,33 +133,17 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 7,
"id": "e83e5a44",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response Object:\n",
"@status: -1\n",
"@batch: Batch Object:\n",
"@dataframe: error\n",
"0 Exception occurred while committing to database.\n",
"@batch_size: 1\n",
"@identifier_column: id\n",
"@metrics: None\n"
]
}
],
"outputs": [],
"source": [
"cursor.execute(\"\"\"CREATE UDF FastRCNNObjectDetector \n",
"cursor.execute(\"\"\"CREATE UDF IF NOT EXISTS FastRCNNObjectDetector \n",
" INPUT (frame NDARRAY UINT8(3, 256, 256))\n",
" OUTPUT (labels NDARRAY STR(20), bboxes NDARRAY INT32(20, 4))\n",
" TYPE Classification\n",
" IMPL 'src/udfs/fastrcnn_object_detector.py'\"\"\")\n",
"response = cursor.fetch_one()\n",
"print(response)"
"response = cursor.fetch_one()"
]
},
{
Expand All @@ -170,46 +156,93 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 8,
"id": "91bdcaca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Response Object:\n",
"@status: -1\n",
"@batch: Batch Object:\n",
"@dataframe: error\n",
"0 Exception occurred while committing to database.\n",
"@batch_size: 1\n",
"@identifier_column: id\n",
"@metrics: None\n"
]
}
],
"outputs": [],
"source": [
"cursor.execute(\"\"\"SELECT FastRCNNObjectDetector(data) FROM MyVideo WHERE id < 10\"\"\")\n",
"response = cursor.fetch_one()\n",
"print(response)"
"cursor.execute(\"\"\"SELECT id, Unnest(FastRCNNObjectDetector(data)) FROM MyVideo\"\"\")\n",
"response = cursor.fetch_one()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7d6391b",
"cell_type": "markdown",
"id": "d81ed233",
"metadata": {},
"outputs": [],
"source": []
"source": [
"### Visualize output of Object detector on the video"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "ecc977d8",
"metadata": {},
"outputs": [],
"source": []
"source": [
"import cv2\n",
"def annotate_video(input_path, output_path):\n",
" color=(0,255,0)\n",
" thickness=3\n",
"\n",
" vcap = cv2.VideoCapture(input_path)\n",
" width = int(vcap.get(3))\n",
" height = int(vcap.get(4))\n",
" fps = vcap.get(5)\n",
" fourcc = cv2.VideoWriter_fourcc(*'H264') #codec\n",
" video=cv2.VideoWriter(output_path, fourcc, fps, (width,height))\n",
"\n",
" frame_id = 0\n",
" # Capture frame-by-frame\n",
" ret, frame = vcap.read() # ret = 1 if the video is captured; frame is the image\n",
"\n",
" while ret:\n",
" df = response.batch.frames\n",
" df = df[['boxes', 'labels']][df.id == frame_id]\n",
" if df.size:\n",
" for bbox, label in df.values:\n",
" x1, y1 = bbox[0]\n",
" x2, y2 = bbox[1]\n",
" x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)\n",
" img=cv2.rectangle(frame, (x1, y1), (x2, y2), color, thickness) # object bbox\n",
" cv2.putText(img, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, thickness-1) # object label\n",
" video.write(img)\n",
"\n",
" frame_id+=1\n",
" ret, frame = vcap.read()\n",
"\n",
" video.release()\n",
" vcap.release()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "7a2dee29",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "74dce176cfb14b6fb531228e2bf960e1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Video(value=b'\\x00\\x00\\x00\\x1cftypisom\\x00\\x00\\x02\\x00isomiso2mp41\\x00\\x00\\x00\\x08free\\x00\\xdai\\\\mdat\\x82I\\x83…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from ipywidgets import Video\n",
"input_path = '../data/ua_detrac/ua_detrac.mp4'\n",
"output_path = 'video.mp4'\n",
"annotate_video(input_path, output_path)\n",
"Video.from_file(output_path)"
]
}
],
"metadata": {
Expand Down

0 comments on commit 7595f1b

Please sign in to comment.