# niraj1742/anubhavai architecture

> Architecture diagram of the GitHub repository niraj1742/anubhavai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/niraj1742/anubhavai
- Repository: https://github.com/niraj1742/anubhavai

## Overview

##### Main Architectural Layers
- **Presentation Layer (Frontend)**
- Streamlit Web UI (main.py)
- Browser client
- **Application/Orchestration Layer (Controller)**
- main.py + utils.py
- Receives image uploads, calls each analysis module, aggregates results
- **Processing Modules (Backend Services)**
1. Face Detection Engine (OpenCV + Haar Cascades)
2. Emotion Recognition Engine (PyTorch ResNet50 / DeepFace)
3. Background Removal Engine (U-2-Net model)
4. Scene Classification Engine (Places365 models)
- **Data & Model Artifacts**
- models/U-2-Net (weights, code, test data)
- models/places365 (prototxt, weights, Docker support)
- Saved Haar cascade XML under models/U-2-Net/saved_models
- **Output**
- JSON analysis report

##### Relationships and Data Flow
- User uploads an image via the Streamlit UI
- Streamlit Server (main.py) passes the image to the Controller (utils.py)
- Controller invokes each module in sequence or parallel:
b. **Emotion Recognition** (on each crop) → emotion labels + confidence
c. **Background Removal** → segmentation mask + transparent PNG
d. **Scene Classification** → scene category + confidence
- Controller collects module outputs into a unified JSON object
- Streamlit renders results: annotated image, masks, bar charts, report

##### Architectural Patterns & Design Principles
- **Modular Pipeline**: Each analysis task is encapsulated in its own Python module, making it easy to replace or extend
- **Separation of Concerns**: UI (Streamlit), orchestration (utils/main), and computation (models/) are clearly divided
- **Plug-and-Play Models**: weights and inference code live under models/, enabling model upgrades without touching orchestration
- **Synchronous vs. Asynchronous**: Although currently sequential, you could parallelize module calls if needed

##### Technologies & Frameworks
- Frontend: Python Streamlit
- Face Detection: OpenCV (cv2), Haar Cascades
- Emotion Recognition: PyTorch, TorchVision, DeepFace
- Background Removal: rembg wrapper around U-2-Net
- Scene Classification: Places365 models (Caffe prototxt, PyTorch conversion)
- Core Language: Python 3.x
- Extras: NumPy, Pillow, scikit-learn, TensorFlow (DeepFace), Docker (only for Places365 demos)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Presentation Layer
    subgraph "Presentation Layer"
        BrowserClient["Browser Client"]:::frontend
        StreamlitApp["Streamlit Web App\n(port 8501)"]:::frontend
        Config[".streamlit/config.toml"]:::frontend
    end

    %% Controller Layer
    subgraph "Controller / Orchestration Layer"
        Controller["Controller / Orchestrator\n(main.py + utils.py)"]:::orchestration
    end

    %% Processing Modules
    subgraph "Processing Modules"
        FaceDetection["Face Detection Engine"]:::service
        EmotionRecognition["Emotion Recognition Engine"]:::service
        BackgroundRemoval["Background Removal Engine"]:::service
        SceneClassification["Scene Classification Engine"]:::service
    end

    %% Data & Model Artifacts
    subgraph "Data & Model Artifacts"
        U2NetArtifacts["models/U-2-Net"]:::artifact
        Places365Artifacts["models/places365"]:::artifact
    end

    %% Result Renderer
    ResultRenderer["Result Renderer\n(Annotated UI + JSON Report)"]:::frontend

    %% Data Flow
    BrowserClient -->|"HTTP Request"| StreamlitApp
    StreamlitApp -->|"call(main.py)"| Controller
    Config --> StreamlitApp

    Controller -->|"Image"| FaceDetection
    Controller -->|"Face Crops"| EmotionRecognition
    Controller -->|"Image"| BackgroundRemoval
    Controller -->|"Image"| SceneClassification

    FaceDetection -->|"haarcascade xml"| U2NetArtifacts
    EmotionRecognition -->|"model weights"| Places365Artifacts
    BackgroundRemoval -->|"u2net code/weights"| U2NetArtifacts
    SceneClassification -->|"prototxt/weights"| Places365Artifacts

    FaceDetection -->|"bounding boxes JSON"| Controller
    EmotionRecognition -->|"emotion labels JSON"| Controller
    BackgroundRemoval -->|"mask PNG/json"| Controller
    SceneClassification -->|"scene label JSON"| Controller

    Controller -->|"Aggregated Results"| ResultRenderer
    ResultRenderer -->|"render UI"| StreamlitApp

    %% Click Events
    click StreamlitApp "https://github.com/niraj1742/anubhavai/blob/main/main.py"
    click Config "https://github.com/niraj1742/anubhavai/blob/main/.streamlit/config.toml"
    click Controller "https://github.com/niraj1742/anubhavai/blob/main/utils.py"
    click FaceDetection "https://github.com/niraj1742/anubhavai/blob/main/models/U-2-Net/saved_models/face_detection_cv2/haarcascade_frontalface_default.xml"
    click BackgroundRemoval "https://github.com/niraj1742/anubhavai/blob/main/models/U-2-Net/model/u2net.py"
    click BackgroundRemoval "https://github.com/niraj1742/anubhavai/blob/main/models/U-2-Net/model/u2net_refactor.py"
    click BackgroundRemoval "https://github.com/niraj1742/anubhavai/blob/main/models/U-2-Net/setup_model_weights.py"
    click BackgroundRemoval "https://github.com/niraj1742/anubhavai/blob/main/models/U-2-Net/u2net_portrait_demo.py"
    click BackgroundRemoval "https://github.com/niraj1742/anubhavai/blob/main/models/U-2-Net/u2net_human_seg_test.py"
    click SceneClassification "https://github.com/niraj1742/anubhavai/blob/main/models/places365/deploy_resnet152_places365.prototxt"
    click SceneClassification "https://github.com/niraj1742/anubhavai/blob/main/models/places365/run_placesCNN_basic.py"
    click SceneClassification "https://github.com/niraj1742/anubhavai/blob/main/models/places365/run_placesCNN_unified.py"
    click SceneClassification "https://github.com/niraj1742/anubhavai/blob/main/models/places365/convert_model.py"
    click SceneClassification "https://github.com/niraj1742/anubhavai/tree/main/models/places365/docker/Dockerfile"
    click U2NetArtifacts "https://github.com/niraj1742/anubhavai/tree/main/models/U-2-Net"
    click Places365Artifacts "https://github.com/niraj1742/anubhavai/tree/main/models/places365"

    %% Styles
    classDef frontend fill:#D0E6FA,stroke:#1B4F72,color:#1B2631;
    classDef orchestration fill:#D5F5E3,stroke:#196F3D,color:#145A32;
    classDef service fill:#FDEBD0,stroke:#D35400,color:#A04000;
    classDef artifact fill:#E5E8E8,stroke:#566573,color:#1C2833;
```
