# MorphGuard: Comprehensive Documentation & Usage Guide ## 🛡️ Table of Contents 1. [System Overview](#system-overview) 2. [Architecture Deep Dive](#architecture-deep-dive) 3. [User Workflows](#user-workflows) 4. [API Reference](#api-reference) 5. [Deployment Guide](#deployment-guide) 6. [Use Cases & Applications](#use-cases--applications) 7. [Configuration Guide](#configuration-guide) 8. [Monitoring & Observability](#monitoring--observability) 9. [Security Features](#security-features) 10. [Development Guide](#development-guide) --- ## 🏗️ System Overview MorphGuard is an **enterprise-grade AI-powered face authentication system** designed to detect morphed faces, verify identity, and provide comprehensive face analysis with blockchain-backed verification. The system combines cutting-edge machine learning, real-time processing, and enterprise security. ### Core Capabilities - **Face Morph Detection**: Multi-model ensemble detection with 95%+ accuracy - **Identity Verification**: Blockchain-backed face matching and ID verification - **Real-time Demorphing**: Multiple AI methods (Transformers, GANs, Diffusion) - **Liveness Detection**: Advanced anti-spoofing with micro-texture analysis - **Cloud Integration**: AWS Rekognition, Azure Face API, multi-provider failover - **WebRTC Capture**: Real-time face capture with 6D quality assessment --- ## 🎯 Architecture Deep Dive ### System Architecture Overview ``` ┌─────────────────┐ ┌───────────────────┐ ┌─────────────────┐ │ Frontend UI │◄──►│ Flask App API │◄──►│ AI/ML Core │ │ (Enhanced UI) │ │ (app.py) │ │ (morphguard_api)│ └─────────────────┘ └───────────────────┘ └─────────────────┘ │ │ │ │ ▼ ▼ ┌─────────────────┐ ┌───────────────────┐ ┌─────────────────┐ │ WebRTC │ │ TimescaleDB │ │ Model Store │ │ Capture │ │ (Metrics) │ │ (Weights) │ └─────────────────┘ └───────────────────┘ └─────────────────┘ │ │ │ │ ▼ ▼ ┌─────────────────┐ ┌───────────────────┐ ┌─────────────────┐ │ Blockchain │ │ Cloud APIs │ │ Plugins │ │ (Ethereum) │ │ (AWS/Azure) │ │ (DroneKit) │ └─────────────────┘ └───────────────────┘ └─────────────────┘ ``` ### Core Components #### 1. **Flask Application (`app.py`)** ```python # Main application with modular blueprint architecture app = Flask(__name__) app.register_blueprint(advanced_face_bp) # WebRTC face capture app.register_blueprint(plugin_api_bp) # Plugin system app.register_blueprint(notification_api_bp) # Real-time notifications # Key features: - Session-based authentication with role management - Circuit breaker pattern for API resilience - Real-time metrics collection with TimescaleDB - Blockchain integration for verification logging ``` #### 2. **AI/ML Core (`morphguard_api.py`)** ```python class MorphGuardAPI: """Primary AI engine with multi-model ensemble""" # Core detection models self.detection_models = [ 'vit_base_patch16_224', # Vision Transformer 'swin_base_patch4_window7', # Swin Transformer 'efficientnet_b4', # EfficientNet ] # Demorphing methods self.demorphing_methods = { 'transformer': TransformerDemorpher, 'gan': pSpDemorpher, 'diffusion': StableDiffusionDemorpher, 'latent': LatentDiffusionDemorpher } ``` #### 3. **WebRTC Advanced Capture (`src/webrtc/`)** ```python class AdvancedFaceCapture: """Real-time face capture with 6D quality assessment""" # Quality dimensions assessed: - Sharpness (Laplacian variance) - Illumination (brightness + uniformity) - Pose (yaw/pitch/roll angles) - Face size (eye distance validation) - Motion blur (gradient magnitude) - Overall composite score ``` --- ## 🔄 User Workflows ### Workflow 1: Basic Face Morph Detection ```mermaid graph TD A[Upload Image] --> B[Preprocessing] B --> C[Face Detection] C --> D[Model Inference] D --> E[Confidence Scoring] E --> F[Result + Heatmap] F --> G[Store Metrics] ``` **Step-by-step Process:** 1. **Upload**: User uploads image via web interface or API 2. **Validation**: System validates file format, size, content 3. **Face Detection**: MTCNN/OpenCV extracts face regions 4. **Model Ensemble**: Multiple models process the face 5. **Confidence Calibration**: Temperature scaling for reliable scores 6. **Visualization**: Gradient-based saliency heatmap generation 7. **Storage**: Results logged to TimescaleDB for analytics ### Workflow 2: Identity Verification with Blockchain ```mermaid graph TD A[Selfie Capture] --> B[Government ID Upload] B --> C[Face Extraction] C --> D[Liveness Detection] D --> E[Face Matching] E --> F[MFA Challenge] F --> G[Blockchain Logging] G --> H[Verification Certificate] ``` **Enterprise Use Case:** 1. **Enrollment**: User captures selfie + uploads government ID 2. **Liveness Check**: Anti-spoofing validation using micro-texture analysis 3. **Face Matching**: Biometric comparison between selfie and ID photo 4. **Multi-factor Auth**: SMS/email verification for additional security 5. **Blockchain Record**: Immutable verification log on Ethereum 6. **Certificate**: Cryptographically signed verification certificate ### Workflow 3: Real-time WebRTC Capture ```mermaid graph TD A[Camera Access] --> B[Real-time Stream] B --> C[Frame Processing] C --> D[Quality Assessment] D --> E{Quality > Threshold} E -->|Yes| F[Capture Frame] E -->|No| G[User Guidance] G --> C F --> H[Process & Store] ``` **Real-time Processing:** 1. **Camera Initialization**: WebRTC stream with optimal constraints 2. **Live Processing**: 30 FPS real-time face detection and quality scoring 3. **User Guidance**: Real-time feedback for optimal positioning 4. **Smart Capture**: Automatic capture when quality thresholds met 5. **Background Processing**: Asynchronous AI processing pipeline --- ## 📡 API Reference ### Core Detection APIs #### `POST /api/detect` **Face Morph Detection with Confidence Scoring** ```bash curl -X POST http://localhost:5000/api/detect \ -F "image=@face.jpg" \ -F "method=ensemble" \ -F "return_heatmap=true" ``` **Response:** ```json { "is_morphed": false, "confidence": 0.923, "morph_score": 0.156, "processing_time": 245.6, "models_used": ["vit_base", "swin_base", "efficientnet_b4"], "ensemble_scores": { "vit_base": 0.134, "swin_base": 0.178, "efficientnet_b4": 0.156 }, "heatmap_url": "/static/heatmaps/12345.png", "metadata": { "face_detected": true, "face_quality": 0.887, "resolution": [512, 512] } } ``` #### `POST /api/demorph` **Multi-method Face Demorphing** ```bash curl -X POST http://localhost:5000/api/demorph \ -F "image=@morphed_face.jpg" \ -F "method=transformer" \ -F "strength=0.8" ``` **Available Methods:** - `transformer`: Vision Transformer-based (fastest, balanced quality) - `gan`: pSp GAN-based (highest quality, slower) - `diffusion`: Stable Diffusion (creative, artistic) - `latent`: Latent Diffusion (text-guided demorphing) #### `POST /api/verify_identity` **Blockchain-backed Identity Verification** ```bash curl -X POST http://localhost:5000/api/verify_identity \ -F "selfie=@selfie.jpg" \ -F "id_document=@passport.jpg" \ -F "liveness_video=@liveness.mp4" ``` ### Advanced WebRTC APIs #### `GET /api/advanced-capture` **Real-time WebRTC Face Capture Interface** - Returns enhanced HTML interface with real-time processing - Includes quality metrics dashboard and AI recommendations #### `POST /api/store-face-metrics` **Real-time Metrics Storage** ```json { "sessionId": "session_123", "frameNumber": 45, "qualityMetrics": { "sharpnessScore": 0.89, "illuminationScore": 0.92, "poseScore": 0.78, "overallScore": 0.86 }, "poseAngles": {"yaw": 2.3, "pitch": -1.7, "roll": 0.8}, "isFrontal": true } ``` ### Management APIs #### `POST /api/train/detector` **Model Retraining with Custom Data** ```bash curl -X POST http://localhost:5000/api/train/detector \ -H "Authorization: Bearer admin_token" \ -F "training_data=@dataset.zip" \ -F "epochs=50" \ -F "learning_rate=0.0001" ``` #### `GET /api/system/health` **System Health and Performance Metrics** ```json { "status": "healthy", "uptime": 3600, "models_loaded": 4, "gpu_utilization": 45.2, "memory_usage": 2.8, "database_status": "connected", "blockchain_status": "synced" } ``` --- ## 🚀 Deployment Guide ### Option 1: Docker Deployment (Recommended) ```bash # Clone repository git clone https://github.com/your-org/morphguard.git cd morphguard # Build and run with Docker Compose docker-compose up -d # Services started: # - MorphGuard API: http://localhost:5000 # - TimescaleDB: localhost:5432 # - Grafana Dashboard: http://localhost:3000 # - Ethereum Node: localhost:8545 ``` ### Option 2: Manual Setup ```bash # Install dependencies ./setup.sh # Configure environment cp .env.example .env # Edit .env with your settings # Initialize database python -c "from database_init import init_db; init_db()" # Start application python app.py ``` ### Production Configuration ```bash # Set production environment variables export MORPHGUARD_SECRET_KEY="your-secret-key" export DB_HOST="your-timescale-host" export ETH_ENDPOINT="https://mainnet.infura.io/v3/your-key" export GRAFANA_URL="https://your-grafana.com" # Enable production features export FLASK_ENV=production export USE_BLOCKCHAIN=true export ENABLE_MONITORING=true ``` --- ## 🎯 Use Cases & Applications ### 1. **Government & Immigration** **Border Control & Passport Verification** ```python # Automated passport verification at border crossings workflow = { "capture": "Document scanner + live selfie capture", "processing": "Face matching + morph detection + liveness", "verification": "Government database lookup + blockchain logging", "decision": "Automated approve/flag for manual review" } # Benefits: - 95%+ accuracy in morph detection - Sub-3-second processing time - Tamper-evident blockchain audit trail - Real-time threat intelligence integration ``` ### 2. **Financial Services** **Digital Banking & KYC Compliance** ```python # Account opening with enhanced verification features = { "onboarding": "Mobile selfie + ID document verification", "ongoing": "Periodic re-verification with face matching", "fraud_detection": "Real-time morph detection during transactions", "compliance": "Automated KYC/AML reporting with audit trails" } # Compliance Benefits: - GDPR compliant data handling - SOX audit trail with blockchain backing - Real-time fraud prevention - Reduced manual review costs by 80% ``` ### 3. **Healthcare** **Patient Identity Management** ```python # Secure patient identification system implementation = { "registration": "Biometric enrollment with medical ID", "access_control": "Secure access to medical records", "prescription_safety": "Prevent identity fraud in medication", "telemedicine": "Verify patient identity in remote consultations" } # Security Features: - HIPAA compliant architecture - End-to-end encryption - Audit logging for compliance - Integration with EMR systems ``` ### 4. **Enterprise Security** **Employee Access Control** ```python # Zero-trust office access system system = { "enrollment": "Employee biometric registration", "daily_access": "Touchless entry with liveness detection", "secure_areas": "Multi-factor biometric verification", "visitor_management": "Temporary access with ID verification" } # Enterprise Benefits: - Eliminate badge cloning/sharing - Real-time security monitoring - Integration with Active Directory - Detailed access analytics ``` ### 5. **Education** **Exam Integrity & Student Verification** ```python # Online examination security proctoring = { "pre_exam": "Student identity verification with ID check", "during_exam": "Continuous liveness monitoring", "behavior_analysis": "Detect impersonation attempts", "integrity_report": "Comprehensive exam security report" } # Academic Benefits: - Prevent exam impersonation - Remote learning security - Automated proctoring at scale - Detailed integrity reporting ``` --- ## ⚙️ Configuration Guide ### Environment Variables ```bash # Core Application MORPHGUARD_SECRET_KEY=your-256-bit-secret-key FLASK_ENV=production DEBUG=false # Database Configuration DB_HOST=localhost DB_PORT=5432 DB_NAME=morphguard DB_USER=morphguard DB_PASS=secure-password # AI Model Configuration USE_QUANTIZED=true DETECTOR_TEMP=1.2 GAN_CHECKPOINT_PATH=/models/psp_ffhq_inversion.pt DIFFUSION_MODEL_PATH=/models/stable-diffusion-v1-4 # Blockchain Configuration ETH_ENDPOINT=https://mainnet.infura.io/v3/your-project-id ETH_WALLET_ADDRESS=0xYourWalletAddress ETH_PRIVATE_KEY=your-private-key # Cloud Provider APIs AWS_ACCESS_KEY_ID=your-aws-key AWS_SECRET_ACCESS_KEY=your-aws-secret AWS_REGION=us-east-1 AZURE_FACE_API_KEY=your-azure-key AZURE_FACE_ENDPOINT=https://your-region.api.cognitive.microsoft.com # Monitoring & Observability GRAFANA_URL=http://localhost:3000 ENABLE_TELEMETRY=true LOG_LEVEL=INFO ``` ### Model Configuration (`models_config.py`) ```python # Detection Models (ordered by priority) DETECTION_MODELS = [ "vit_base_patch16_224", # Primary: Best balance of speed/accuracy "swin_base_patch4_window7", # Secondary: High accuracy for difficult cases "efficientnet_b4", # Fallback: Fast inference for edge deployment ] # Demorphing Configuration DEMORPHER_CONFIG = { "transformer": { "model_path": "models/demorpher/transformer.ckpt", "precision": "fp16", "batch_size": 4 }, "gan": { "checkpoint": "models/demorpher/psp_ffhq_inversion.pt", "resolution": 1024, "latent_avg": "models/demorpher/latent_avg.pt" } } ``` ### WebRTC Capture Settings ```javascript // Advanced camera constraints for optimal quality const captureConfig = { video: { width: { ideal: 1280 }, height: { ideal: 720 }, frameRate: { ideal: 30 }, facingMode: 'user', focusMode: { ideal: 'continuous' }, exposureMode: { ideal: 'continuous' }, whiteBalanceMode: { ideal: 'continuous' } }, qualityThresholds: { minimum: 0.7, // Minimum quality for processing optimal: 0.85, // Target quality threshold sharpness: 0.6, // Minimum sharpness score illumination: 0.65, // Minimum illumination score pose: 0.8 // Maximum pose deviation } }; ``` --- ## 📊 Monitoring & Observability ### TimescaleDB Metrics Schema ```sql -- Real-time performance metrics CREATE TABLE device_metrics ( timestamp TIMESTAMPTZ NOT NULL, cpu_usage REAL, memory_usage REAL, disk_usage REAL, network_io JSONB ); -- AI model performance tracking CREATE TABLE detection_metrics ( timestamp TIMESTAMPTZ NOT NULL, model_name TEXT, inference_time_ms REAL, confidence_score REAL, accuracy_score REAL, throughput_fps REAL ); -- WebRTC capture quality metrics CREATE TABLE face_quality_metrics ( timestamp TIMESTAMPTZ NOT NULL, session_id TEXT, sharpness_score REAL, illumination_score REAL, pose_score REAL, overall_score REAL, processing_time_ms REAL ); ``` ### Grafana Dashboard Features ```yaml # Key Performance Indicators (KPIs) dashboards: - system_health: panels: - CPU/Memory utilization - GPU temperature and usage - Request latency (p50, p95, p99) - Error rates by endpoint - ai_model_performance: panels: - Model inference times - Confidence score distributions - Accuracy trends over time - Throughput metrics - security_monitoring: panels: - Failed authentication attempts - Suspicious activity patterns - Blockchain transaction status - Fraud detection alerts # Alerting Rules alerts: - high_cpu_usage: "CPU > 80% for 5 minutes" - model_drift: "Accuracy drops below 90%" - security_breach: "Failed logins > 10 in 1 minute" - blockchain_failure: "Transaction fails for 3 attempts" ``` ### Real-time Notifications ```python # WebSocket-based real-time alerts class NotificationSystem: def __init__(self): self.alerts = { 'performance': self.handle_performance_alert, 'security': self.handle_security_alert, 'model_drift': self.handle_model_drift, 'system_health': self.handle_health_alert } async def broadcast_alert(self, alert_type, data): """Send real-time alerts to connected clients""" message = { 'type': alert_type, 'timestamp': datetime.now().isoformat(), 'data': data, 'severity': self.calculate_severity(alert_type, data) } await self.websocket_manager.broadcast(message) ``` --- ## 🔒 Security Features ### Authentication & Authorization ```python # Role-based access control (RBAC) roles = { 'admin': { 'permissions': ['*'], # Full system access 'can_manage_users': True, 'can_train_models': True, 'can_access_blockchain': True }, 'moderator': { 'permissions': ['detect', 'demorph', 'verify'], 'can_manage_users': False, 'can_train_models': True, 'can_access_blockchain': False }, 'user': { 'permissions': ['detect', 'demorph'], 'can_manage_users': False, 'can_train_models': False, 'can_access_blockchain': False } } ``` ### Data Protection ```python # Encryption and data handling security_measures = { 'encryption_at_rest': 'AES-256-GCM', 'encryption_in_transit': 'TLS 1.3', 'key_management': 'Hardware Security Modules (HSM)', 'data_retention': 'Configurable with automatic purging', 'privacy_compliance': 'GDPR, CCPA, HIPAA ready', 'audit_logging': 'Immutable blockchain-backed logs' } # Zero-trust architecture class SecurityHandler: def __init__(self): self.require_authentication = True self.require_authorization = True self.validate_input = True self.encrypt_sensitive_data = True self.log_all_access = True ``` ### Blockchain Verification ```solidity // Smart contract for tamper-evident verification logs contract MorphGuardVerification { struct VerificationRecord { address verifier; bytes32 dataHash; uint256 timestamp; uint8 confidenceLevel; bool isValid; } mapping(bytes32 => VerificationRecord) public verifications; function recordVerification( bytes32 _id, bytes32 _dataHash, uint8 _confidence ) external { verifications[_id] = VerificationRecord({ verifier: msg.sender, dataHash: _dataHash, timestamp: block.timestamp, confidenceLevel: _confidence, isValid: true }); } } ``` --- ## 👨‍💻 Development Guide ### Setting Up Development Environment ```bash # Clone and setup git clone https://github.com/your-org/morphguard.git cd morphguard # Create virtual environment python -m venv venv source venv/bin/activate # Linux/Mac # venv\Scripts\activate # Windows # Install dependencies pip install -r requirements.txt pip install -r requirements-dev.txt # Setup pre-commit hooks pre-commit install # Initialize development database python scripts/setup_dev_db.py # Download development models python download_models.py --dev ``` ### Running Tests ```bash # Run full test suite pytest tests/ -v --cov=morphguard # Run specific test categories pytest tests/test_detection.py -v # Detection tests pytest tests/test_api.py -v # API tests pytest tests/test_security.py -v # Security tests pytest tests/test_webrtc.py -v # WebRTC tests # Run integration tests pytest tests/integration/ -v --slow # Generate coverage report pytest --cov=morphguard --cov-report=html ``` ### Code Quality Tools ```bash # Code formatting black morphguard/ isort morphguard/ # Linting flake8 morphguard/ pylint morphguard/ # Type checking mypy morphguard/ # Security scanning bandit -r morphguard/ safety check ``` ### Adding New AI Models ```python # Example: Adding a new detection model class NewDetectionModel(BaseDetectionModel): def __init__(self, model_path): super().__init__() self.model = self.load_model(model_path) def preprocess(self, image): """Preprocess input image for this model""" return transform(image) def predict(self, image): """Generate prediction with confidence score""" processed = self.preprocess(image) logits = self.model(processed) confidence = torch.softmax(logits, dim=1) return confidence.max().item() def get_heatmap(self, image): """Generate saliency heatmap for explainability""" return grad_cam(self.model, image) # Register the new model DETECTION_MODELS['new_model'] = NewDetectionModel ``` ### Plugin Development ```python # Example: Creating a custom identity verification plugin class CustomIDVerifier(BaseVerificationPlugin): def __init__(self, config): self.api_key = config['api_key'] self.endpoint = config['endpoint'] async def verify_document(self, document_image): """Verify government-issued document""" result = await self.call_external_api(document_image) return { 'is_valid': result['valid'], 'document_type': result['type'], 'extracted_data': result['data'], 'confidence': result['confidence'] } async def match_faces(self, selfie, document_photo): """Compare faces between selfie and document""" similarity = await self.face_comparison_api(selfie, document_photo) return { 'match': similarity > 0.85, 'similarity_score': similarity, 'landmarks_match': True } # Register plugin register_plugin('custom_id_verifier', CustomIDVerifier) ``` --- ## 🎉 Summary MorphGuard represents a **comprehensive, production-ready face authentication platform** that combines: - ✅ **Advanced AI/ML** with ensemble model architecture - ✅ **Real-time WebRTC processing** with 6D quality assessment - ✅ **Blockchain verification** for tamper-evident audit trails - ✅ **Enterprise security** with RBAC and encryption - ✅ **Cloud integration** with multi-provider failover - ✅ **Comprehensive monitoring** with TimescaleDB and Grafana - ✅ **Extensible architecture** with plugin system - ✅ **Production deployment** with Docker and health checks The system is designed for **enterprise-scale deployment** with robust security, comprehensive monitoring, and the flexibility to adapt to various use cases from government border control to financial services KYC compliance. **Next Steps:** 1. Deploy using the provided Docker configuration 2. Configure environment variables for your use case 3. Integrate with your existing identity management systems 4. Set up monitoring dashboards for operational visibility 5. Train custom models on your specific data if needed For support, contribute to the project, or get involved in the community! 🚀