{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "cells": [ { "cell_type": "markdown", "source": [ "
\n", "\n", " \n", " \n", "\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/) | [हिन्दी](https://docs.ultralytics.com/hi/) | [العربية](https://docs.ultralytics.com/ar/)\n", "\n", " \"Open\n", "\n", "Welcome to the Ultralytics YOLOv8 🚀 notebook! YOLOv8 is the latest version of the YOLO (You Only Look Once) AI models developed by Ultralytics. This notebook serves as the starting point for exploring the Object Counting and understand its features and capabilities.\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", "\n", "We hope that the resources in this notebook will help you get the most out of Ultralytics Object Counting. Please browse the YOLOv8 Docs for details, raise an issue on GitHub for support, and join our Discord community for questions and discussions!\n", "\n", "
" ], "metadata": { "id": "PN1cAxdvd61e" } }, { "cell_type": "markdown", "source": [ "# Setup\n", "\n", "Pip install `ultralytics` and [dependencies](https://github.com/ultralytics/ultralytics/blob/main/pyproject.toml) and check software and hardware." ], "metadata": { "id": "o68Sg1oOeZm2" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "9dSwz_uOReMI" }, "outputs": [], "source": [ "!pip install ultralytics" ] }, { "cell_type": "markdown", "source": [ "# Ultralytics Object Counting\n", "\n", "Counting objects using Ultralytics YOLOv8 entails the precise detection and enumeration of specific objects within videos and camera streams. YOLOv8 demonstrates exceptional performance in real-time applications, delivering efficient and accurate object counting across diverse scenarios such as crowd analysis and surveillance. This is attributed to its advanced algorithms and deep learning capabilities." ], "metadata": { "id": "m7VkxQ2aeg7k" } }, { "cell_type": "code", "source": [ "from ultralytics import YOLO\n", "from ultralytics.solutions import object_counter\n", "import cv2\n", "\n", "model = YOLO(\"yolov8n.pt\")\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", "# Define region points\n", "region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]\n", "\n", "# Video writer\n", "video_writer = cv2.VideoWriter(\"object_counting_output.avi\",\n", " cv2.VideoWriter_fourcc(*'mp4v'),\n", " fps,\n", " (w, h))\n", "\n", "# Init Object Counter\n", "counter = object_counter.ObjectCounter()\n", "counter.set_args(view_img=True,\n", " reg_pts=region_points,\n", " classes_names=model.names,\n", " draw_tracks=True)\n", "\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", " tracks = model.track(im0, persist=True, show=False)\n", "\n", " im0 = counter.start_counting(im0, tracks)\n", " video_writer.write(im0)\n", "\n", "cap.release()\n", "video_writer.release()\n", "cv2.destroyAllWindows()" ], "metadata": { "id": "Cx-u59HQdu2o" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "#Community Support\n", "\n", "For more information, you can explore Ultralytics Object Counting Docs\n", "\n", "Ultralytics ⚡ resources\n", "- About Us – https://ultralytics.com/about\n", "- Join Our Team – https://ultralytics.com/work\n", "- Contact Us – https://ultralytics.com/contact\n", "- Discord – https://discord.gg/2wNGbc6g9X\n", "- Ultralytics License – https://ultralytics.com/license\n", "\n", "YOLOv8 🚀 resources\n", "- GitHub – https://github.com/ultralytics/ultralytics\n", "- Docs – https://docs.ultralytics.com/" ], "metadata": { "id": "QrlKg-y3fEyD" } } ] }