Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix notebook #138

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions pynq_composable/notebooks/04_OpenCV_vs_Composable_Overlay.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,13 @@
"outputs": [],
"source": [
"kernel = np.ones((3,3), np.uint8)\n",
"def run_sw_pipeline():\n",
" ret, frame = webcam.read()\n",
"def run_sw_pipeline(frame):\n",
" frame_hsv = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)\n",
" #frame_threshold0 = cv2.inRange(frame_hsv, (22, 38, 160), (38, 75, 179))\n",
" #frame_threshold1 = cv2.inRange(frame_hsv, (150, 150, 150), (255, 255, 255))\n",
" frame_threshold0 = cv2.inRange(frame_hsv, (22, 38, 160), (38, 75, 179))\n",
" frame_threshold1 = cv2.inRange(frame_hsv, (150, 150, 150), (255, 255, 255))\n",
" frame_threshold2 = cv2.inRange(frame_hsv, (60, 60, 60), (255, 255, 255))\n",
" #frame_threshold = frame_threshold0 | frame_threshold1 | frame_threshold2\n",
" erode_0 = cv2.erode(frame_threshold2, kernel, iterations=1)\n",
" frame_threshold = frame_threshold0 | frame_threshold1 | frame_threshold2\n",
" erode_0 = cv2.erode(frame_threshold, kernel, iterations=1)\n",
" dilate_0 = cv2.erode(erode_0, kernel, iterations=1)\n",
" dilate_1 = cv2.erode(dilate_0, kernel, iterations=1)\n",
" erode_1 = cv2.erode(dilate_1, kernel, iterations=1)\n",
Expand All @@ -165,7 +164,8 @@
"metadata": {},
"outputs": [],
"source": [
"res = run_sw_pipeline()\n",
"ret, frame = webcam.read() # use same frame not to be limitted by the camera FPS\n",
"res = run_sw_pipeline(frame)\n",
"show_image(res)"
]
},
Expand All @@ -185,12 +185,13 @@
"outputs": [],
"source": [
"frames = 120\n",
"ret, frame = webcam.read() # use same frame not to be limitted by the camera FPS\n",
"start_time = time.time()\n",
"for _ in range(frames):\n",
" res = run_sw_pipeline()\n",
" res = run_sw_pipeline(frame)\n",
"end_time = time.time()\n",
"\n",
"print(\"Took {:.3f} seconds to process {} frames. Software achieved {:.2f} FPS\".format(ex_time := end_time-start_time, frames, frames/ex_time))"
"print(\"Took {:.3f} seconds to process {} frames. Software achieved {:.2f} FPS\".format(ex_time_sw := end_time-start_time, frames, frames/ex_time_sw))"
]
},
{
Expand Down Expand Up @@ -270,9 +271,7 @@
"metadata": {},
"outputs": [],
"source": [
"cpipe.load('pr_0/dilate_accel')\n",
"cpipe.load('pr_1/dilate_accel')\n",
"cpipe.load('pr_2/bitwise_and_accel')\n",
"cpipe.load(['pr_0/dilate_accel', 'pr_1/dilate_accel', 'pr_2/bitwise_and_accel'])\n",
"\n",
"pipeline = [cpipe.ps_video_in, cpipe.duplicate_accel,\n",
" [[cpipe.rgb2hsv_accel, cpipe.colorthresholding_accel, cpipe.pr_0.erode_accel,\n",
Expand All @@ -299,9 +298,9 @@
"metadata": {},
"outputs": [],
"source": [
"def run_hw_pipeline():\n",
"def run_hw_pipeline(frame):\n",
" fpgaframe = writechannel.newframe()\n",
" _, fpgaframe[:] = webcam.read()\n",
" fpgaframe[:] = frame\n",
" writechannel.writeframe(fpgaframe)\n",
" res = readchannel.readframe()\n",
" return res"
Expand All @@ -323,12 +322,31 @@
"outputs": [],
"source": [
"frames = 120\n",
"ret, frame = webcam.read() # use same frame not to be limitted by the camera FPS\n",
"start_time = time.time()\n",
"for _ in range(frames):\n",
" res = run_hw_pipeline()\n",
" res = run_hw_pipeline(frame)\n",
"end_time = time.time()\n",
"\n",
"print(\"Took {:.3f} seconds to process {} frames. Hardware achieved {:.2f} FPS\".format(ex_time := end_time-start_time, frames, frames/ex_time))"
"print(\"Took {:.3f} seconds to process {} frames. Hardware achieved {:.2f} FPS\".format(ex_time_hw := end_time-start_time, frames, frames/ex_time_hw))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6342f299-dc35-4fcb-b6f5-1488ef7536b0",
"metadata": {},
"outputs": [],
"source": [
"print(f\"Hardware acceleration: {ex_time_sw/ex_time_hw:.3f}x\")"
]
},
{
"cell_type": "markdown",
"id": "052617f7-a3fc-4429-a3e7-250d639fa7a1",
"metadata": {},
"source": [
"You can clearly see the benefit of using the FPGA to accelerate the video processing. Not only, it is faster but given that it is implemented as a dataflow pipeline, we can add more video processing functions without loss of performance."
]
},
{
Expand Down Expand Up @@ -375,7 +393,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -389,7 +407,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
Loading