# A-AI-BACKEND **Repository Path**: andrewtestma/A-AI-BACKEND ## Basic Information - **Project Name**: A-AI-BACKEND - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-23 - **Last Updated**: 2026-02-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ๐Ÿš€ AI-Powered Security & Analytics Backend
[![Python](https://img.shields.io/badge/Python-3.8+-blue.svg)](https://python.org) [![FastAPI](https://img.shields.io/badge/FastAPI-0.95+-green.svg)](https://fastapi.tiangolo.com) [![YOLO](https://img.shields.io/badge/YOLO-v8-orange.svg)](https://ultralytics.com) [![OpenCV](https://img.shields.io/badge/OpenCV-4.7+-red.svg)](https://opencv.org) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](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](#-project-overview) - [โœจ Key Features](#-key-features) - [๐Ÿ—๏ธ System Architecture](#๏ธ-system-architecture) - [๐Ÿš€ Quick Start](#-quick-start) - [๐Ÿ’ป Installation](#-installation) - [๐Ÿ“ฑ API Documentation](#-api-documentation) - [๐Ÿ”ง Configuration](#-configuration) - [๐ŸŽฅ Features Deep Dive](#-features-deep-dive) - [๐Ÿ“Š Model Training](#-model-training) - [๐Ÿงช Testing & Validation](#-testing--validation) - [๐Ÿ“ˆ Performance & Monitoring](#-performance--monitoring) - [๐Ÿ› ๏ธ Development Guide](#๏ธ-development-guide) - [๐Ÿ”’ Security & Privacy](#-security--privacy) - [๐Ÿ“„ License](#-license) --- ## ๐ŸŽฏ 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 ```mermaid 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 ``` ### ๐Ÿ”„ **Data Flow Architecture** ```mermaid 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 ``` --- ## ๐Ÿš€ Quick Start ### Prerequisites - Python 3.8+ - CUDA-compatible GPU (optional, for faster inference) - Webcam or IP camera with RTSP support ### 1. Clone & Setup ```bash 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 ```bash # 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 ```bash # Development mode python app/main.py # Production mode uvicorn app.main:app --host 0.0.0.0 --port 8000 ``` ### 4. Access the API - **API Documentation**: http://localhost:8000/docs - **Health Check**: http://localhost:8000/api/v1/cameras/ - **WebSocket**: ws://localhost:8000/ws --- ## ๐Ÿ’ป Installation ### ๐Ÿง **Linux (Ubuntu/Debian)** ```bash # 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** ```bash # Install Homebrew (if not installed) /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # System dependencies brew install python@3.11 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** ```powershell # 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** ```bash # 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 ```http POST /api/v1/cameras/ Content-Type: multipart/form-data name: "Main Entrance" rtsp_url: "rtsp://admin:password@192.168.1.100/stream" verify_stream: true ``` #### List Cameras ```http GET /api/v1/cameras/ ``` #### Update Camera Filters ```http 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 ```http 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 ```http POST /api/v1/users/ Content-Type: multipart/form-data name: "John Doe" role: "employee" image: [image file] ``` #### List Users ```http GET /api/v1/users/ ``` ### ๐Ÿ“Š **Analytics & Reports** #### Get Attendance Records ```http GET /api/v1/analytics/attendance?date=2024-01-15 ``` #### Get Unauthorized Entries ```http 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 ```json { "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:** ```python # 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:** ```python # 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:** ```python # 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:** ```javascript // 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:** ```json { "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:** ```bash # 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:** ```json { "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:** ```bash # Prepare training data python prepare_training_data.py \ --input_dir "raw_images/" \ --output_dir "data/gunny_bag_dataset/" \ --train_split 0.8 ``` #### **Training Configuration:** ```yaml # 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:** ```bash # 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 | |--------|-------|-------------| | **mAP@0.5** | 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** ```bash # 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** ```bash # 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** ```bash # 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:** ```python # 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:** ```bash # Enable debug logging export LOG_LEVEL=DEBUG python app/main.py ``` --- ## ๐Ÿ› ๏ธ Development Guide ### ๐Ÿ”ง **Development Setup** ```bash # 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** ```yaml # .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 # 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** ```python # 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** ```python 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:pass@192.168.1.100/stream") result = client.query_camera(camera["id"], "How many people are visible?") ``` ### ๐ŸŒ **JavaScript Client** ```javascript 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:pass@192.168.1.100/stream'); const result = await client.queryCamera(camera.id, 'How many people are visible?'); ``` --- ## ๐ŸŽ›๏ธ Configuration Examples ### ๐Ÿข **Enterprise Deployment** ```yaml # 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** ```python # 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:** ```bash # 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:** ```bash # 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** ```yaml # 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:** ```python # Enable GPU for YOLO device = "cuda" if torch.cuda.is_available() else "cpu" model = YOLO("yolov8n.pt") model.to(device) ``` #### **Model Quantization:** ```python # Quantize model for faster inference import torch.quantization as quantization model_quantized = quantization.quantize_dynamic( model, {torch.nn.Linear}, dtype=torch.qint8 ) ``` #### **Caching Strategy:** ```python # 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 # 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** ```bash # Solution: Reduce batch size or model size export PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512 ``` #### **2. RTSP Stream Connection Failed** ```python # 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** ```python # 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** ```bash # 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** - **Rudra Sah** - *Initial work* - [@rudra-sah00](https://github.com/rudra-sah00) --- ## ๐Ÿ“„ License This project is licensed under the **MIT License** - see the [LICENSE](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** - **Issues**: [GitHub Issues](https://github.com/rudra-sah00/A-AI-BACKEND/issues) - **Discussions**: [GitHub Discussions](https://github.com/rudra-sah00/A-AI-BACKEND/discussions) - **Email**: rudra.sah00@example.com ### ๐Ÿ“ฑ **Social Media** - **LinkedIn**: [Rudra Sah](https://linkedin.com/in/rudra-sah) - **Twitter**: [@rudra_sah00](https://twitter.com/rudra_sah00) ---
### ๐ŸŒŸ Star this repository if it helped you! **Made with โค๏ธ for the Andhra Pradesh Government Hackathon** [โฌ† Back to Top](#-ai-powered-security--analytics-backend)