Skip to content

🤖 AI-Powered Security & Analytics Backend for AP Government Hackathon | FastAPI + YOLO v8 + Face Recognition + Gemini Vision | Real-time camera monitoring, person detection, gunny bag counting & intelligent security system with RTSP stream processing

Notifications You must be signed in to change notification settings

rudra-sah00/A-AI-BACKEND

Repository files navigation

🚀 AI-Powered Security & Analytics Backend

Python FastAPI YOLO OpenCV License

🏆 Andhra Pradesh Government Hackathon Project

An advanced AI backend system combining computer vision, real-time analytics, and intelligent security monitoring


📋 Table of Contents


🎯 Project Overview

This AI-powered backend system was developed for the Andhra Pradesh Government Hackathon to address critical security and monitoring challenges. The system combines cutting-edge computer vision, machine learning, and real-time analytics to provide intelligent surveillance, object detection, and automated monitoring capabilities.

🌟 Core Objectives

  • Real-time Security Monitoring: Advanced person detection and face recognition
  • Intelligent Analytics: AI-powered contextual analysis using Gemini Vision API
  • Object Counting: Specialized gunny bag counting for warehouse/agricultural applications
  • Stream Management: Multi-camera RTSP stream processing and management
  • Attendance Tracking: Automated attendance system with authorization checks

✨ Key Features

🎥 Multi-Camera Management System

  • RTSP Stream Processing: Support for multiple IP cameras with real-time validation
  • Dynamic Filter System: Configurable AI filters per camera
  • Stream Health Monitoring: Automatic reconnection and status tracking
  • WebSocket Integration: Real-time status updates and notifications

🤖 Advanced AI Engine

  • YOLO v8 Object Detection: High-performance person and object detection
  • Face Recognition: Multiple algorithms (face_recognition, InsightFace)
  • Gemini Vision Integration: Natural language queries about video content
  • Custom Model Support: Trained models for specific use cases (gunny bags)

🛡️ Security & Authorization

  • Real-time Person Authentication: Face-based authorization system
  • Unauthorized Entry Detection: Automatic logging of security breaches
  • Role-based Access Control: Different access levels for different users
  • Attendance Tracking: Automated entry/exit logging

📊 Analytics & Monitoring

  • Real-time Processing: Live video analysis with minimal latency
  • Performance Metrics: FPS tracking, detection accuracy, system health
  • Data Persistence: JSON-based data storage with structured logging
  • Query Interface: Natural language queries about camera feeds

🎯 Specialized Applications

  • Gunny Bag Counting: Agricultural/warehouse inventory management
  • Crowd Analytics: Person counting and movement tracking
  • Incident Detection: Automated alert system for security events

🏗️ System Architecture

graph TB
    subgraph "Frontend Layer"
        UI[React Frontend]
        WS[WebSocket Client]
    end

    subgraph "API Layer"
        API[FastAPI Server]
        REST[REST Endpoints]
        WSS[WebSocket Server]
    end

    subgraph "AI Engine Core"
        ENGINE[AI Engine]
        PROC[Processors]
        YOLO[YOLO Model]
        FACE[Face Recognition]
        VISION[Gemini Vision]
    end

    subgraph "Data Processing"
        STREAM[Stream Processor]
        DETECT[Detection Engine]
        TRACK[Object Tracking]
        FILTER[Filter System]
    end

    subgraph "Storage Layer"
        JSON[JSON Files]
        IMGS[Image Storage]
        LOGS[Log Files]
        MODELS[Model Files]
    end

    subgraph "External Systems"
        RTSP[RTSP Cameras]
        GEMINI[Gemini API]
        MODELS_EXT[Model Registry]
    end

    UI --> API
    WS --> WSS
    API --> ENGINE
    ENGINE --> PROC
    PROC --> YOLO
    PROC --> FACE
    PROC --> VISION
    ENGINE --> STREAM
    STREAM --> DETECT
    DETECT --> TRACK
    TRACK --> FILTER
    ENGINE --> JSON
    ENGINE --> IMGS
    ENGINE --> LOGS
    YOLO --> MODELS
    RTSP --> STREAM
    VISION --> GEMINI
    YOLO --> MODELS_EXT
Loading

🔄 Data Flow Architecture

sequenceDiagram
    participant C as Camera
    participant S as Stream Processor
    participant Y as YOLO Engine
    participant F as Face Recognition
    participant A as AI Engine
    participant API as REST API
    participant UI as Frontend

    C->>S: RTSP Stream
    S->>Y: Frame Processing
    Y->>A: Person Detection
    A->>F: Face Extraction
    F->>A: Recognition Result
    A->>API: Detection Data
    API->>UI: Real-time Updates
    
    Note over A: Apply Filters & Rules
    Note over A: Log Security Events
    Note over A: Update Analytics
Loading

🚀 Quick Start

Prerequisites

  • Python 3.8+
  • CUDA-compatible GPU (optional, for faster inference)
  • Webcam or IP camera with RTSP support

1. Clone & Setup

git clone https://github.com/rudra-sah00/A-AI-BACKEND.git
cd A-AI-BACKEND

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

2. Environment Configuration

# Create .env file
cat > .env << EOF
BACKEND_URL=http://localhost:8000
SECRET_KEY=your-super-secret-key-here
DATA_DIR=data
ENABLE_GPU=false
AI_ENGINE_MONITOR_INTERVAL_SECONDS=30
EOF

3. Start the Server

# Development mode
python app/main.py

# Production mode
uvicorn app.main:app --host 0.0.0.0 --port 8000

4. Access the API


💻 Installation

🐧 Linux (Ubuntu/Debian)

# System dependencies
sudo apt update
sudo apt install -y python3 python3-pip python3-venv
sudo apt install -y libgl1-mesa-glx libglib2.0-0 libsm6 libxext6 libxrender-dev
sudo apt install -y ffmpeg libavcodec-dev libavformat-dev libswscale-dev

# Clone and setup
git clone https://github.com/rudra-sah00/A-AI-BACKEND.git
cd A-AI-BACKEND
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

# CUDA support (optional)
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118

🍎 macOS

# Install Homebrew (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# System dependencies
brew install [email protected] ffmpeg opencv

# Clone and setup
git clone https://github.com/rudra-sah00/A-AI-BACKEND.git
cd A-AI-BACKEND
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

🪟 Windows

# Install Python 3.8+ from python.org
# Install Git from git-scm.com

# Clone and setup
git clone https://github.com/rudra-sah00/A-AI-BACKEND.git
cd A-AI-BACKEND
python -m venv venv
venv\Scripts\activate
python -m pip install --upgrade pip
pip install -r requirements.txt

# Install Microsoft Visual C++ Redistributable if needed

🐳 Docker Installation

# Build Docker image
docker build -t ai-backend .

# Run container
docker run -d \
  --name ai-backend \
  -p 8000:8000 \
  -v $(pwd)/data:/app/data \
  -v $(pwd)/logs:/app/logs \
  ai-backend

# With GPU support
docker run -d \
  --name ai-backend-gpu \
  --gpus all \
  -p 8000:8000 \
  -v $(pwd)/data:/app/data \
  ai-backend

📱 API Documentation

🎥 Camera Management

Create Camera

POST /api/v1/cameras/
Content-Type: multipart/form-data

name: "Main Entrance"
rtsp_url: "rtsp://admin:[email protected]/stream"
verify_stream: true

List Cameras

GET /api/v1/cameras/

Update Camera Filters

PUT /api/v1/cameras/{camera_id}/filters
Content-Type: application/json

{
  "filters": [
    {
      "filter_name": "authorized_entry",
      "enabled": true
    },
    {
      "filter_name": "OllamaVision",
      "enabled": true
    }
  ]
}

🤖 AI Vision Queries

Process Contextual Query

POST /api/v1/ai-vision/query
Content-Type: application/json

{
  "camera_id": "camera-uuid",
  "query": "How many people are in the scene?"
}

👥 User Management

Add User

POST /api/v1/users/
Content-Type: multipart/form-data

name: "John Doe"
role: "employee"
image: [image file]

List Users

GET /api/v1/users/

📊 Analytics & Reports

Get Attendance Records

GET /api/v1/analytics/attendance?date=2024-01-15

Get Unauthorized Entries

GET /api/v1/analytics/unauthorized

🔧 Configuration

⚙️ Environment Variables

Variable Default Description
BACKEND_URL http://localhost:8000 Backend server URL
SECRET_KEY your-secret-key-here JWT secret key
DATA_DIR data Data storage directory
ENABLE_GPU false Enable GPU acceleration
AI_ENGINE_MONITOR_INTERVAL_SECONDS 30 AI engine monitoring interval

📂 Directory Structure

A-AI-BACKEND/
├── app/                          # Main application
│   ├── main.py                   # FastAPI application entry
│   ├── ai_engine/               # AI processing core
│   │   ├── engine.py            # Main AI engine
│   │   ├── models/              # AI models
│   │   │   ├── yolo_model.py    # YOLO implementation
│   │   │   └── deploy.prototxt  # Model configuration
│   │   ├── processors/          # Processing modules
│   │   │   ├── ai_vision_processor.py      # Gemini Vision
│   │   │   ├── attendance_processor.py     # Attendance tracking
│   │   │   ├── authorized_entry_processor.py # Security
│   │   │   └── base_processor.py          # Base processor
│   │   └── utils/               # Utility functions
│   │       ├── face_detector.py # Face detection
│   │       └── image_utils.py   # Image processing
│   ├── api/                     # REST API
│   │   ├── api.py              # API router
│   │   └── endpoints/          # API endpoints
│   ├── core/                   # Core configuration
│   │   ├── config.py          # Application settings
│   │   └── websocket_manager.py # WebSocket handling
│   ├── models/                 # Data models
│   ├── services/              # Business logic
│   └── utils/                 # Utilities
├── data/                      # Data storage
│   ├── cameras.json          # Camera configurations
│   ├── users.json           # User database
│   ├── rules.json           # Security rules
│   ├── attendance/          # Attendance records
│   ├── unauthorized/        # Security logs
│   └── gunny_bag_dataset/   # Training data
├── logs/                    # Application logs
└── requirements.txt         # Python dependencies

🎛️ Camera Filter Configuration

Available Filters

  • authorized_entry: Person authorization and access control
  • OllamaVision: AI-powered contextual analysis (now using Gemini)
  • attendance: Automatic attendance tracking
  • object_counting: Generic object counting

Filter Configuration Example

{
  "filters": [
    {
      "filter_id": "auth-001",
      "filter_name": "authorized_entry",
      "enabled": true,
      "settings": {
        "confidence_threshold": 0.4,
        "recognition_method": "face_recognition"
      }
    },
    {
      "filter_id": "vision-001", 
      "filter_name": "OllamaVision",
      "enabled": true,
      "settings": {
        "model": "gemini-2.0-flash",
        "query_cooldown": 1,
        "timeout": 30
      }
    }
  ]
}

🎥 Features Deep Dive

🔍 1. Multi-Camera RTSP Management

The system supports unlimited IP cameras with advanced stream management:

Features:

  • Real-time Stream Validation: Automatically tests RTSP streams before adding
  • Dynamic Reconnection: Handles network interruptions gracefully
  • Performance Monitoring: Tracks FPS, latency, and stream health
  • Concurrent Processing: Multiple cameras processed simultaneously

Technical Implementation:

# Stream validation example
validation_result = StreamValidator.validate_rtsp_stream(rtsp_url)
if validation_result["is_valid"]:
    camera = camera_service.create_camera(camera_data)

Supported Camera Types:

  • IP Cameras with RTSP support
  • USB webcams (via OpenCV)
  • Network video recorders (NVRs)
  • ONVIF-compliant devices

🤖 2. YOLO v8 Object Detection

Advanced object detection powered by Ultralytics YOLO v8:

Capabilities:

  • Person Detection: High-accuracy human detection in various conditions
  • Real-time Processing: Optimized for live video streams
  • Custom Training: Support for domain-specific models
  • Multi-scale Detection: Handles objects of various sizes

Performance Optimizations:

  • GPU Acceleration: CUDA support for faster inference
  • Model Quantization: Reduced model size for edge deployment
  • Batch Processing: Efficient multi-frame processing
  • Dynamic Resolution: Adaptive resolution based on system performance

Code Example:

# YOLO detection implementation
detections = yolo_model.detect_persons(frame)
for detection in detections:
    bbox = detection["bbox"]
    confidence = detection["confidence"]
    # Process detection...

👤 3. Advanced Face Recognition

Multi-algorithm face recognition system with high accuracy:

Recognition Methods:

  1. face_recognition Library: dlib-based deep learning
  2. InsightFace: State-of-the-art face analysis
  3. OpenCV Face Detection: Fallback method
  4. Custom Enhancement: Image preprocessing for better accuracy

Features:

  • Multiple Algorithm Support: Automatic fallback between methods
  • Glasses & Lighting Tolerance: Enhanced preprocessing for challenging conditions
  • Real-time Encoding: Fast face embedding generation
  • Similarity Scoring: Confidence-based matching

Technical Details:

# Face recognition pipeline
face_encoding = face_recognition.face_encodings(face_rgb, model="small")
face_distance = face_recognition.face_distance(ref_encoding, face_encoding[0])
similarity = 1 - float(face_distance[0])
is_match = face_distance[0] <= threshold

🧠 4. Gemini Vision Integration

AI-powered contextual analysis using Google's Gemini 2.0 Flash model:

Capabilities:

  • Natural Language Queries: Ask questions about camera feeds
  • Scene Understanding: Comprehensive visual analysis
  • Object Counting: "How many people are in the scene?"
  • Activity Recognition: "What are people doing?"
  • Safety Assessment: "Are there any safety violations?"

Query Examples:

// Sample queries
{
  "camera_id": "main-entrance",
  "query": "How many people are wearing masks?"
}

{
  "camera_id": "warehouse",
  "query": "Are there any safety hazards visible?"
}

{
  "camera_id": "parking",
  "query": "Count the number of vehicles"
}

Response Format:

{
  "success": true,
  "response": "I can see 3 people in the scene. Two people are wearing masks, and one person is not wearing a mask.",
  "camera_id": "main-entrance",
  "camera_name": "Main Entrance Camera",
  "timestamp": "2024-01-15T14:30:00Z",
  "model": "gemini-2.0-flash"
}

🏭 5. Gunny Bag Counting System

Specialized computer vision solution for agricultural/warehouse applications:

Use Cases:

  • Warehouse Inventory: Automated counting of stored bags
  • Agricultural Processing: Grain storage monitoring
  • Supply Chain: Real-time inventory tracking
  • Quality Control: Visual inspection integration

Features:

  • Custom YOLO Model: Trained specifically for gunny bag detection
  • Real-time Counting: Live video analysis with count overlay
  • Batch Processing: Process recorded videos
  • Export Capabilities: Save results with annotations

Training Data:

  • 100+ Annotated Images: Diverse lighting and angles
  • Two Classes: gunny_bag_counting, gunny_bag_ignore
  • YOLO Format: Standard annotation format
  • Validation Split: 80/20 train/validation split

Usage:

# Run gunny bag counter
python gunny_bag_counter_app.py \
  --video_path "warehouse_video.mp4" \
  --model_path "data/gunny_bag_dataset/best.pt" \
  --output_dir "results/"

🛡️ 6. Security & Authorization System

Comprehensive security monitoring with real-time alerts:

Authorization Features:

  • Face-based Authentication: Automatic person identification
  • Role-based Access: Different authorization levels
  • Unauthorized Detection: Real-time security breach alerts
  • Entry/Exit Logging: Comprehensive access tracking

Security Rules Engine:

{
  "rule_id": "entrance-security",
  "camera_ids": ["main-entrance", "side-door"],
  "conditions": {
    "require_authorization": true,
    "allowed_roles": ["employee", "admin", "visitor"],
    "time_restrictions": {
      "start": "06:00",
      "end": "22:00"
    }
  },
  "actions": {
    "log_entry": true,
    "send_alert": true,
    "capture_image": true
  }
}

Attendance Tracking:

  • Automatic Entry/Exit: Face recognition-based logging
  • Daily Reports: Comprehensive attendance analytics
  • Late Arrival Alerts: Configurable time-based notifications
  • Export Features: CSV/JSON data export

📊 7. Real-time Analytics Dashboard

Comprehensive monitoring and analytics capabilities:

Metrics Tracked:

  • Detection Performance: FPS, accuracy, latency
  • System Health: CPU, memory, GPU utilization
  • Security Events: Unauthorized entries, alerts
  • User Analytics: Entry patterns, attendance statistics

Data Visualization:

  • Real-time Charts: Live performance metrics
  • Historical Trends: Long-term pattern analysis
  • Alert Dashboards: Security event monitoring
  • Export Capabilities: Data export for external analysis

📊 Model Training

🎯 Custom YOLO Training

Dataset Preparation:

# Prepare training data
python prepare_training_data.py \
  --input_dir "raw_images/" \
  --output_dir "data/gunny_bag_dataset/" \
  --train_split 0.8

Training Configuration:

# dataset.yaml
names:
  0: gunny_bag_counting
  1: gunny_bag_ignore
path: /path/to/gunny_bag_dataset
train: train/images
val: val/images

Training Process:

# Train YOLO model
python train_yolo.py \
  --data "data/gunny_bag_dataset/dataset.yaml" \
  --epochs 100 \
  --imgsz 640 \
  --batch 16 \
  --device 0

📈 Training Metrics:

Metric Value Description
[email protected] 0.85+ Mean Average Precision at IoU 0.5
Precision 0.90+ True positive rate
Recall 0.88+ Detection rate
Inference Speed 15ms Average inference time per frame

🧪 Testing & Validation

🔧 Unit Tests

# Run all tests
pytest tests/ -v

# Test specific modules
pytest tests/test_yolo_model.py -v
pytest tests/test_face_recognition.py -v
pytest tests/test_api_endpoints.py -v

📊 Performance Testing

# Benchmark YOLO performance
python benchmark_yolo.py --test_images 1000

# Test stream processing
python test_stream_performance.py --rtsp_url "rtsp://..."

# Load testing
python load_test_api.py --concurrent_users 50

Validation Scripts

# Validate installation
python validate_installation.py

# Test camera connectivity
python test_camera_connection.py --rtsp_url "rtsp://..."

# Validate model accuracy
python validate_model_accuracy.py --test_dataset "validation/"

📈 Performance & Monitoring

🚀 Performance Metrics

Component Metric Target Actual
YOLO Detection FPS 30+ 35+
Face Recognition Processing Time <100ms 85ms
API Response Latency <200ms 150ms
Memory Usage RAM <2GB 1.5GB
GPU Utilization VRAM <4GB 3.2GB

📊 Monitoring Dashboard

System Health:

  • CPU/Memory/GPU utilization
  • Active camera streams
  • Detection accuracy metrics
  • Error rate tracking

Security Metrics:

  • Authorized vs unauthorized entries
  • Failed recognition attempts
  • Alert frequency analysis
  • Response time tracking

🔍 Logging & Debugging

Log Levels:

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('backend.log')
    ]
)

Debug Mode:

# Enable debug logging
export LOG_LEVEL=DEBUG
python app/main.py

🛠️ Development Guide

🔧 Development Setup

# Development dependencies
pip install -r requirements-dev.txt

# Pre-commit hooks
pre-commit install

# Code formatting
black app/
isort app/

# Type checking
mypy app/

📝 Code Standards

Python Style Guide:

  • PEP 8 compliance
  • Black code formatting
  • isort import sorting
  • Type hints for all functions
  • Docstrings for all modules/classes

API Design:

  • RESTful endpoint design
  • Consistent response formats
  • Comprehensive error handling
  • OpenAPI documentation

🔄 CI/CD Pipeline

# .github/workflows/ci.yml
name: CI/CD Pipeline

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install -r requirements-dev.txt
      - name: Run tests
        run: pytest tests/ -v
      - name: Code quality
        run: |
          black --check app/
          isort --check app/
          mypy app/

🐳 Docker Development

# Dockerfile.dev
FROM python:3.9-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

# Development dependencies
COPY requirements-dev.txt .
RUN pip install -r requirements-dev.txt

COPY . .

# Development server
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

🔒 Security & Privacy

🛡️ Security Features

Data Protection:

  • Encrypted Storage: Sensitive data encryption at rest
  • Secure Transmission: HTTPS/WSS for all communications
  • Access Control: Role-based permissions
  • Audit Logging: Comprehensive security event logging

Privacy Compliance:

  • Data Minimization: Only necessary data collection
  • Retention Policies: Automatic data cleanup
  • Anonymization: Face encodings instead of raw images
  • Consent Management: User permission tracking

🔐 Authentication & Authorization

# JWT Token Implementation
from jose import JWTError, jwt
from passlib.context import CryptContext

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

def create_access_token(data: dict):
    to_encode = data.copy()
    expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    to_encode.update({"exp": expire})
    encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
    return encoded_jwt

🔒 Security Best Practices

  1. Regular Updates: Keep all dependencies updated
  2. Input Validation: Sanitize all user inputs
  3. Rate Limiting: Prevent API abuse
  4. Monitoring: Real-time security event monitoring
  5. Backup: Regular data backups
  6. Recovery: Disaster recovery procedures

🌐 API Integration Examples

🐍 Python Client

import requests
import asyncio
import websockets

class AIBackendClient:
    def __init__(self, base_url="http://localhost:8000"):
        self.base_url = base_url
        self.api_url = f"{base_url}/api/v1"
    
    def add_camera(self, name, rtsp_url):
        response = requests.post(
            f"{self.api_url}/cameras/",
            data={
                "name": name,
                "rtsp_url": rtsp_url,
                "verify_stream": True
            }
        )
        return response.json()
    
    def query_camera(self, camera_id, query):
        response = requests.post(
            f"{self.api_url}/ai-vision/query",
            json={
                "camera_id": camera_id,
                "query": query
            }
        )
        return response.json()
    
    async def listen_events(self):
        uri = f"ws://localhost:8000/ws"
        async with websockets.connect(uri) as websocket:
            async for message in websocket:
                event = json.loads(message)
                print(f"Event: {event}")

# Usage example
client = AIBackendClient()
camera = client.add_camera("Main Door", "rtsp://admin:[email protected]/stream")
result = client.query_camera(camera["id"], "How many people are visible?")

🌐 JavaScript Client

class AIBackendClient {
    constructor(baseUrl = 'http://localhost:8000') {
        this.baseUrl = baseUrl;
        this.apiUrl = `${baseUrl}/api/v1`;
    }

    async addCamera(name, rtspUrl) {
        const formData = new FormData();
        formData.append('name', name);
        formData.append('rtsp_url', rtspUrl);
        formData.append('verify_stream', 'true');

        const response = await fetch(`${this.apiUrl}/cameras/`, {
            method: 'POST',
            body: formData
        });
        return await response.json();
    }

    async queryCamera(cameraId, query) {
        const response = await fetch(`${this.apiUrl}/ai-vision/query`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                camera_id: cameraId,
                query: query
            })
        });
        return await response.json();
    }

    connectWebSocket() {
        const ws = new WebSocket('ws://localhost:8000/ws');
        
        ws.onmessage = (event) => {
            const data = JSON.parse(event.data);
            console.log('Event:', data);
        };
        
        return ws;
    }
}

// Usage example
const client = new AIBackendClient();
const camera = await client.addCamera('Main Door', 'rtsp://admin:[email protected]/stream');
const result = await client.queryCamera(camera.id, 'How many people are visible?');

🎛️ Configuration Examples

🏢 Enterprise Deployment

# docker-compose.yml
version: '3.8'

services:
  ai-backend:
    build: .
    ports:
      - "8000:8000"
    environment:
      - ENABLE_GPU=true
      - AI_ENGINE_MONITOR_INTERVAL_SECONDS=15
      - SECRET_KEY=${SECRET_KEY}
    volumes:
      - ./data:/app/data
      - ./logs:/app/logs
    restart: unless-stopped
    
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./ssl:/etc/nginx/ssl
    depends_on:
      - ai-backend
    restart: unless-stopped

  redis:
    image: redis:alpine
    ports:
      - "6379:6379"
    restart: unless-stopped

🔧 Production Settings

# config.py for production
class ProductionSettings(Settings):
    ENABLE_GPU: bool = True
    AI_ENGINE_MONITOR_INTERVAL_SECONDS: int = 15
    CORS_ORIGINS: List[str] = [
        "https://yourdomain.com",
        "https://dashboard.yourdomain.com"
    ]
    MAX_UPLOAD_SIZE: int = 50 * 1024 * 1024  # 50 MB
    
    # Redis for caching
    REDIS_URL: str = "redis://localhost:6379"
    
    # Database
    DATABASE_URL: str = "postgresql://user:pass@localhost/ai_backend"
    
    # Monitoring
    SENTRY_DSN: str = "https://your-sentry-dsn"
    ENABLE_METRICS: bool = True

🚀 Deployment Guide

☁️ Cloud Deployment

AWS EC2 Deployment:

# Launch GPU-enabled instance
aws ec2 run-instances \
  --image-id ami-0abcdef1234567890 \
  --instance-type p3.2xlarge \
  --key-name your-key-pair \
  --security-groups ai-backend-sg

# Setup instance
ssh -i your-key.pem ubuntu@your-instance-ip
sudo apt update && sudo apt install -y docker.io nvidia-docker2
git clone https://github.com/rudra-sah00/A-AI-BACKEND.git
cd A-AI-BACKEND
docker-compose up -d

Google Cloud Platform:

# Create VM with GPU
gcloud compute instances create ai-backend-instance \
  --zone=us-west1-b \
  --machine-type=n1-standard-4 \
  --accelerator=type=nvidia-tesla-t4,count=1 \
  --image-family=ubuntu-2004-lts \
  --image-project=ubuntu-os-cloud \
  --boot-disk-size=100GB

# Deploy application
gcloud compute ssh ai-backend-instance
# Follow installation steps...

🐳 Kubernetes Deployment

# k8s-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ai-backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: ai-backend
  template:
    metadata:
      labels:
        app: ai-backend
    spec:
      containers:
      - name: ai-backend
        image: ai-backend:latest
        ports:
        - containerPort: 8000
        env:
        - name: ENABLE_GPU
          value: "true"
        resources:
          limits:
            nvidia.com/gpu: 1
          requests:
            memory: "2Gi"
            cpu: "1000m"
        volumeMounts:
        - name: data-volume
          mountPath: /app/data
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: ai-backend-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: ai-backend-service
spec:
  selector:
    app: ai-backend
  ports:
  - port: 8000
    targetPort: 8000
  type: LoadBalancer

📈 Scaling & Optimization

Performance Optimization

GPU Acceleration:

# Enable GPU for YOLO
device = "cuda" if torch.cuda.is_available() else "cpu"
model = YOLO("yolov8n.pt")
model.to(device)

Model Quantization:

# Quantize model for faster inference
import torch.quantization as quantization

model_quantized = quantization.quantize_dynamic(
    model, {torch.nn.Linear}, dtype=torch.qint8
)

Caching Strategy:

# Redis caching for face encodings
import redis

redis_client = redis.Redis(host='localhost', port=6379, db=0)

def cache_face_encoding(user_id, encoding):
    redis_client.setex(
        f"face_encoding:{user_id}", 
        3600, 
        encoding.tobytes()
    )

📊 Load Balancing

# nginx.conf
upstream ai_backend {
    server ai-backend-1:8000 weight=3;
    server ai-backend-2:8000 weight=2;
    server ai-backend-3:8000 weight=1;
}

server {
    listen 80;
    location / {
        proxy_pass http://ai_backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

🐛 Troubleshooting

Common Issues

1. CUDA Out of Memory

# Solution: Reduce batch size or model size
export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512

2. RTSP Stream Connection Failed

# Check stream with OpenCV
cap = cv2.VideoCapture(rtsp_url)
if not cap.isOpened():
    print("Stream validation failed")
    # Try different transport protocols
    cap = cv2.VideoCapture(rtsp_url + "?rtsp_transport=tcp")

3. Face Recognition Low Accuracy

# Increase image quality and lighting
enhanced_img = cv2.convertScaleAbs(img, alpha=1.3, beta=30)
face_locations = face_recognition.face_locations(enhanced_img, model="cnn")

🔧 Debug Commands

# Check GPU availability
python -c "import torch; print(torch.cuda.is_available())"

# Test YOLO model
python -c "from ultralytics import YOLO; model = YOLO('yolov8n.pt'); print('YOLO loaded successfully')"

# Validate dependencies
python -c "import cv2, face_recognition, insightface; print('All dependencies loaded')"

# Check API health
curl http://localhost:8000/api/v1/cameras/

# Test WebSocket connection
websocat ws://localhost:8000/ws

🤝 Contributing

🔄 Development Workflow

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📝 Contribution Guidelines

  • Code Style: Follow PEP 8 and use Black formatter
  • Testing: Add tests for new features
  • Documentation: Update README and docstrings
  • Performance: Consider performance impact of changes

🏆 Contributors


📄 License

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


🙏 Acknowledgments

  • Andhra Pradesh Government for the hackathon opportunity
  • Ultralytics for the excellent YOLO implementation
  • Google for the Gemini Vision API
  • OpenCV community for computer vision tools
  • FastAPI team for the amazing web framework

📞 Support & Contact

🆘 Getting Help

📱 Social Media


🌟 Star this repository if it helped you!

Made with ❤️ for the Andhra Pradesh Government Hackathon

⬆ Back to Top

About

🤖 AI-Powered Security & Analytics Backend for AP Government Hackathon | FastAPI + YOLO v8 + Face Recognition + Gemini Vision | Real-time camera monitoring, person detection, gunny bag counting & intelligent security system with RTSP stream processing

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages