Skip to content

Commit

Permalink
Update colab notebooks
Browse files Browse the repository at this point in the history
Co-authored-by: UltralyticsAssistant <[email protected]>
  • Loading branch information
RizwanMunawar and UltralyticsAssistant authored Jan 6, 2025
1 parent 0625633 commit 0aabbda
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 53 deletions.
95 changes: 80 additions & 15 deletions notebooks/how-to-count-the-objects-using-ultralytics-yolo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@
" [中文](https://docs.ultralytics.com/zh/) | [한국어](https://docs.ultralytics.com/ko/) | [日本語](https://docs.ultralytics.com/ja/) | [Русский](https://docs.ultralytics.com/ru/) | [Deutsch](https://docs.ultralytics.com/de/) | [Français](https://docs.ultralytics.com/fr/) | [Español](https://docs.ultralytics.com/es/) | [Português](https://docs.ultralytics.com/pt/) | [Türkçe](https://docs.ultralytics.com/tr/) | [Tiếng Việt](https://docs.ultralytics.com/vi/) | [العربية](https://docs.ultralytics.com/ar/)\n",
"\n",
" <a href=\"https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml\"><img src=\"https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml/badge.svg\" alt=\"Ultralytics CI\"></a>\n",
" <a href=\"https://console.paperspace.com/github/ultralytics/ultralytics\"><img src=\"https://assets.paperspace.io/img/gradient-badge.svg\" alt=\"Run on Gradient\"/></a>\n",
" <a href=\"https://colab.research.google.com/github/ultralytics/notebooks/blob/main/notebooks/how-to-count-the-objects-using-ultralytics-yolo.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
" <a href=\"https://www.kaggle.com/models/ultralytics/yolo11\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" alt=\"Open In Kaggle\"></a>\n",
"\n",
" \n",
" <a href=\"https://ultralytics.com/discord\"><img alt=\"Discord\" src=\"https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue\"></a>\n",
" <a href=\"https://community.ultralytics.com\"><img alt=\"Ultralytics Forums\" src=\"https://img.shields.io/discourse/users?server=https%3A%2F%2Fcommunity.ultralytics.com&logo=discourse&label=Forums&color=blue\"></a>\n",
" <a href=\"https://reddit.com/r/ultralytics\"><img alt=\"Ultralytics Reddit\" src=\"https://img.shields.io/reddit/subreddit-subscribers/ultralytics?style=flat&logo=reddit&logoColor=white&label=Reddit&color=blue\"></a>\n",
Expand Down Expand Up @@ -79,7 +77,11 @@
},
"source": [
"%pip install ultralytics\n",
"\n",
"import ultralytics\n",
"import cv2\n",
"from ultralytics import solutions\n",
"\n",
"ultralytics.checks()"
],
"execution_count": null,
Expand All @@ -88,7 +90,10 @@
{
"cell_type": "markdown",
"source": [
"### Count the objects"
"### Read the Video File\n",
"\n",
"- You can either read the video file directly or stream the content from an RTSP (Real-Time Streaming Protocol) source, allowing for flexible video input depending on your needs.\n",
"- We will also set up the video writer to handle the output video writing."
],
"metadata": {
"id": "h8go3HNgN0WU"
Expand All @@ -97,22 +102,62 @@
{
"cell_type": "code",
"source": [
"import cv2\n",
"\n",
"from ultralytics import solutions\n",
"\n",
"cap = cv2.VideoCapture(\"path/to/video/file.mp4\")\n",
"assert cap.isOpened(), \"Error reading video file\"\n",
"w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))\n",
"w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH,\n",
" cv2.CAP_PROP_FRAME_HEIGHT,\n",
" cv2.CAP_PROP_FPS))\n",
"\n",
"# Video writer\n",
"video_writer = cv2.VideoWriter(\"object_counting_output.avi\",\n",
" cv2.VideoWriter_fourcc(*\"mp4v\"),\n",
" fps, (w, h))"
],
"metadata": {
"id": "QUgMYUvlNLvy"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Define Region Coordinates\n",
"\n",
"Here, we set the coordinates for specific regions to ensure accurate object tracking and analysis within the video or stream. This helps monitor and count objects effectively in different areas."
],
"metadata": {
"id": "3wJlBXORXNsj"
}
},
{
"cell_type": "code",
"source": [
"# Define region points\n",
"# region_points = [(20, 400), (1080, 400)] # For line counting\n",
"region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360)] # For rectangle region counting\n",
"# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360), (20, 400)] # For polygon region counting\n",
"\n",
"# Video writer\n",
"video_writer = cv2.VideoWriter(\"object_counting_output.avi\", cv2.VideoWriter_fourcc(*\"mp4v\"), fps, (w, h))\n",
"# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360), (20, 400)] # For polygon region counting"
],
"metadata": {
"id": "bVCrrForXRgS"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Initialize the ObjectCounter Class\n",
"\n",
"- Now, let's initialize the `ObjectCounter` class to track and count objects in each frame of the video."
],
"metadata": {
"id": "rt3soEHzXe8c"
}
},
{
"cell_type": "code",
"source": [
"# Init ObjectCounter\n",
"counter = solutions.ObjectCounter(\n",
" show=True, # Display the output\n",
Expand All @@ -122,8 +167,28 @@
" # show_in=True, # Display in counts\n",
" # show_out=True, # Display out counts\n",
" # line_width=2, # Adjust the line width for bounding boxes and text display\n",
")\n",
")"
],
"metadata": {
"id": "Va24DpUZXTh3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### Process Video Frames\n",
"\n",
"In this step, we will process each frame of the video to detect and analyze objects. This allows for real-time tracking and counting, based on the visual data in the frames."
],
"metadata": {
"id": "1ewYRFFqXvtj"
}
},
{
"cell_type": "code",
"source": [
"# Process video\n",
"while cap.isOpened():\n",
" success, im0 = cap.read()\n",
Expand All @@ -138,7 +203,7 @@
"cv2.destroyAllWindows()"
],
"metadata": {
"id": "QUgMYUvlNLvy"
"id": "PVf1pyRtXijz"
},
"execution_count": null,
"outputs": []
Expand Down
98 changes: 79 additions & 19 deletions notebooks/how-to-generate-heatmaps-using-ultralytics-yolo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
" [中文](https://docs.ultralytics.com/zh/) | [한국어](https://docs.ultralytics.com/ko/) | [日本語](https://docs.ultralytics.com/ja/) | [Русский](https://docs.ultralytics.com/ru/) | [Deutsch](https://docs.ultralytics.com/de/) | [Français](https://docs.ultralytics.com/fr/) | [Español](https://docs.ultralytics.com/es/) | [Português](https://docs.ultralytics.com/pt/) | [Türkçe](https://docs.ultralytics.com/tr/) | [Tiếng Việt](https://docs.ultralytics.com/vi/) | [العربية](https://docs.ultralytics.com/ar/)\n",
"\n",
" <a href=\"https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml\"><img src=\"https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml/badge.svg\" alt=\"Ultralytics CI\"></a>\n",
" <a href=\"https://console.paperspace.com/github/ultralytics/ultralytics\"><img src=\"https://assets.paperspace.io/img/gradient-badge.svg\" alt=\"Run on Gradient\"/></a>\n",
" <a href=\"https://colab.research.google.com/github/ultralytics/notebooks/blob/main/notebooks/how-to-generate-heatmaps-using-ultralytics-yolo.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
" <a href=\"https://www.kaggle.com/models/ultralytics/yolo11\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" alt=\"Open In Kaggle\"></a>\n",
"\n",
" \n",
" <a href=\"https://ultralytics.com/discord\"><img alt=\"Discord\" src=\"https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue\"></a>\n",
" <a href=\"https://community.ultralytics.com\"><img alt=\"Ultralytics Forums\" src=\"https://img.shields.io/discourse/users?server=https%3A%2F%2Fcommunity.ultralytics.com&logo=discourse&label=Forums&color=blue\"></a>\n",
" <a href=\"https://reddit.com/r/ultralytics\"><img alt=\"Ultralytics Reddit\" src=\"https://img.shields.io/reddit/subreddit-subscribers/ultralytics?style=flat&logo=reddit&logoColor=white&label=Reddit&color=blue\"></a>\n",
Expand Down Expand Up @@ -81,43 +79,85 @@
],
"source": [
"%pip install ultralytics\n",
"\n",
"import cv2\n",
"import ultralytics\n",
"from ultralytics import solutions\n",
"\n",
"ultralytics.checks()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m7VkxQ2aeg7k"
"id": "qpKaC03TZDlr"
},
"source": [
"### Generate the heatmap"
"### Read the Video File\n",
"\n",
"You can either read the video file directly or stream the content from an RTSP (Real-Time Streaming Protocol) source, allowing for flexible video input depending on your needs. We will also set up the video writer to handle the output video writing."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Cx-u59HQdu2o"
"id": "2daZuXgaZDFH"
},
"outputs": [],
"source": [
"import cv2\n",
"from ultralytics import solutions\n",
"\n",
"cap = cv2.VideoCapture(\"Path/to/video/file.mp4\")\n",
"assert cap.isOpened(), \"Error reading video file\"\n",
"w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))\n",
"\n",
"# Video writer\n",
"video_writer = cv2.VideoWriter(\"heatmap_output.avi\", cv2.VideoWriter_fourcc(*\"mp4v\"), fps, (w, h))\n",
"video_writer = cv2.VideoWriter(\"heatmap_output.avi\", cv2.VideoWriter_fourcc(*\"mp4v\"), fps, (w, h))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1IlONOqdZKjz"
},
"source": [
"### Define Region Coordinates (Optional)\n",
"\n",
"# In case you want to apply object counting + heatmaps, you can pass region points.\n",
"# region_points = [(20, 400), (1080, 400)] # Define line points\n",
"# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360)] # Define region points\n",
"# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360), (20, 400)] # Define polygon points\n",
"`Heatmap` solution doesn't require region coordinates by default, but If you want to count the objects in parallel during generation of heatmaps, you can pass the region coordinates for line counting."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "t1sTggW6ZhTD"
},
"outputs": [],
"source": [
"# Define region points\n",
"# region_points = [(20, 400), (1080, 400)] # For line counting\n",
"# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360)] # For rectangle region counting\n",
"# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360), (20, 400)] # For polygon region counting"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JQSj3p4XZlj7"
},
"source": [
"### Initialize the Heatmap Class\n",
"\n",
"Next, we will initialize the Heatmap class to track objects in each video frame and highlight the areas with the highest concentration of activity."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SpcXqN3DZ5rc"
},
"outputs": [],
"source": [
"# Init heatmap\n",
"heatmap = solutions.Heatmap(\n",
" show=True, # Display the output\n",
Expand All @@ -128,19 +168,39 @@
" # show_in=True, # Display in counts\n",
" # show_out=True, # Display out counts\n",
" # line_width=2, # Adjust the line width for bounding boxes and text display\n",
")\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m7VkxQ2aeg7k"
},
"source": [
"### Process Video Frames\n",
"\n",
"In this step, we will analyze each video frame for object detection and tracking. We also generate heatmaps to highlight areas with the highest object concentration."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Cx-u59HQdu2o"
},
"outputs": [],
"source": [
"# Process video\n",
"while cap.isOpened():\n",
" success, im0 = cap.read()\n",
" if not success:\n",
" print(\"Video frame is empty or video processing has been successfully completed.\")\n",
" break\n",
" im0 = heatmap.generate_heatmap(im0)\n",
" video_writer.write(im0)\n",
" im0 = heatmap.generate_heatmap(im0) # generate heatmap the objects\n",
" video_writer.write(im0) # write the video frame\n",
"\n",
"cap.release()\n",
"video_writer.release()\n",
"video_writer.release() # release the video_writer\n",
"cv2.destroyAllWindows()"
]
},
Expand All @@ -150,7 +210,7 @@
"id": "lRALUInRkadi"
},
"source": [
"![Ultralytics YOLO11 Transportation Heatmap](https://github.com/ultralytics/docs/releases/download/0/ultralytics-yolov8-transportation-heatmap.avif)"
"![Ultralytics YOLO11 Retail Heatmap](https://github.com/ultralytics/docs/releases/download/0/ultralytics-yolov8-retail-heatmap.avif)"
]
}
],
Expand Down
Loading

0 comments on commit 0aabbda

Please sign in to comment.