Skip to content

saadkhan2003/CCTV_Video_Anomaly_Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“Ή CCTV Video Anomaly Detection System

Python YOLOv11 OpenVINO FastAPI Version License

A high-performance, AI-powered video anomaly detection system designed for CCTV surveillance. Leverages YOLOv11 with OpenVINO optimization for real-time detection on standard CPUs, featuring a modern web interface and comprehensive alerting capabilities.


🌟 Key Features

πŸ” Advanced Detection

  • Multi-Object Detection: Persons, Vehicles, and Weapons (Knives, Baseball Bats, Scissors)
  • Robust Tracking: ByteTrack multi-object tracking with persistent IDs
  • High Accuracy: 85-95% detection accuracy on test datasets
  • Real-time Processing: 15-30 FPS on standard CPU hardware

🚨 Intelligent Anomaly Detection

  • Crowd Detection: Configurable threshold for group gatherings
  • Loitering Detection: Time-based stationary person identification
  • Fast Movement: Panic/running situation detection
  • Conflict Detection: Close-proximity group flagging
  • Weapon Alerts: High-priority notifications for dangerous objects

⚑ Performance & Optimization

  • OpenVINO Acceleration: 2-3x speedup on Intel CPUs
  • Async Processing: Non-blocking background task execution
  • Progress Tracking: Real-time progress monitoring
  • Auto-Optimization: Automatic model conversion on first run

🎨 Modern Web Interface

  • Responsive Design: Works on desktop, tablet, and mobile
  • Dark Theme: Eye-friendly glassmorphism UI
  • Real-time Updates: Live stream monitoring with SSE
  • Intuitive Dashboard: Easy-to-use video upload and analysis

πŸ“§ Alert System

  • Email Notifications: Automated SMTP alerts for anomalies
  • Customizable Triggers: Configure which events trigger alerts
  • Detailed Reports: HTML-formatted email with statistics

πŸ’Ύ Data Management

  • SQLite Database: Store analysis results and video metadata
  • Search Functionality: Find videos by name, date, or anomaly type
  • Statistics Dashboard: View aggregate analytics across all videos

πŸ“š Documentation

Comprehensive documentation is available in the /docs folder:

Document Description Link
πŸ“– User Manual Complete guide for end users USER_MANUAL.md
πŸ”§ Configuration Guide System configuration and tuning CONFIGURATION_GUIDE.md
πŸš€ Deployment Guide Production deployment instructions DEPLOYMENT_GUIDE.md
πŸ”Œ API Documentation REST API reference API_DOCUMENTATION.md
πŸ’» Developer Guide Contributing and development setup DEVELOPER_GUIDE.md
πŸ§ͺ Testing Documentation Test strategy and procedures TESTING_DOCUMENTATION.md
πŸ› οΈ Maintenance Guide Operational maintenance procedures MAINTENANCE_GUIDE.md
βœ… Project Closure Project completion summary PROJECT_CLOSURE.md

πŸ› οΈ Quick Start

Prerequisites

  • Python: 3.10 or higher
  • Operating System: Ubuntu 22.04 / Windows 10+ / macOS 12+
  • RAM: 8 GB minimum (16 GB recommended)
  • Storage: 10 GB free space

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/cctv-anomaly-detection.git
    cd cctv-anomaly-detection
  2. Create virtual environment (recommended)

    python3 -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt

    Note: First installation may take 5-10 minutes to download all dependencies.

First Run

  1. Start the application

    python app.py

    First run will automatically download YOLOv11 model (~50MB) and convert to OpenVINO format (~1-2 minutes).

  2. Access the dashboard

    Open your browser: http://localhost:8000
    
  3. Upload and analyze a video

    • Navigate to "Analyze" tab
    • Upload video file (MP4, AVI, MOV, MKV)
    • Set crowd threshold (default: 5)
    • Click "Start Analysis"
    • Monitor progress and view results

πŸš€ Usage

Video Analysis

Step 1: Upload Video

  • Drag & drop or click to select video file
  • Supported formats: MP4, AVI, MOV, MKV
  • Max file size: 500 MB (configurable)

Step 2: Configure Settings

  • Crowd Threshold: Number of people to trigger alert (default: 5)
  • Confidence: Detection confidence level 0.0-1.0 (default: 0.6)

Step 3: Start Analysis

  • Click "Start Analysis"
  • Monitor real-time progress
  • View results when complete

Step 4: Review Results

  • Anomaly statistics and breakdown
  • Download annotated video
  • View frame-by-frame analysis

Live Stream Monitoring

Webcam:

  1. Go to "Live" tab
  2. Select "Webcam" from dropdown
  3. Click "Start Monitoring"

IP Camera (RTSP):

  1. Go to "Live" tab
  2. Select "RTSP Stream"
  3. Enter URL: rtsp://username:password@ip:port/path
  4. Click "Start Monitoring"

Example RTSP URLs:

Hikvision: rtsp://admin:password@192.168.1.100:554/Streaming/Channels/101
Dahua: rtsp://admin:password@192.168.1.101:554/cam/realmonitor?channel=1&subtype=0
Generic: rtsp://admin:password@192.168.1.102:554/stream1

βš™οΈ Configuration

Basic Configuration

Create a .env file in the project root:

# Model Configuration
MODEL_SIZE=s          # n, s, m, l, x (s recommended)
DEVICE=cpu            # cpu, cuda, mps
CONFIDENCE_THRESHOLD=0.5

# Anomaly Thresholds
CROWD_THRESHOLD=5
LOITER_THRESHOLD=10.0

# Server Configuration
HOST=0.0.0.0
PORT=8000
DEBUG=false

Email Alerts

Create email_config.json:

{
    "smtp_server": "smtp.gmail.com",
    "smtp_port": 587,
    "sender_email": "security@company.com",
    "sender_password": "your-app-password",
    "receiver_emails": ["admin@example.com", "security@example.com"]
}

Gmail Setup:

  1. Enable 2-Step Verification
  2. Generate App Password: Google Account Settings
  3. Use App Password in config (not your regular password)

For detailed configuration options, see CONFIGURATION_GUIDE.md.


πŸ“ Project Structure

cctv-anomaly-detection/
β”œβ”€β”€ app.py                      # FastAPI application entry point
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ run.sh                      # Launch script
β”œβ”€β”€ .env                        # Environment configuration
β”œβ”€β”€ email_config.json          # Email alert settings
β”‚
β”œβ”€β”€ src/                       # Source code
β”‚   β”œβ”€β”€ detection/             # Detection and tracking
β”‚   β”‚   β”œβ”€β”€ yolo_detector.py   # YOLO detection logic
β”‚   β”‚   β”œβ”€β”€ visualization.py   # Bounding box drawing
β”‚   β”‚   └── live_stream.py     # Live stream processing
β”‚   β”œβ”€β”€ storage/               # Data persistence
β”‚   β”‚   └── database.py        # SQLite operations
β”‚   └── alerts/                # Alert system
β”‚       └── email_alerts.py    # Email notifications
β”‚
β”œβ”€β”€ static/                    # Static web assets
β”‚   β”œβ”€β”€ style.css              # UI styles (glassmorphism)
β”‚   └── videos/                # Processed output videos
β”‚
β”œβ”€β”€ templates/                 # HTML templates
β”‚   β”œβ”€β”€ index.html             # Main dashboard
β”‚   └── live.html              # Live stream page
β”‚
β”œβ”€β”€ docs/                      # Documentation
β”‚   β”œβ”€β”€ API_DOCUMENTATION.md
β”‚   β”œβ”€β”€ USER_MANUAL.md
β”‚   β”œβ”€β”€ CONFIGURATION_GUIDE.md
β”‚   β”œβ”€β”€ DEPLOYMENT_GUIDE.md
β”‚   β”œβ”€β”€ DEVELOPER_GUIDE.md
β”‚   β”œβ”€β”€ TESTING_DOCUMENTATION.md
β”‚   β”œβ”€β”€ MAINTENANCE_GUIDE.md
β”‚   └── PROJECT_CLOSURE.md
β”‚
β”œβ”€β”€ tests/                     # Test suite
β”‚   β”œβ”€β”€ test_detection.py
β”‚   β”œβ”€β”€ test_database.py
β”‚   β”œβ”€β”€ test_api.py
β”‚   └── test_email_alerts.py
β”‚
└── yolo11*.pt                 # YOLO model files

πŸ”Œ API Reference

The system provides a RESTful API for integration with other applications.

Key Endpoints

# Health check
GET /api/health

# Upload and analyze video
POST /api/analyze
  - file: video file
  - crowd_threshold: int
  - confidence: float

# Get task status
GET /api/task/{task_id}

# Get all videos
GET /api/videos

# Get specific video
GET /api/videos/{video_id}

# Search videos
GET /api/search?query=...

# Get statistics
GET /api/statistics

# Live stream
GET /api/live-stream?source=...

For complete API documentation, see API_DOCUMENTATION.md.


πŸ§ͺ Testing

Run Tests

# Install test dependencies
pip install pytest pytest-asyncio pytest-cov httpx

# Run all tests
pytest tests/

# Run with coverage
pytest --cov=src tests/

# Run specific test
pytest tests/test_detection.py

Test Coverage

Current test coverage: 85%

For detailed testing information, see TESTING_DOCUMENTATION.md.


🚒 Deployment

Production Deployment

Using Systemd (Linux):

# Create systemd service
sudo nano /etc/systemd/system/cctv-detection.service

# Enable and start
sudo systemctl enable cctv-detection
sudo systemctl start cctv-detection

Using Docker:

# Build image
docker build -t cctv-detection .

# Run container
docker run -d -p 8000:8000 --name cctv cctv-detection

Using Nginx Reverse Proxy:

server {
    listen 80;
    server_name cctv.company.com;
    
    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
    }
}

For complete deployment instructions, see DEPLOYMENT_GUIDE.md.


πŸ”§ Troubleshooting

Common Issues

Issue: Application won't start

# Check Python version
python --version  # Should be 3.10+

# Reinstall dependencies
pip install -r requirements.txt --force-reinstall

Issue: Slow detection speed

# Verify OpenVINO is being used
# Check logs for "Using OpenVINO" message

# Try smaller model
MODEL_SIZE=n python app.py

Issue: Camera connection fails

# Test RTSP URL with VLC
vlc rtsp://admin:password@192.168.1.100:554/stream1

# Check network connectivity
ping 192.168.1.100

For more troubleshooting, see USER_MANUAL.md.


πŸ“Š Performance Metrics

Metric Value Notes
Detection Speed 15-30 FPS On Intel i5 CPU with OpenVINO
Detection Accuracy 85-95% On standard test datasets
Memory Usage <2 GB During typical operation
Startup Time 10-15 seconds Includes model loading
API Response Time <2 seconds For most endpoints

🀝 Contributing

We welcome contributions! Please see our DEVELOPER_GUIDE.md for:

  • Development environment setup
  • Code style guidelines
  • Testing requirements
  • Pull request process

Quick Contribution Guide

  1. Fork the repository
  2. Create feature branch: git checkout -b feature/amazing-feature
  3. Write tests for new functionality
  4. Ensure tests pass: pytest tests/
  5. Format code: black src/ tests/
  6. Commit changes: git commit -m 'Add amazing feature'
  7. Push to branch: git push origin feature/amazing-feature
  8. Open Pull Request

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

Third-Party Licenses

  • YOLOv11: AGPL-3.0 (Ultralytics)
  • OpenVINO: Apache 2.0 (Intel)
  • FastAPI: MIT
  • Other dependencies: See individual package licenses

πŸ™ Acknowledgments

  • Ultralytics for YOLOv11 model
  • Intel for OpenVINO toolkit
  • FastAPI team for excellent web framework
  • ByteTrack for tracking algorithm
  • All open-source contributors

πŸ“ž Support

Getting Help

Professional Support

For enterprise support, custom development, or consulting:


πŸ—ΊοΈ Roadmap

Version 4.0 (Planned)

  • User authentication and authorization
  • Multi-camera simultaneous monitoring
  • Zone-based detection rules
  • Advanced analytics dashboard
  • Mobile app (iOS/Android)

Version 4.1 (Future)

  • PostgreSQL support
  • VMS integration
  • Custom model training UI
  • SMS/Push notifications

See PROJECT_CLOSURE.md for complete roadmap.


πŸ“ˆ Project Status

Status: βœ… Production Ready
Version: 3.0.0
Last Updated: March 3, 2026
Maintained: Yes
Test Coverage: 85%


⭐ Star History

If you find this project useful, please consider giving it a star! ⭐


Built with ❀️ for smarter, safer surveillance

About

Real-time CCTV Anomaly Detection system using YOLOv8 & OpenVINO. Detects crowds, weapons, loitering, and accidents with a modern glassmorphism UI.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors