# foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app- architecture

> Architecture diagram of the GitHub repository foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-
- Repository: https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-

## Overview

##### Main Components
- Mobile Android App:
- User Interface and Activities: Contains MainActivity, CameraActivity, and screens for registration, face listing (RecyclerViewAdaptor ~ ShowRegisteredFaces), and recognition.
- Face Processing Modules: Contains implementations of MobileFaceNet (in mobilefacenet package) and MTCNN (in mtcnn package which has alignment, face-detection helper classes).
- Local Storage/Asset Management: Uses assets (preconverted model files like MobileFaceNet.tflite, onet.tflite, pnet.tflite, and MainMobileFaceNet.tflite) to perform inference.
- Utility Code: Contains helper classes (Helper.java, MyUtil.java) that support face capture, cropping, and matching (using euclidean distance).

- Training and Model Preparation Pipeline (Python/OpenCV):
- Training Scripts: train_v2.py conducts the training process based on images located in the openCV/Faces directory.
- Face Detection & Recognition Script: detect.py, which leverages the trained model to perform real-time or batch recognition.
- Model Conversion: Notebooks and scripts (toTFLite.ipynb, architecture.py) are used for converting the pretrained Facenet_keras model into MobileFaceNet.tflite.
- Data & Encodings: Contains training images (organized in sub-folders under openCV/Faces) and precomputed encodings (encodings.pkl).

- External Components:
- User Devices: Android phones where FaceReg.apk is installed.
- Data Preparation: The user manually adds images (for registration) or takes photos using the app camera.
- (Potentially) External Data Sources: Downloaded model weights (facenet_keras_weights.h5, and TFRecords for training) referenced in the README.

##### Relationships and Interactions
- The Python/OpenCV training pipeline takes labeled images from the openCV/Faces directory and uses train_v2.py to generate the face recognition model. The output might be used directly or converted into the TFLite format.
- The converted MobileFaceNet.tflite file is stored in the Android app’s assets folder (app/src/main/assets) so that the mobile app can load it for face detection and recognition.
- The Android app processes user inputs (face registration, face recognition commands) via activities on the device. It uses the mobilefacenet and mtcnn modules to first detect the face, then crop and compare the resulting face embeddings using a Euclidean distance metric.
- The user flow includes registration (adding a new face image), confirmation (via RecyclerView showing registered faces), and recognition (face matching) processes that are all handled locally on the device.

##### Architectural Patterns/Design Principles
- Separation of Concerns: The system cleanly separates the face recognition training pipeline (Python/OpenCV) from the mobile client using a pre-trained/converted model.
- Modular Design: The Android app is organized into distinct modules (UI, image processing, utilities, and model integration) which aid maintainability.
- Asset Management: Use of assets for storing model files isolates the inference engine from the rest of the application logic.
- Data Flow: User images are captured, processed (face detection and cropping), and then compared to locally stored face embeddings.

##### Relevant Technologies, Frameworks, and Libraries
- Android Framework: For building the mobile application (activities, layouts, resources).
- TensorFlow Lite: For running the MobileFaceNet model on mobile devices.
- OpenCV and Python: For training the face recognition models and handling data preprocessing.
- Java-based libraries: Supporting UI elements like RecyclerView and other utility classes.
- Possibly a lightweight database or file storage mechanism (though not explicitly mentioned, it might be used for registering and storing face embeddings).

##### Final Touches
- Every component is labeled with its purpose (e.g., “Handles user registration and face image capture” for CameraActivity).
- That the Android app’s assets incorporate a pre-converted model file from the training pipeline.
- Stress that both the training (offline/desktop) and inference (mobile) parts are integral to the overall face recognition operation.

## Diagram (Mermaid)

```mermaid
flowchart TD
    E1["User Devices (Android Phone)"]:::external

    subgraph "Android Mobile App"
        A1["Android UI & Activities (MainActivity, CameraActivity, RecyclerViewAdaptor)"]:::ui
        A2["Face Processing - MobileFaceNet"]:::faceProc
        A3["Face Processing - MTCNN"]:::faceProc
        A4["Local Storage/Assets (TFLite Models)"]:::storage
        A5["Helper & Utility Code"]:::util
    end

    subgraph "Training Pipeline (Python/OpenCV)"
        T4["Data & Encodings (openCV/Faces, encodings.pkl)"]:::data
        T1["Training Scripts (train_v2.py)"]:::train
        T3["Model Conversion Scripts (toTFLite.ipynb, architecture.py)"]:::convert
        T2["Face Detection & Recognition Script (detect.py)"]:::faceRec
    end

    E1 -->|"triggers"| A1
    A1 -->|"captures_face"| A3
    A1 -->|"recognizes_face"| A2
    A2 -->|"loads_model"| A4
    A3 -->|"crops_face"| A4
    A1 -->|"uses_utilities"| A5

    T4 -->|"provides_images"| T1
    T1 -->|"trains_model"| T3
    T3 -->|"produces_TFLite"| A4
    T1 -->|"triggers_detection"| T2

    %% Click Events
    click A1 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/app/src/main/java/com/example/scorpion/MainActivity.java"
    click A1 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/app/src/main/java/com/example/scorpion/CameraActivity.java"
    click A1 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/app/src/main/java/com/example/scorpion/ShowRegisteredFaces.java"
    click A2 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/app/src/main/java/com/example/scorpion/mobilefacenet/MobileFaceNet.java"
    click A3 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/tree/main/app/src/main/java/com/example/scorpion/mtcnn"
    click A4 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/tree/main/app/src/main/assets"
    click A5 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/app/src/main/java/com/example/scorpion/Helper.java"
    click A5 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/app/src/main/java/com/example/scorpion/MyUtil.java"
    click T1 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/openCV/train_v2.py"
    click T2 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/openCV/detect.py"
    click T3 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/openCV/toTFLite.ipynb"
    click T3 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/openCV/architecture.py"
    click T4 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/tree/main/openCV/Faces"
    click T4 "https://github.com/foxriver-2005/face-recognition-using-android-and-computer-vision-facereg-app-/blob/main/openCV/encodings/encodings.pkl"

    %% Styles
    classDef external fill:#ffcccc,stroke:#990000,stroke-width:2px;
    classDef ui fill:#ccffcc,stroke:#006600,stroke-width:2px;
    classDef faceProc fill:#cce5ff,stroke:#00509e,stroke-width:2px;
    classDef storage fill:#fce5cd,stroke:#e69138,stroke-width:2px;
    classDef util fill:#ead1dc,stroke:#b854a7,stroke-width:2px;
    classDef data fill:#fff2cc,stroke:#b45f06,stroke-width:2px;
    classDef train fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef convert fill:#d0e0e3,stroke:#115e67,stroke-width:2px;
    classDef faceRec fill:#f9cb9c,stroke:#e69138,stroke-width:2px;
```
