This is the AI/ML component of the CrowdShield AI system. It handles real-time crowd density estimation, person detection, privacy preservation, and alert generation.
- YOLOv8 Person Detection: Fast and accurate person detection using state-of-the-art deep learning
- Crowd Density Estimation: Calculates density levels (Low, Medium, High, Critical) based on area capacity
- Privacy Preservation: Automatically blurs faces in video feeds to protect individual privacy
- Overcrowding Alerts: Generates alerts when crowd density exceeds safe thresholds
- Backend Integration: Seamlessly communicates with FastAPI backend for data persistence and analytics
ai-engine/
├── config.py # Configuration and settings management
├── logger.py # Logging setup
├── yolo_detector.py # YOLOv8 person detection module
├── people_counter.py # People counting with centroid tracking
├── density_estimator.py # Density calculation and risk classification
├── face_blur.py # Privacy-preserving face blurring
├── alert_generator.py # Overcrowding alert generation
├── video_processor.py # Main video processing pipeline
├── backend_client.py # HTTP client for backend API communication
├── ai_engine.py # Main AI Engine orchestrator
├── api_server.py # FastAPI server for AI Engine APIs
├── requirements.txt # Python dependencies
├── .env.example # Example environment configuration
└── README.md # This file
-
Clone the repository
git clone <repository-url> cd ai-engine
-
Create virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment
cp .env.example .env # Edit .env with your configuration
from ai_engine import get_engine
import cv2
# Get engine instance
engine = get_engine()
# Create processor for a camera
processor = engine.create_processor("camera_001", capacity=100)
# Process video file
stats = processor.process_video_file("input.mp4", "output.mp4")
print(stats)python api_server.pyServer runs on http://localhost:8001
POST /api/process-frame- Process single framePOST /api/process-video- Process video filePOST /api/stream- Process video streamPOST /api/init-camera- Initialize cameraGET /api/cameras/{camera_id}/stats- Get camera statisticsGET /api/stats- Get all camera statisticsGET /api/model-info- Get model informationGET /health- Health check
Key environment variables:
# Backend API
BACKEND_API_URL=http://localhost:8000
BACKEND_API_KEY=your_api_key
# YOLO Model
YOLO_MODEL=yolov8m.pt
YOLO_CONFIDENCE=0.5
# Density Thresholds (%)
DENSITY_LOW_THRESHOLD=30
DENSITY_MEDIUM_THRESHOLD=60
DENSITY_HIGH_THRESHOLD=85
# Face Blurring
ENABLE_FACE_BLUR=true
BLUR_KERNEL_SIZE=31
# Processing
FRAME_SKIP=5
DEFAULT_CAPACITY=100Video/Frame Input
↓
YOLO Person Detection
↓
People Counting
↓
Density Estimation
↓
Risk Classification
↓
Face Blurring (Privacy)
↓
Alert Generation
↓
Backend API Submission
↓
Dashboard Analytics
The system classifies crowd density into four levels:
| Density % | Level | Risk Score | Alert Triggered |
|---|---|---|---|
| 0-30% | Low | 0.0-0.3 | No |
| 31-60% | Medium | 0.3-0.6 | No |
| 61-85% | High | 0.6-0.85 | Yes |
| 86-100% | Critical | 0.85-1.0 | Yes |
- Face Blurring: Automatically detects and blurs faces using Haar Cascade classifiers
- Anonymization: All stored data contains only crowd counts and density metrics, never identities
- No Facial Recognition: System deliberately avoids biometric identification
- Data Minimization: Only essential metadata is stored and transmitted
- Real-time Processing: Processes at 30+ FPS on modern hardware
- Frame Skipping: Configurable frame skipping for performance optimization
- Async Communication: Non-blocking backend API submissions
- Efficient Tracking: Centroid-based person tracking across frames
The AI Engine communicates with the FastAPI backend through REST APIs:
- Detection Submission: Sends density and count data after each processed frame
- Alert Submission: Sends alerts when density exceeds safe thresholds
- Camera Info Retrieval: Fetches camera capacity and metadata
- Stream Configuration: Gets configuration for stream processing
- Graceful degradation if backend is unavailable
- Comprehensive logging for debugging
- Timeout handling for network requests
- Invalid input validation
- Python 3.8+
- CUDA-capable GPU (recommended for real-time processing)
- OpenCV 4.8+
- PyTorch 2.0+
- YOLOv8 model files (auto-downloaded on first use)
The model will auto-download on first use. Ensure you have internet connectivity.
- Adjust
YOLO_CONFIDENCEthreshold in.env - Ensure adequate lighting in video feeds
- Check camera placement and angle
- Increase
FRAME_SKIPto process fewer frames - Reduce
FRAME_WIDTHandFRAME_HEIGHT - Use a smaller YOLO model (yolov8n instead of yolov8l)
- Verify
BACKEND_API_URLis correct - Check backend service is running
- Verify network connectivity
This project is part of the CrowdShield AI hackathon submission.
For issues, feature requests, or questions, contact the development team.