Filename-based Label Extraction
Relevant source files
The following files were used as context for generating this wiki page:
The Filename-based Label Extraction system is a core component of the mbbank-capcha-ocr project's data pipeline. Its primary purpose is to automate the labeling process for training an Optical Character Recognition (OCR) model by deriving ground-truth text directly from image filenames within a specified dataset directory.
This mechanism eliminates the need for separate metadata files or manual annotation databases. By adopting a convention where the image file name matches the CAPTCHA content, the system can efficiently load, shuffle, and prepare thousands of training samples for the PyTorch-based neural network.
Extraction Logic and Pattern Matching
The extraction process relies on string manipulation and regular expressions to isolate the label from the file system path. The system expects images to be stored in a flat directory structure where each file follows the format {label}.(png|jpg|jpeg).
Path Parsing Process
The system uses the os.path.join and re.split methods to handle cross-platform file paths. It isolates the filename from the full path and then strips the file extension to retrieve the raw string label.
The diagram shows the sequence of operations performed to transform a file path into a usable text label.
Sources: train_model/core.py:48-52, README.md:36-38
Dataset Loader Implementation
The DatasetLoader class, inheriting from torch.utils.data.Dataset, serves as the primary engine for extraction. During initialization, it scans the target directory and prepares a list of images, applying an optional limit to prevent model overfitting.
Key Methods and Components
| Component | Description |
|---|---|
__init__ | Initializes the path, shuffles images, and splits data into training and test sets. |
get_name(idx) | Retrieves the full path and extracts the label string using regex and string splitting. |
__getitem__(idx) | Loads the image, converts it to grayscale, encodes the extracted label, and returns the processed data. |
encode(char) | Converts a single character into a one-hot encoded vector based on a predefined character set. |
Sources: train_model/core.py:30-67, train_model/train.py:25-27
Encoding extracted labels
Once the string is extracted from the filename, it is transformed into a numerical format suitable for deep learning. Each character in the label (up to a maximum length of 6) is converted into a one-hot vector using a sorted list of alphanumeric characters.
This flow illustrates how the raw string label extracted from the filename is converted into a tensor-compatible format.
Sources: train_model/core.py:16-27, train_model/core.py:61-63
Configuration and Constraints
The extraction system is governed by several hardcoded parameters and environmental configurations defined in the core logic.
| Parameter | Value | Source |
|---|---|---|
| Dataset Path | dataset/ | train_model/train.py:20 |
| Supported Extensions | .png, .jpg, .jpeg | README.md:37 |
| Max Training Samples | 62,500 | train_model/core.py:40 |
| Test Split Ratio | 10% (min 1 sample) | train_model/core.py:42 |
| Max Captcha Length | 6 | train_model/core.py:14 |
Data Flow for Training
During the training loop, the DataLoader iterates through the DatasetLoader, providing batches of images alongside their extracted and encoded labels.
Sequence diagram demonstrating the interaction between the training script and the extraction logic.
Sources: train_model/train.py:38-41, train_model/core.py:54-67
Conclusion
The Filename-based Label Extraction system provides a streamlined, convention-over-configuration approach to dataset management. By tightly coupling the ground-truth labels with the filesystem metadata, the project simplifies the workflow for developers and researchers training OCR models for MBBank CAPTCHAs.