| # NETRA - System Architecture |
|
|
| ## ποΈ Application Architecture |
|
|
| ``` |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| β FRONTEND β |
| β βββββββββββββ ββββββββββββββββ ββββββββββββββββββββββ β |
| β β Login/ β β Dashboard β β Live Camera / β β |
| β β Register β β β β Video Analysis β β |
| β βββββββ¬ββββββ ββββββββ¬ββββββββ βββββββββββ¬βββββββββββ β |
| β β β β β |
| β βββββββββββββββββ΄ββββββββββββββββββββββ β |
| β β β |
| β [HTTP/AJAX] β |
| β β β |
| ββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββ |
| β |
| ββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββ |
| β FLASK BACKEND β |
| β β β |
| β βββββββββββββββββββββββ΄ββββββββββββββββββββββββββ β |
| β β Flask Application (app.py) β β |
| β β ββββββββββββββββββββββββββββββββββββββββββββ β β |
| β β β Route Handlers β β β |
| β β β β’ /register β’ /login β’ /dashboard β β β |
| β β β β’ /live-camera β’ /video-analysis β β β |
| β β β β’ /camera_feed β’ /upload_video β β β |
| β β ββββββββββββββββββββββββββββββββββββββββββββ β β |
| β βββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββ β |
| β β β β |
| β ββββββββββββββ΄βββββββββ βββββ΄βββββββββββββββ β |
| β β Authentication β β Video Processor β β |
| β β β’ User Login β β β’ Frame Process β β |
| β β β’ Session Mgmt β β β’ Model Apply β β |
| β β β’ Password Hash β β β’ Alert Gen β β |
| β βββββββββββββββββββββββ βββββββββββ¬βββββββββ β |
| β β β |
| β ββββββββββββββββββββββββββββββββββββββ΄βββββββββ β |
| β β Model Manager β β |
| β β ββββββββββββββββ βββββββββββββββββββββββ β β |
| β β β YOLO Object β β Violence Detection β β β |
| β β β Detection β β CNN Model β β β |
| β β ββββββββββββββββ βββββββββββββββββββββββ β β |
| β β β β |
| β β ββββββββββββββββββββββββββββββββββββββββ β β |
| β β β Extensible Model Interface β β β |
| β β β (Add More Models Here) β β β |
| β β ββββββββββββββββββββββββββββββββββββββββ β β |
| β βββββββββββββββββββββββββββββββββββββββββββββββ β |
| ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ |
| β |
| ββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββ |
| β DATA LAYER β |
| β β β |
| β ββββββββββββββββββββββ΄βββββββββββββββββββββββ β |
| β β SQLite Database (netra.db) β β |
| β β ββββββββββββββββ βββββββββββββββββββββββ β |
| β β β Users β β Analysis History ββ β |
| β β β β’ id β β β’ id ββ β |
| β β β β’ username β β β’ user_id ββ β |
| β β β β’ email β β β’ type ββ β |
| β β β β’ password β β β’ detections ββ β |
| β β β β’ created β β β’ alert_count ββ β |
| β β ββββββββββββββββ βββββββββββββββββββββββ β |
| β βββββββββββββββββββββββββββββββββββββββββββ β |
| β β |
| β ββββββββββββββββββββββββββββββββββββββββββββ β |
| β β File Storage (uploads/) β β |
| β β β’ Uploaded video files β β |
| β β β’ Temporary processing files β β |
| β ββββββββββββββββββββββββββββββββββββββββββββ β |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| |
| |
| ## π Data Flow |
| |
| ### User Registration/Login Flow |
| ``` |
|
|
| User Input β Flask Route β Validate Data β Hash Password |
| β Store in DB β Create Session β Redirect to Dashboard |
|
|
| ``` |
| |
| ### Live Camera Flow |
| ``` |
|
|
| Camera β OpenCV Capture β Frame Extract β Model Manager |
| β YOLO Detection β Violence Detection β Annotate Frame |
| β Generate Alerts β Stream to Frontend β Display Results |
|
|
| ``` |
| |
| ### Video Analysis Flow |
| ``` |
|
|
| Upload Video β Save to Uploads/ β Extract Frames |
| β Process Each Frame β Collect Detections β Generate Report |
| β Save to History β Return Results β Display to User |
|
|
| ``` |
| |
| ## π Component Interactions |
| |
| ### Frontend β Backend |
| - **Authentication**: POST JSON β Flask validates β Return success/error |
| - **Video Stream**: GET request β Flask streams MJPEG β Browser displays |
| - **File Upload**: FormData POST β Flask saves β Processes β Returns JSON |
| - **Model List**: GET request β Flask queries ModelManager β Returns JSON |
| |
| ### Backend β Models |
| - **Model Loading**: App start β ModelManager loads all models |
| - **Frame Processing**: Frame β YOLO.detect() β Violence.predict() |
| - **Result Aggregation**: Combine all model outputs β Format alerts |
| |
| ### Backend β Database |
| - **User Operations**: Create, Read, Authenticate |
| - **History Logging**: Store analysis results for each session |
| - **Session Management**: Track logged-in users |
| |
| ## π Technology Stack |
| |
| ``` |
|
|
| βββββββββββββββββββββββββββββββββββββββββββ |
| β Presentation Layer β |
| β β’ HTML5 β’ CSS3 β’ JavaScript (Vanilla) β |
| ββββββββββββββββββββ¬βββββββββββββββββββββββ |
| β |
| ββββββββββββββββββββ΄βββββββββββββββββββββββ |
| β Application Layer β |
| β β’ Flask 3.0 β’ Python 3.8+ β |
| β β’ Flask-SQLAlchemy β’ Werkzeug β |
| ββββββββββββββββββββ¬βββββββββββββββββββββββ |
| β |
| ββββββββββββββββββββ΄βββββββββββββββββββββββ |
| β AI Layer β |
| β β’ YOLOv8 (Ultralytics) β |
| β β’ TensorFlow/Keras β |
| β β’ OpenCV β |
| ββββββββββββββββββββ¬βββββββββββββββββββββββ |
| β |
| ββββββββββββββββββββ΄βββββββββββββββββββββββ |
| β Data Layer β |
| β β’ SQLite β’ File System β |
| βββββββββββββββββββββββββββββββββββββββββββ |
|
|
| ``` |
| |
| ## π― Design Patterns Used |
| |
| 1. **MVC Pattern**: Models (DB), Views (Templates), Controllers (Routes) |
| 2. **Manager Pattern**: ModelManager handles all AI models |
| 3. **Singleton**: Database connection |
| 4. **Factory**: Model creation and loading |
| 5. **Strategy**: Different detection strategies per model |
| |
| ## π Security Architecture |
| |
| ``` |
|
|
| Request β Session Check β Authentication β Authorization |
| β Input Validation β Process β Sanitize Output |
|
|
| ``` |
| |
| - Password hashing (Werkzeug) |
| - Session management (Flask) |
| - File upload validation |
| - Path sanitization |
| |
| ## π Deployment Architecture |
| |
| ``` |
|
|
| Development: |
| Flask Dev Server β SQLite β Local Files |
|
|
| Production (Future): |
| Nginx β Gunicorn β Flask App β PostgreSQL |
| β S3/Cloud Storage |
| β Redis (Caching) |
|
|
| ``` |
| |
| ## π Scalability Considerations |
| |
| Current: Single-threaded with threading support |
| Future paths: |
| - Multi-process workers (Gunicorn) |
| - Background job queue (Celery) |
| - Message broker (RabbitMQ/Redis) |
| - Load balancer |
| - Microservices architecture |
| |
| --- |
| |
| **This architecture supports:** |
| β
Easy model integration |
| β
Horizontal scaling potential |
| β
Maintainable code structure |
| β
Security best practices |
| β
Future enhancements |
| ``` |
|
|