# bscs24133/psl-to-speech-project architecture

> Architecture diagram of the GitHub repository bscs24133/psl-to-speech-project, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bscs24133/psl-to-speech-project
- Repository: https://github.com/bscs24133/psl-to-speech-project

## Overview

1. **Overall shape**
   - This repository is a **local, end-to-end PSL recognition system** with three clear stages: **dataset preparation**, **model training/evaluation**, and **real-time webcam inference**.
   - The runtime path is not a generic web service; it is a **desktop/edge pipeline** that captures hand signs, classifies them, then renders and speaks Urdu output.

2. **Two recognition branches**
   - The project has **two model families**:
     - **Alphabet recognition** for PSL letters.
     - **Word recognition** for PSL vocabulary signs.
   - These are stored as separate artifacts (`src/models/alphabet_cnn.h5` and `src/models/word_cnn.h5`) and are trained/evaluated by separate scripts.

3. **Input data sources**
   - Training data comes from two local datasets:
     - **UAlpha40** for Urdu alphabet signs, mixing **static image signs** and **dynamic video signs**.
     - **PSL Dictionary Toolkit** for **80 word-level video signs**.
   - The datasets are intentionally kept outside GitHub because they are large and machine-local.

4. **Preprocessing boundary**
   - Raw images/videos are not fed directly into the classifier.
   - `preprocess.py`, `collect_webcam.py`, and `hand_crop_flow.py` form the data-ingestion boundary that normalizes input into consistent, model-ready samples.
   - `hand_landmarker.task` indicates the pipeline uses a **hand landmark detector** to localize the signing hand before feature extraction.

5. **Core feature representation: optical flow**
   - `optical_flow.py` is the central transformation step.
   - It converts a sign video into a **temporal motion tensor** by sampling **16 frames**, generating **15 optical-flow maps**, and resizing them to **64x64** with **2 channels** (horizontal and vertical motion).
   - This is the key architectural choice: the classifier learns **motion patterns**, not just static hand shapes.

6. **Generated artifact layer**
   - Processed flow arrays are saved as `.npy` files under `Dataset/3_OpticalFlow/`.
   - This creates a clean separation between:
     - **raw media** (JPG/MP4),
     - **intermediate motion features** (`.npy`),
     - and **trained models** (`.h5`).
   - That separation makes the training and inference code much simpler and faster.

7. **Data augmentation and class balancing**
   - `augment_flow.py` and `augment_words.py` expand small or imbalanced classes using transformations like flipping, brightness changes, and noise.
   - This is especially important for the dynamic alphabet subset, where the number of original samples is limited.

8. **Training subsystem**
   - `train.py`, `train_alphabet.py`, and `finetune.py` handle model fitting.
   - The architecture is likely a **CNN over optical-flow tensors**, with one training path tuned for alphabet signs and another for word signs.
   - `src/models/` is the model boundary: training outputs are serialized there for later inference.

9. **Evaluation and verification**
   - `evaluate.py` and `evaluate_alphabet.py` measure performance, likely including accuracy and confusion-matrix style analysis.
   - `verify_flow.py` provides a visual sanity-check layer for ensuring optical-flow generation is correct before training.
   - This repo treats feature correctness as a first-class concern, which is important for motion-based recognition.

10. **Live inference and user-facing output**
   - `app.py`, `inference.py`, `text_builder.py`, `translator.py`, `urdu_renderer.py`, and `tts_engine.py` form the runtime experience.
   - The live loop is: **webcam frame buffer → hand crop/flow features → CNN prediction → text assembly → Urdu translation/rendering → speech synthesis**.
   - `text_builder.py` suggests predictions are accumulated into readable phrases rather than shown as isolated labels.

11. **Urdu language output layer**
   - The system does not stop at English class labels; it converts them into **Urdu text and speech**.
   - `translator.py` handles text translation, `urdu_renderer.py` handles proper Urdu display, and `tts_engine.py` handles audio output.
   - These components introduce external service dependencies through translation/TTS libraries, unlike the core CV and CNN pipeline which is local.

12. **Configuration and portability**
   - `config.example.py` centralizes paths and dataset locations, which is important because the repo expects **large local datasets** and machine-specific directories.
   - `requirements.txt` defines the runtime stack, with the major architecture-relevant dependencies being **OpenCV**, **NumPy**, **TensorFlow/Keras**, **MediaPipe**, **matplotlib**, and speech/translation libraries.
   - The repository is therefore structured as a **portable research system**, but only if the external datasets and local model assets are present.

13. **Notable repo boundary**
   - The README uses a few older names (`config.py`, `translate.py`), while the current tree shows `config.example.py` and `translator.py`.
   - For diagramming, treat the **actual code tree as authoritative** and the README as the conceptual description of the pipeline.

Main entry points: [`src/optical_flow.py`](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/optical_flow.py) (Optical Flow), [`src/models/word_cnn.h5`](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/models/word_cnn.h5) (Word CNN), [`src/inference.py`](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/inference.py) (Inference), [`src/models/alphabet_cnn.h5`](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/models/alphabet_cnn.h5) (Alphabet CNN)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_data["Data Prep"]
  node_n_preprocess["Preprocess<br/>ingestion script<br/>[preprocess.py]"]
  node_n_collect["Webcam Capture<br/>capture tool<br/>[collect_webcam.py]"]
  node_n_handcrop["Hand Crop<br/>crop pipeline<br/>[hand_crop_flow.py]"]
  node_n_landmarker{{"Hand Landmarker<br/>mediapipe task"}}
  node_n_optflow["Optical Flow<br/>feature transform<br/>[optical_flow.py]"]
  node_n_proc_alpha["Alpha Flows<br/>dataset builder"]
  node_n_proc_words["Word Flows<br/>dataset builder"]
  node_n_augment_flow["Flow Augment<br/>augmentation<br/>[augment_flow.py]"]
  node_n_augment_words["Word Augment<br/>augmentation<br/>[augment_words.py]"]
end

subgraph group_g_model["Modeling"]
  node_n_train["Train<br/>[train.py]"]
  node_n_train_alpha["Alpha Train<br/>training<br/>[train_alphabet.py]"]
  node_n_finetune["Finetune<br/>training<br/>[finetune.py]"]
  node_n_eval["Evaluate<br/>evaluation<br/>[evaluate.py]"]
  node_n_eval_alpha["Alpha Eval<br/>evaluation"]
  node_n_verify["Verify Flow<br/>sanity check<br/>[verify_flow.py]"]
end

subgraph group_g_runtime["Runtime"]
  node_n_app["App<br/>runtime entrypoint<br/>[app.py]"]
  node_n_infer["Inference<br/>prediction loop<br/>[inference.py]"]
  node_n_text["Text Builder<br/>phrase assembly<br/>[text_builder.py]"]
  node_n_translator["Translator<br/>language bridge<br/>[translator.py]"]
  node_n_urdu["Urdu Render<br/>rendering<br/>[urdu_renderer.py]"]
  node_n_tts["TTS Engine<br/>speech output<br/>[tts_engine.py]"]
end

subgraph group_g_assets["Assets"]
  node_n_alpha_model[("Alphabet CNN<br/>trained model<br/>[alphabet_cnn.h5]")]
  node_n_word_model[("Word CNN<br/>trained model<br/>[word_cnn.h5]")]
end

node_n_landmarker -->|"landmarks"| node_n_handcrop
node_n_preprocess -->|"normalize"| node_n_optflow
node_n_collect -->|"capture"| node_n_handcrop
node_n_handcrop -->|"crop frames"| node_n_optflow
node_n_optflow -->|"alpha features"| node_n_proc_alpha
node_n_optflow -->|"word features"| node_n_proc_words
node_n_proc_alpha -->|"balance"| node_n_augment_flow
node_n_proc_words -->|"expand"| node_n_augment_words
node_n_augment_flow -->|"train set"| node_n_train_alpha
node_n_augment_words -->|"train set"| node_n_train
node_n_train_alpha -->|"save model"| node_n_alpha_model
node_n_train -->|"save model"| node_n_word_model
node_n_finetune -->|"refine"| node_n_word_model
node_n_alpha_model -->|"evaluate"| node_n_eval_alpha
node_n_word_model -->|"evaluate"| node_n_eval
node_n_optflow -->|"inspect"| node_n_verify
node_n_app -->|"launch"| node_n_infer
node_n_infer -->|"assemble"| node_n_text
node_n_text -->|"translate"| node_n_translator
node_n_translator -->|"render"| node_n_urdu
node_n_urdu -->|"speak"| node_n_tts
node_n_alpha_model -->|"alphabet path"| node_n_infer
node_n_word_model -->|"word path"| node_n_infer

click node_n_preprocess "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/preprocess.py"
click node_n_collect "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/collect_webcam.py"
click node_n_handcrop "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/hand_crop_flow.py"
click node_n_landmarker "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/hand_landmarker.task"
click node_n_optflow "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/optical_flow.py"
click node_n_proc_alpha "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/process_alpha_flows.py"
click node_n_proc_words "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/process_word_flows.py"
click node_n_augment_flow "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/augment_flow.py"
click node_n_augment_words "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/augment_words.py"
click node_n_train "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/train.py"
click node_n_train_alpha "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/train_alphabet.py"
click node_n_finetune "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/finetune.py"
click node_n_eval "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/evaluate.py"
click node_n_eval_alpha "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/evaluate_alphabet.py"
click node_n_verify "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/verify_flow.py"
click node_n_app "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/app.py"
click node_n_infer "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/inference.py"
click node_n_text "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/text_builder.py"
click node_n_translator "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/translator.py"
click node_n_urdu "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/urdu_renderer.py"
click node_n_tts "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/tts_engine.py"
click node_n_alpha_model "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/models/alphabet_cnn.h5"
click node_n_word_model "https://github.com/bscs24133/psl-to-speech-project/blob/main/src/models/word_cnn.h5"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_preprocess,node_n_collect,node_n_handcrop,node_n_landmarker,node_n_optflow,node_n_proc_alpha,node_n_proc_words,node_n_augment_flow,node_n_augment_words toneBlue
class node_n_train,node_n_train_alpha,node_n_finetune,node_n_eval,node_n_eval_alpha,node_n_verify toneAmber
class node_n_app,node_n_infer,node_n_text,node_n_translator,node_n_urdu,node_n_tts toneMint
class node_n_alpha_model,node_n_word_model toneRose
```

## Components

### Data Prep

Raw dataset ingestion and feature generation

- [Preprocess](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/preprocess.py): ingestion script · Normalizes raw sign data before feature extraction · `src/preprocess.py`
- [Webcam Capture](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/collect_webcam.py): capture tool · Collects live hand-sign samples from camera input · `src/collect_webcam.py`
- [Hand Crop](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/hand_crop_flow.py): crop pipeline · Locates and crops the signing hand using landmarks · `src/hand_crop_flow.py`
- [Hand Landmarker](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/hand_landmarker.task): mediapipe task · Hand landmark detector used to localize hands in frames · `src/hand_landmarker.task`
- [Optical Flow](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/optical_flow.py): feature transform · Converts sign videos into 16-frame motion tensors of 15 flow maps · `src/optical_flow.py`
- [Alpha Flows](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/process_alpha_flows.py): dataset builder · Processes alphabet samples into stored optical-flow arrays · `src/process_alpha_flows.py`
- [Word Flows](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/process_word_flows.py): dataset builder · Processes word-sign videos into stored optical-flow arrays · `src/process_word_flows.py`
- [Flow Augment](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/augment_flow.py): augmentation · Expands optical-flow samples for balancing and robustness · `src/augment_flow.py`
- [Word Augment](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/augment_words.py): augmentation · Augments word-level samples with visual perturbations · `src/augment_words.py`

### Modeling

Training, finetuning, and evaluation

- [Train](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/train.py): training · Main model fitting entrypoint · `src/train.py`
- [Alpha Train](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/train_alphabet.py): training · Alphabet-specific training path · `src/train_alphabet.py`
- [Finetune](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/finetune.py): training · Refines pretrained models on target data · `src/finetune.py`
- [Evaluate](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/evaluate.py): evaluation · Runs model performance checks for the main branch · `src/evaluate.py`
- [Alpha Eval](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/evaluate_alphabet.py): evaluation · Evaluates alphabet recognition performance · `src/evaluate_alphabet.py`
- [Verify Flow](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/verify_flow.py): sanity check · Visual inspection of optical-flow generation correctness · `src/verify_flow.py`

### Runtime

Live webcam inference and user-facing output

- [App](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/app.py): runtime entrypoint · Starts the live PSL-to-speech experience · `src/app.py`
- [Inference](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/inference.py): prediction loop · Runs webcam-based classification over prepared features · `src/inference.py`
- [Text Builder](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/text_builder.py): phrase assembly · Accumulates predictions into readable text · `src/text_builder.py`
- [Translator](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/translator.py): language bridge · Converts predicted text into Urdu output · `src/translator.py`
- [Urdu Render](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/urdu_renderer.py): rendering · Formats Urdu text for display · `src/urdu_renderer.py`
- [TTS Engine](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/tts_engine.py): speech output · Synthesizes spoken Urdu audio · `src/tts_engine.py`

### Assets

Persisted models and detector artifact

- [Alphabet CNN](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/models/alphabet_cnn.h5): trained model · Serialized alphabet recognition model · `src/models/alphabet_cnn.h5`
- [Word CNN](https://github.com/bscs24133/psl-to-speech-project/blob/main/src/models/word_cnn.h5): trained model · Serialized word recognition model · `src/models/word_cnn.h5`

## Connections

- Hand Landmarker → Hand Crop: landmarks · Uses detected hand landmarks to crop the signing hand
- Preprocess → Optical Flow: normalize · Feeds cleaned samples into optical-flow extraction
- Webcam Capture → Hand Crop: capture · Collects webcam frames for crop and flow preparation
- Hand Crop → Optical Flow: crop frames · Provides hand-centered frames to motion extraction
- Optical Flow → Alpha Flows: alpha features · Produces flow tensors for alphabet dataset processing
- Optical Flow → Word Flows: word features · Produces flow tensors for word dataset processing
- Alpha Flows → Flow Augment: balance · Augments alphabet flow samples
- Word Flows → Word Augment: expand · Augments word-level samples
- Flow Augment → Alpha Train: train set · Supplies augmented alphabet data to alphabet training
- Word Augment → Train: train set · Supplies augmented word data to main training
- Alpha Train → Alphabet CNN: save model · Serializes the alphabet CNN artifact
- Train → Word CNN: save model · Serializes the word CNN artifact
- Finetune → Word CNN: refine · Updates the stored model after transfer learning
- Alphabet CNN → Alpha Eval: evaluate · Evaluates the alphabet model
- Word CNN → Evaluate: evaluate · Evaluates the word model
- Optical Flow → Verify Flow: inspect · Checks correctness of generated flow tensors
- App → Inference: launch · Application entrypoint starts inference loop
- Inference → Text Builder: assemble · Turns class predictions into phrase-like output
- Text Builder → Translator: translate · Passes assembled text into Urdu translation
- Translator → Urdu Render: render · Formats translated text for Urdu display
- Urdu Render → TTS Engine: speak · Feeds rendered Urdu text to speech synthesis
- Alphabet CNN → Inference: alphabet path · Used by live inference when alphabet recognition is active
- Word CNN → Inference: word path · Used by live inference when word recognition is active
