diff --git a/docs/usecases/crowd-density-estimation.md b/docs/usecases/crowd-density-estimation.md index ff4b340..2b29361 100644 --- a/docs/usecases/crowd-density-estimation.md +++ b/docs/usecases/crowd-density-estimation.md @@ -1,34 +1,157 @@ --- comments: true -description: By leveraging the object detection capabilities of Ultralytics YOLO11 we can estimate the density of a crowd in a given area, which can be useful for event management, safety monitoring, and urban planning. -keywords: item counting, Ultralytics YOLO11, persons detection, persons tracking, surveillance, crowd analysis, density estimation, computer vision +description: Learn how to estimate crowd density using Ultralytics YOLO11 for efficient crowd monitoring and analysis. +keywords: crowd density estimation, YOLO11, Ultralytics YOLO11, AI crowd analysis, crowd monitoring, real-time crowd analysis, computer vision, crowd detection, advanced image processing --- -# Crowd Density Estimation using Ultralytics YOLO11 +# Accurate Crowd Density Estimation Using Ultralytics YOLO11 🎯 -Real-time crowd density estimation is crucial for event management, public safety, and urban planning. Using computer vision and AI, we can accurately monitor and analyze crowd dynamics with high precision and reliability. +Discover how to utilize [Ultralytics YOLO11](https://docs.ultralytics.com/models/yolo11/) for accurate crowd density estimation. This guide will take you through a step-by-step implementation using a YOLO11-based system to measure and monitor crowd density in various environments, improving safety and event management capabilities. -
+
- - Crowd Density Estimation Solution with YOLO11 + allow="autoplay"> +
-## Hardware, Model, and Dataset Information +## System Specifications Used for This Implementation -- **CPU**: Any modern CPU with multi-core processing capabilities. -- **GPU**: NVIDIA GPU with CUDA support recommended for real-time processing. -- **RAM**: Minimum 8GB RAM recommended. -- **Model**: Ultralytics YOLO11 model with person detection and tracking capabilities. -- **Dataset**: A proprietary dataset was annotated in-house, tailored specifically for this application. -- **Dependencies**: - - **ultralytics** - - **opencv-python** - - **numpy** +- **CPU**: Intel® Core™ i7-10700 CPU @ 2.90GHz for efficient processing. +- **GPU**: NVIDIA RTX 3060 for faster object detection. +- **RAM & Storage**: 32 GB RAM and 512 GB SSD for optimal performance. +- **Model**: Pre-trained YOLO11 model for person detection. +- **Dataset**: Custom dataset for various crowd scenarios to fine-tune YOLO11 performance. -## Real-World Applications for Item Counting in Retail +## How to Implement Crowd Density Estimation + +### Step 1: Setup and Model Initialization + +To get started, the code utilizes a pre-trained YOLO11 model for person detection. This model is loaded into the `CrowdDensityEstimation` class, which is designed to track individuals in a crowd and estimate crowd density in real time. + +#### Code to Initialize and Track with YOLO11 + +```python +import cv2 +from estimator import CrowdDensityEstimation + +def main(): + estimator = CrowdDensityEstimation() + + # Open video capture (0 for webcam, or video file path) + cap = cv2.VideoCapture("path/to/video/file.mp4") + + # Get video properties for output + frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + fps = int(cap.get(cv2.CAP_PROP_FPS)) + fourcc = cv2.VideoWriter_fourcc(*'mp4v') + out = cv2.VideoWriter('path/to/video/output-file.mp4', fourcc, fps, (frame_width, frame_height)) + + while True: + ret, frame = cap.read() + if not ret: + break + + # Process frame + processed_frame, density_info = estimator.process_frame(frame) + + # Display output + estimator.display_output(processed_frame, density_info) + + # Write output frame + out.write(processed_frame) + + # Break loop on 'q' press + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + # Cleanup + cap.release() + cv2.destroyAllWindows() + +if __name__ == "__main__": + main() +``` + +This setup captures frames from a video source, processes them using YOLO11 to detect people, and calculates crowd density. + +### Step 2: Real-Time Crowd Detection and Tracking + +The core of the implementation relies on tracking individuals in each frame using the YOLO11 model and estimating the crowd density. This is achieved through a series of steps, which include detecting people, calculating density, and classifying the crowd level. + +#### Code for Crowd Density Estimation + +The main class `CrowdDensityEstimation` includes the following functionality: + +- **Person Detection**: Using YOLO11 to detect individuals in each frame. +- **Density Calculation**: Based on the number of detected persons relative to the frame area. +- **Tracking**: Visualization of tracking history for each detected person. + +```python +# Install ultralytics package +# pip install ultralytics + +import cv2 +import numpy as np +from ultralytics import YOLO +from collections import defaultdict + +class CrowdDensityEstimation: + def __init__(self, model_path='yolo11n.pt', conf_threshold=0.3): + self.model = YOLO(model_path) + self.conf_threshold = conf_threshold + self.track_history = defaultdict(lambda: []) + self.density_levels = { + 'Low': (0, 0.2), # 0-0.2 persons/m² + 'Medium': (0.2, 0.5), # 0.2-0.5 persons/m² + 'High': (0.5, 0.8), # 0.5-0.8 persons/m² + 'Very High': (0.8, float('inf')) # >0.8 persons/m² + } + + def extract_tracks(self, im0): + results = self.model.track(im0, persist=True, conf=self.conf_threshold, classes=[0]) + return results + + def calculate_density(self, results, frame_area): + if not results or len(results) == 0: + return 0, 'Low', 0 + + person_count = len(results[0].boxes) + density_value = person_count / frame_area * 10000 + + density_level = 'Low' + for level, (min_val, max_val) in self.density_levels.items(): + if min_val <= density_value < max_val: + density_level = level + break + + return density_value, density_level, person_count +``` + +### Step 3: Visualizing Density and Results + +Once density is calculated, the processed frame is annotated with information like density level, person count, and a tracking visualization. This enhances situational awareness by providing clear visual cues. + +#### Displaying Density Information on Video Frames + +```python +def display_output(self, im0, density_info): + density_value, density_level, person_count = density_info + + cv2.rectangle(im0, (0, 0), (350, 150), (0, 0, 0), -1) + + cv2.putText(im0, f'Density Level: {density_level}', (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + cv2.putText(im0, f'Person Count: {person_count}', (10, 70), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + cv2.putText(im0, f'Density Value: {density_value:.2f}', (10, 110), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + + # Display the frame + cv2.imshow('Crowd Density Estimation', im0) +``` + +## Applications of Crowd Density Estimation - **Public Safety:** - **Early Warning System:** Detecting unusual crowd formations. @@ -41,7 +164,9 @@ Real-time crowd density estimation is crucial for event management, public safet - **Space Utilization:** Analyzing how people use public spaces. - **Infrastructure Planning:** Designing facilities based on crowd patterns. -## Social Resources +## Explore More -- [Ultralytics Docs](https://docs.ultralytics.com/fr) -- [OpenCV Documentation](https://opencv.org/) \ No newline at end of file +- [Learn More About YOLO11 on the Official Ultralytics Documentation](https://docs.ultralytics.com/models/yolo11/) +- [Join the Discussion on LinkedIn](https://www.linkedin.com/posts/ivan-apedo_ai-computervision-yolo11-activity-7266460747285602304-D_xR?utm_source=share&utm_medium=member_desktop) + +Unlock the potential of advanced crowd monitoring using YOLO11 and streamline operations for various sectors! 🚀 \ No newline at end of file