`ultralytics 8.2.18` major Ultralytics Solutions refactor (#12797)

Co-authored-by: Muhammad Rizwan Munawar <muhammadrizwanmunawar123@gmail.com>
pull/12801/head v8.2.18
Glenn Jocher 6 months ago committed by GitHub
parent fceea033ad
commit b6d425c041
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 110
      examples/heatmaps.ipynb
  2. 22
      examples/object_counting.ipynb
  3. 178
      examples/object_tracking.ipynb
  4. 2
      ultralytics/__init__.py

@ -4,7 +4,8 @@
"metadata": { "metadata": {
"colab": { "colab": {
"provenance": [], "provenance": [],
"gpuType": "T4" "gpuType": "T4",
"toc_visible": true
}, },
"kernelspec": { "kernelspec": {
"name": "python3", "name": "python3",
@ -27,14 +28,16 @@
" [中文](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/hi/) | [العربية](https://docs.ultralytics.com/ar/)\n", " [中文](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/hi/) | [العربية](https://docs.ultralytics.com/ar/)\n",
"\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://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/ultralytics/blob/main/examples/heatmaps.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n", " <a href=\"https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/heatmaps.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
" <a href=\"https://www.kaggle.com/ultralytics/yolov8\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" alt=\"Open In Kaggle\"></a>\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://ultralytics.com/discord\"><img alt=\"Discord\" src=\"https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue\"></a>\n",
"\n", "\n",
"Welcome to the Ultralytics YOLOv8 🚀 notebook! <a href=\"https://github.com/ultralytics/ultralytics\">YOLOv8</a> is the latest version of the YOLO (You Only Look Once) AI models developed by <a href=\"https://ultralytics.com\">Ultralytics</a>. This notebook serves as the starting point for exploring the <a href=\"https://docs.ultralytics.com/guides/heatmaps/\">heatmaps</a> and understand its features and capabilities.\n", "Welcome to the Ultralytics YOLOv8 🚀 notebook! <a href=\"https://github.com/ultralytics/ultralytics\">YOLOv8</a> is the latest version of the YOLO (You Only Look Once) AI models developed by <a href=\"https://ultralytics.com\">Ultralytics</a>. This notebook serves as the starting point for exploring the various resources available to help you get started with YOLOv8 and understand its features and capabilities.\n",
"\n", "\n",
"YOLOv8 models are fast, accurate, and easy to use, making them ideal for various object detection and image segmentation tasks. They can be trained on large datasets and run on diverse hardware platforms, from CPUs to GPUs.\n", "YOLOv8 models are fast, accurate, and easy to use, making them ideal for various object detection and image segmentation tasks. They can be trained on large datasets and run on diverse hardware platforms, from CPUs to GPUs.\n",
"\n", "\n",
"We hope that the resources in this notebook will help you get the most out of <a href=\"https://docs.ultralytics.com/guides/heatmaps/\">Ultralytics Heatmaps</a>. Please browse the YOLOv8 <a href=\"https://docs.ultralytics.com/\">Docs</a> for details, raise an issue on <a href=\"https://github.com/ultralytics/ultralytics\">GitHub</a> for support, and join our <a href=\"https://ultralytics.com/discord\">Discord</a> community for questions and discussions!\n", "We hope that the resources in this notebook will help you get the most out of YOLOv8. Please browse the YOLOv8 <a href=\"https://docs.ultralytics.com/guides/heatmaps\">Heatmap Docs</a> for details, raise an issue on <a href=\"https://github.com/ultralytics/ultralytics\">GitHub</a> for support, and join our <a href=\"https://ultralytics.com/discord\">Discord</a> community for questions and discussions!\n",
"\n", "\n",
"</div>" "</div>"
], ],
@ -55,21 +58,43 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"id": "9dSwz_uOReMI" "id": "9dSwz_uOReMI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "99866c77-e210-41e1-d581-8508371ce634"
}, },
"outputs": [], "outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Ultralytics YOLOv8.2.17 🚀 Python-3.10.12 torch-2.2.1+cu121 CUDA:0 (Tesla T4, 15102MiB)\n",
"Setup complete ✅ (2 CPUs, 12.7 GB RAM, 29.8/78.2 GB disk)\n"
]
}
],
"source": [ "source": [
"!pip install ultralytics" "%pip install ultralytics\n",
"import ultralytics\n",
"ultralytics.checks()"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"# Ultralytics Heatmaps\n", "# Introduction to Heatmaps\n",
"\n",
"A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ultralytics/) transforms complex data into a vibrant, color-coded matrix. This visual tool employs a spectrum of colors to represent varying data values, where warmer hues indicate higher intensities and cooler tones signify lower values. Heatmaps excel in visualizing intricate data patterns, correlations, and anomalies, offering an accessible and engaging approach to data interpretation across diverse domains.\n",
"\n",
"## Real World Applications\n",
"\n", "\n",
"Heatmap is color-coded matrix, generated by Ultralytics YOLOv8, simplifies intricate data by using vibrant colors. This visual representation employs warmer hues for higher intensities and cooler tones for lower values. Heatmaps are effective in illustrating complex data patterns, correlations, and anomalies, providing a user-friendly and engaging way to interpret data across various domains." "| Transportation | Retail |\n",
"|:-----------------------------------------------------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------------:|\n",
"| ![Ultralytics YOLOv8 Transportation Heatmap](https://github.com/RizwanMunawar/ultralytics/assets/62513924/288d7053-622b-4452-b4e4-1f41aeb764aa) | ![Ultralytics YOLOv8 Retail Heatmap](https://github.com/RizwanMunawar/ultralytics/assets/62513924/edef75ad-50a7-4c0a-be4a-a66cdfc12802) |\n",
"| Ultralytics YOLOv8 Transportation Heatmap | Ultralytics YOLOv8 Retail Heatmap |\n"
], ],
"metadata": { "metadata": {
"id": "m7VkxQ2aeg7k" "id": "m7VkxQ2aeg7k"
@ -78,39 +103,46 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"from ultralytics import YOLO\n",
"from ultralytics.solutions import heatmap\n",
"import cv2\n", "import cv2\n",
"from ultralytics import YOLO, solutions\n",
"\n", "\n",
"# Load YOLO model\n",
"model = YOLO(\"yolov8n.pt\")\n", "model = YOLO(\"yolov8n.pt\")\n",
"\n",
"# Open video file\n",
"cap = cv2.VideoCapture(\"path/to/video/file.mp4\")\n", "cap = cv2.VideoCapture(\"path/to/video/file.mp4\")\n",
"assert cap.isOpened(), \"Error reading video file\"\n", "assert cap.isOpened(), \"Error reading video file\"\n",
"\n",
"# Get video properties\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, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))\n",
"\n", "\n",
"# Video writer\n", "# Initialize video writer\n",
"video_writer = cv2.VideoWriter(\"heatmap_output.avi\",\n", "video_writer = cv2.VideoWriter(\"heatmap_output.avi\", cv2.VideoWriter_fourcc(*\"mp4v\"), fps, (w, h))\n",
" cv2.VideoWriter_fourcc(*'mp4v'),\n",
" fps,\n",
" (w, h))\n",
"\n", "\n",
"# Init heatmap\n", "# Initialize heatmap object\n",
"heatmap_obj = heatmap.Heatmap()\n", "heatmap_obj = solutions.Heatmap(\n",
"heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA,\n", " colormap=cv2.COLORMAP_PARULA,\n",
" imw=w,\n", " view_img=True,\n",
" imh=h,\n", " shape=\"circle\",\n",
" view_img=True,\n", " classes_names=model.names,\n",
" shape=\"circle\")\n", ")\n",
"\n", "\n",
"while cap.isOpened():\n", "while cap.isOpened():\n",
" success, im0 = cap.read()\n", " success, im0 = cap.read()\n",
" if not success:\n", " if not success:\n",
" print(\"Video frame is empty or video processing has been successfully completed.\")\n", " print(\"Video frame is empty or video processing has been successfully completed.\")\n",
" break\n", " break\n",
"\n",
" # Perform tracking on the current frame\n",
" tracks = model.track(im0, persist=True, show=False)\n", " tracks = model.track(im0, persist=True, show=False)\n",
"\n", "\n",
" # Generate heatmap on the frame\n",
" im0 = heatmap_obj.generate_heatmap(im0, tracks)\n", " im0 = heatmap_obj.generate_heatmap(im0, tracks)\n",
"\n",
" # Write the frame to the output video\n",
" video_writer.write(im0)\n", " video_writer.write(im0)\n",
"\n", "\n",
"# Release resources\n",
"cap.release()\n", "cap.release()\n",
"video_writer.release()\n", "video_writer.release()\n",
"cv2.destroyAllWindows()" "cv2.destroyAllWindows()"
@ -124,20 +156,30 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"#Community Support\n", "# Additional Resources\n",
"\n",
"## Community Support\n",
"\n",
"For more information on using heatmaps with Ultralytics, you can explore the comprehensive [Ultralytics Heatmaps Docs](https://docs.ultralytics.com/guides/heatmaps/). This guide covers everything from basic concepts to advanced techniques, ensuring you get the most out of your heatmap visualizations.\n",
"\n",
"## Ultralytics ⚡ Resources\n",
"\n",
"At Ultralytics, we are committed to providing cutting-edge AI solutions. Here are some key resources to learn more about our company and get involved with our community:\n",
"\n",
"- [Ultralytics HUB](https://ultralytics.com/hub): Simplify your AI projects with Ultralytics HUB, our no-code tool for effortless YOLO training and deployment.\n",
"- [Ultralytics Licensing](https://ultralytics.com/license): Review our licensing terms to understand how you can use our software in your projects.\n",
"- [About Us](https://ultralytics.com/about): Discover our mission, vision, and the story behind Ultralytics.\n",
"- [Join Our Team](https://ultralytics.com/work): Explore career opportunities and join our team of talented professionals.\n",
"\n",
"## YOLOv8 🚀 Resources\n",
"\n", "\n",
"For more information, you can explore <a href=\"https://docs.ultralytics.com/guides/heatmaps/#heatmap-colormaps\">Ultralytics Heatmaps Docs</a>\n", "YOLOv8 is the latest evolution in the YOLO series, offering state-of-the-art performance in object detection and image segmentation. Here are some essential resources to help you get started with YOLOv8:\n",
"\n", "\n",
"Ultralytics ⚡ resources\n", "- [GitHub](https://github.com/ultralytics/ultralytics): Access the YOLOv8 repository on GitHub, where you can find the source code, contribute to the project, and report issues.\n",
"- About Us – https://ultralytics.com/about\n", "- [Docs](https://docs.ultralytics.com/): Explore the official documentation for YOLOv8, including installation guides, tutorials, and detailed API references.\n",
"- Join Our Team – https://ultralytics.com/work\n", "- [Discord](https://ultralytics.com/discord): Join our Discord community to connect with other users, share your projects, and get help from the Ultralytics team.\n",
"- Contact Us – https://ultralytics.com/contact\n",
"- Discord – https://ultralytics.com/discord\n",
"- Ultralytics License – https://ultralytics.com/license\n",
"\n", "\n",
"YOLOv8 🚀 resources\n", "These resources are designed to help you leverage the full potential of Ultralytics' offerings and YOLOv8. Whether you're a beginner or an experienced developer, you'll find the information and support you need to succeed."
"- GitHub – https://github.com/ultralytics/ultralytics\n",
"- Docs – https://docs.ultralytics.com/"
], ],
"metadata": { "metadata": {
"id": "QrlKg-y3fEyD" "id": "QrlKg-y3fEyD"

@ -78,9 +78,8 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"from ultralytics import YOLO\n",
"from ultralytics.solutions import object_counter\n",
"import cv2\n", "import cv2\n",
"from ultralytics import YOLO, solutions\n",
"\n", "\n",
"model = YOLO(\"yolov8n.pt\")\n", "model = YOLO(\"yolov8n.pt\")\n",
"cap = cv2.VideoCapture(\"path/to/video/file.mp4\")\n", "cap = cv2.VideoCapture(\"path/to/video/file.mp4\")\n",
@ -91,17 +90,16 @@
"region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]\n", "region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]\n",
"\n", "\n",
"# Video writer\n", "# Video writer\n",
"video_writer = cv2.VideoWriter(\"object_counting_output.avi\",\n", "video_writer = cv2.VideoWriter(\"object_counting_output.avi\", cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))\n",
" cv2.VideoWriter_fourcc(*'mp4v'),\n",
" fps,\n",
" (w, h))\n",
"\n", "\n",
"# Init Object Counter\n", "# Init Object Counter\n",
"counter = object_counter.ObjectCounter()\n", "counter = solutions.ObjectCounter(\n",
"counter.set_args(view_img=True,\n", " view_img=True,\n",
" reg_pts=region_points,\n", " reg_pts=region_points,\n",
" classes_names=model.names,\n", " classes_names=model.names,\n",
" draw_tracks=True)\n", " draw_tracks=True,\n",
" line_thickness=2,\n",
")\n",
"\n", "\n",
"while cap.isOpened():\n", "while cap.isOpened():\n",
" success, im0 = cap.read()\n", " success, im0 = cap.read()\n",
@ -146,4 +144,4 @@
} }
} }
] ]
} }

@ -27,14 +27,16 @@
" [中文](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/hi/) | [العربية](https://docs.ultralytics.com/ar/)\n", " [中文](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/hi/) | [العربية](https://docs.ultralytics.com/ar/)\n",
"\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://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/ultralytics/blob/main/examples/object_tracking.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n", " <a href=\"https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/object_tracking.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
" <a href=\"https://www.kaggle.com/ultralytics/yolov8\"><img src=\"https://kaggle.com/static/images/open-in-kaggle.svg\" alt=\"Open In Kaggle\"></a>\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://ultralytics.com/discord\"><img alt=\"Discord\" src=\"https://img.shields.io/discord/1089800235347353640?logo=discord&logoColor=white&label=Discord&color=blue\"></a>\n",
"\n", "\n",
"Welcome to the Ultralytics YOLOv8 🚀 notebook! <a href=\"https://github.com/ultralytics/ultralytics\">YOLOv8</a> is the latest version of the YOLO (You Only Look Once) AI models developed by <a href=\"https://ultralytics.com\">Ultralytics</a>. This notebook serves as the starting point for exploring the <a href=\"https://docs.ultralytics.com/modes/track/\">Object Tracking</a> and understand its features and capabilities.\n", "Welcome to the Ultralytics YOLOv8 🚀 notebook! <a href=\"https://github.com/ultralytics/ultralytics\">YOLOv8</a> is the latest version of the YOLO (You Only Look Once) AI models developed by <a href=\"https://ultralytics.com\">Ultralytics</a>. This notebook serves as the starting point for exploring the various resources available to help you get started with YOLOv8 and understand its features and capabilities.\n",
"\n", "\n",
"YOLOv8 models are fast, accurate, and easy to use, making them ideal for various object detection and image segmentation tasks. They can be trained on large datasets and run on diverse hardware platforms, from CPUs to GPUs.\n", "YOLOv8 models are fast, accurate, and easy to use, making them ideal for various object detection and image segmentation tasks. They can be trained on large datasets and run on diverse hardware platforms, from CPUs to GPUs.\n",
"\n", "\n",
"We hope that the resources in this notebook will help you get the most out of <a href=\"https://docs.ultralytics.com/modes/track/\">Ultralytics Object Tracking</a>. Please browse the YOLOv8 <a href=\"https://docs.ultralytics.com/\">Docs</a> for details, raise an issue on <a href=\"https://github.com/ultralytics/ultralytics\">GitHub</a> for support, and join our <a href=\"https://ultralytics.com/discord\">Discord</a> community for questions and discussions!\n", "We hope that the resources in this notebook will help you get the most out of YOLOv8. Please browse the YOLOv8 <a href=\"https://docs.ultralytics.com/modes/track/\"> Tracking Docs</a> for details, raise an issue on <a href=\"https://github.com/ultralytics/ultralytics\">GitHub</a> for support, and join our <a href=\"https://ultralytics.com/discord\">Discord</a> community for questions and discussions!\n",
"\n", "\n",
"</div>" "</div>"
], ],
@ -55,13 +57,28 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"id": "9dSwz_uOReMI" "id": "9dSwz_uOReMI",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "ed8c2370-8fc7-4e4e-f669-d0bae4d944e9"
}, },
"outputs": [], "outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Ultralytics YOLOv8.2.17 🚀 Python-3.10.12 torch-2.2.1+cu121 CUDA:0 (Tesla T4, 15102MiB)\n",
"Setup complete ✅ (2 CPUs, 12.7 GB RAM, 29.8/78.2 GB disk)\n"
]
}
],
"source": [ "source": [
"!pip install ultralytics" "%pip install ultralytics\n",
"import ultralytics\n",
"ultralytics.checks()"
] ]
}, },
{ {
@ -69,7 +86,20 @@
"source": [ "source": [
"# Ultralytics Object Tracking\n", "# Ultralytics Object Tracking\n",
"\n", "\n",
"Within the domain of video analytics, object tracking stands out as a crucial undertaking. It goes beyond merely identifying the location and class of objects within the frame; it also involves assigning a unique ID to each detected object as the video unfolds. The applications of this technology are vast, spanning from surveillance and security to real-time sports analytics." "[Ultralytics YOLOv8](https://github.com/ultralytics/ultralytics/) instance segmentation involves identifying and outlining individual objects in an image, providing a detailed understanding of spatial distribution. Unlike semantic segmentation, it uniquely labels and precisely delineates each object, crucial for tasks like object detection and medical imaging.\n",
"\n",
"There are two types of instance segmentation tracking available in the Ultralytics package:\n",
"\n",
"- **Instance Segmentation with Class Objects:** Each class object is assigned a unique color for clear visual separation.\n",
"\n",
"- **Instance Segmentation with Object Tracks:** Every track is represented by a distinct color, facilitating easy identification and tracking.\n",
"\n",
"## Samples\n",
"\n",
"| Instance Segmentation | Instance Segmentation + Object Tracking |\n",
"|:---------------------------------------------------------------------------------------------------------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------:|\n",
"| ![Ultralytics Instance Segmentation](https://github.com/RizwanMunawar/ultralytics/assets/62513924/d4ad3499-1f33-4871-8fbc-1be0b2643aa2) | ![Ultralytics Instance Segmentation with Object Tracking](https://github.com/RizwanMunawar/ultralytics/assets/62513924/2e5c38cc-fd5c-4145-9682-fa94ae2010a0) |\n",
"| Ultralytics Instance Segmentation 😍 | Ultralytics Instance Segmentation with Object Tracking 🔥 |"
], ],
"metadata": { "metadata": {
"id": "m7VkxQ2aeg7k" "id": "m7VkxQ2aeg7k"
@ -78,7 +108,9 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"## CLI" "## CLI\n",
"\n",
"Command-Line Interface (CLI) example."
], ],
"metadata": { "metadata": {
"id": "-ZF9DM6e6gz0" "id": "-ZF9DM6e6gz0"
@ -100,7 +132,7 @@
"source": [ "source": [
"## Python\n", "## Python\n",
"\n", "\n",
"- Draw Object tracking trails" "Python Instance Segmentation and Object tracking example."
], ],
"metadata": { "metadata": {
"id": "XRcw0vIE6oNb" "id": "XRcw0vIE6oNb"
@ -109,67 +141,61 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"from collections import defaultdict\n",
"\n",
"import cv2\n", "import cv2\n",
"import numpy as np\n",
"from ultralytics import YOLO\n", "from ultralytics import YOLO\n",
"\n",
"from ultralytics.utils.checks import check_imshow\n",
"from ultralytics.utils.plotting import Annotator, colors\n", "from ultralytics.utils.plotting import Annotator, colors\n",
"\n", "\n",
"from collections import defaultdict\n", "# Dictionary to store tracking history with default empty lists\n",
"\n",
"track_history = defaultdict(lambda: [])\n", "track_history = defaultdict(lambda: [])\n",
"model = YOLO(\"yolov8n.pt\")\n",
"names = model.model.names\n",
"\n", "\n",
"video_path = \"/path/to/video/file.mp4\"\n", "# Load the YOLO model with segmentation capabilities\n",
"cap = cv2.VideoCapture(video_path)\n", "model = YOLO(\"yolov8n-seg.pt\")\n",
"assert cap.isOpened(), \"Error reading video file\"\n", "\n",
"# Open the video file\n",
"cap = cv2.VideoCapture(\"path/to/video/file.mp4\")\n",
"\n", "\n",
"# Retrieve video properties: width, height, and frames per second\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, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))\n",
"\n", "\n",
"result = cv2.VideoWriter(\"object_tracking.avi\",\n", "# Initialize video writer to save the output video with the specified properties\n",
" cv2.VideoWriter_fourcc(*'mp4v'),\n", "out = cv2.VideoWriter(\"instance-segmentation-object-tracking.avi\", cv2.VideoWriter_fourcc(*\"MJPG\"), fps, (w, h))\n",
" fps,\n", "\n",
" (w, h))\n", "while True:\n",
"\n", " # Read a frame from the video\n",
"while cap.isOpened():\n", " ret, im0 = cap.read()\n",
" success, frame = cap.read()\n", " if not ret:\n",
" if success:\n", " print(\"Video frame is empty or video processing has been successfully completed.\")\n",
" results = model.track(frame, persist=True, verbose=False)\n", " break\n",
" boxes = results[0].boxes.xyxy.cpu()\n", "\n",
"\n", " # Create an annotator object to draw on the frame\n",
" if results[0].boxes.id is not None:\n", " annotator = Annotator(im0, line_width=2)\n",
"\n", "\n",
" # Extract prediction results\n", " # Perform object tracking on the current frame\n",
" clss = results[0].boxes.cls.cpu().tolist()\n", " results = model.track(im0, persist=True)\n",
" track_ids = results[0].boxes.id.int().cpu().tolist()\n", "\n",
" confs = results[0].boxes.conf.float().cpu().tolist()\n", " # Check if tracking IDs and masks are present in the results\n",
"\n", " if results[0].boxes.id is not None and results[0].masks is not None:\n",
" # Annotator Init\n", " # Extract masks and tracking IDs\n",
" annotator = Annotator(frame, line_width=2)\n", " masks = results[0].masks.xy\n",
"\n", " track_ids = results[0].boxes.id.int().cpu().tolist()\n",
" for box, cls, track_id in zip(boxes, clss, track_ids):\n", "\n",
" annotator.box_label(box, color=colors(int(cls), True), label=names[int(cls)])\n", " # Annotate each mask with its corresponding tracking ID and color\n",
"\n", " for mask, track_id in zip(masks, track_ids):\n",
" # Store tracking history\n", " annotator.seg_bbox(mask=mask, mask_color=colors(track_id, True), track_label=str(track_id))\n",
" track = track_history[track_id]\n", "\n",
" track.append((int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2)))\n", " # Write the annotated frame to the output video\n",
" if len(track) > 30:\n", " out.write(im0)\n",
" track.pop(0)\n", " # Display the annotated frame\n",
"\n", " cv2.imshow(\"instance-segmentation-object-tracking\", im0)\n",
" # Plot tracks\n", "\n",
" points = np.array(track, dtype=np.int32).reshape((-1, 1, 2))\n", " # Exit the loop if 'q' is pressed\n",
" cv2.circle(frame, (track[-1]), 7, colors(int(cls), True), -1)\n", " if cv2.waitKey(1) & 0xFF == ord(\"q\"):\n",
" cv2.polylines(frame, [points], isClosed=False, color=colors(int(cls), True), thickness=2)\n",
"\n",
" result.write(frame)\n",
" if cv2.waitKey(1) & 0xFF == ord(\"q\"):\n",
" break\n",
" else:\n",
" break\n", " break\n",
"\n", "\n",
"result.release()\n", "# Release the video writer and capture objects, and close all OpenCV windows\n",
"out.release()\n",
"cap.release()\n", "cap.release()\n",
"cv2.destroyAllWindows()" "cv2.destroyAllWindows()"
], ],
@ -182,24 +208,34 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
"#Community Support\n", "# Additional Resources\n",
"\n",
"## Community Support\n",
"\n",
"For more information on using tracking with Ultralytics, you can explore the comprehensive [Ultralytics Tracking Docs](https://docs.ultralytics.com/modes/track/). This guide covers everything from basic concepts to advanced techniques, ensuring you get the most out of tracking and visualization.\n",
"\n",
"## Ultralytics ⚡ Resources\n",
"\n",
"At Ultralytics, we are committed to providing cutting-edge AI solutions. Here are some key resources to learn more about our company and get involved with our community:\n",
"\n",
"- [Ultralytics HUB](https://ultralytics.com/hub): Simplify your AI projects with Ultralytics HUB, our no-code tool for effortless YOLO training and deployment.\n",
"- [Ultralytics Licensing](https://ultralytics.com/license): Review our licensing terms to understand how you can use our software in your projects.\n",
"- [About Us](https://ultralytics.com/about): Discover our mission, vision, and the story behind Ultralytics.\n",
"- [Join Our Team](https://ultralytics.com/work): Explore career opportunities and join our team of talented professionals.\n",
"\n",
"## YOLOv8 🚀 Resources\n",
"\n", "\n",
"For more information, you can explore <a href=\"https://docs.ultralytics.com/modes/track/\">Ultralytics Object Tracking Docs</a>\n", "YOLOv8 is the latest evolution in the YOLO series, offering state-of-the-art performance in object detection and image segmentation. Here are some essential resources to help you get started with YOLOv8:\n",
"\n", "\n",
"Ultralytics ⚡ resources\n", "- [GitHub](https://github.com/ultralytics/ultralytics): Access the YOLOv8 repository on GitHub, where you can find the source code, contribute to the project, and report issues.\n",
"- About Us – https://ultralytics.com/about\n", "- [Docs](https://docs.ultralytics.com/): Explore the official documentation for YOLOv8, including installation guides, tutorials, and detailed API references.\n",
"- Join Our Team – https://ultralytics.com/work\n", "- [Discord](https://ultralytics.com/discord): Join our Discord community to connect with other users, share your projects, and get help from the Ultralytics team.\n",
"- Contact Us – https://ultralytics.com/contact\n",
"- Discord – https://ultralytics.com/discord\n",
"- Ultralytics License – https://ultralytics.com/license\n",
"\n", "\n",
"YOLOv8 🚀 resources\n", "These resources are designed to help you leverage the full potential of Ultralytics' offerings and YOLOv8. Whether you're a beginner or an experienced developer, you'll find the information and support you need to succeed."
"- GitHub – https://github.com/ultralytics/ultralytics\n",
"- Docs – https://docs.ultralytics.com/"
], ],
"metadata": { "metadata": {
"id": "QrlKg-y3fEyD" "id": "QrlKg-y3fEyD"
} }
} }
] ]
} }

@ -1,6 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license # Ultralytics YOLO 🚀, AGPL-3.0 license
__version__ = "8.2.17" __version__ = "8.2.18"
from ultralytics.data.explorer.explorer import Explorer from ultralytics.data.explorer.explorer import Explorer
from ultralytics.models import RTDETR, SAM, YOLO, YOLOWorld from ultralytics.models import RTDETR, SAM, YOLO, YOLOWorld

Loading…
Cancel
Save