Dependency Management Strategy
Relevant source files
The following files were used as context for generating this wiki page:
The dependency management strategy for this project is bifurcated into two distinct environments: the Inference Environment and the Training Environment. This separation ensures that the lightweight production package remains minimal by utilizing ONNX Runtime, while the resource-intensive training utilities are isolated within a sub-module.
The project uses standard Python packaging tools, specifically setuptools for distribution and requirements.txt files for environment replication.
Environment Separation
The project explicitly distinguishes between the dependencies required to run the OCR model (Inference) and those required to develop or retrain it (Training). This is reflected in the file structure and the instructions provided in the documentation.
Inference Environment
This environment is designed for end-users of the mb_capcha_ocr library. It focuses on a minimal footprint to perform predictions using a pre-trained model.onnx file.
| Dependency | Purpose |
|---|---|
onnxruntime | Executing the optimized ONNX model |
pillow | Image loading and preprocessing (grayscale, resizing) |
numpy | Numerical operations on image arrays |
setuptools/wheel | Package installation and distribution |
Sources: requirements.txt:1-5, setup.py:19-27, README.md:52-56
Training Environment
The training environment requires a more robust stack, including deep learning frameworks and visualization tools. These dependencies are housed separately within the train_model/ directory.
| Dependency | Purpose |
|---|---|
torch | Core deep learning framework for model architecture |
torchvision | Computer vision utilities and ResNet18 base model |
matplotlib | Generating training loss plots (loss.png) |
onnx / onnxscript | Exporting the PyTorch model to ONNX format |
Sources: train_model/train_requirements.txt:1-7, README.md:58-64, train_model/train.py:1-12
Installation Flow
The following diagram illustrates the workflow for setting up the environment based on the user's intent (using the library vs. training the model).
Sources: README.md:14-30, setup.py:4-5
Automated Distribution Configuration
The setup.py file automates the dependency resolution for the distributable package. It dynamically reads the requirements.txt file and includes necessary non-code assets.
- Dynamic Requirements: The script reads
requirements.txtinto theinstall_requiresparameter. - Version Tracking: It extracts the package version from
mb_capcha_ocr/__init__.pyusing regular expressions. - Asset Inclusion: It ensures the pre-trained
model.onnxis bundled with the package usingpackage_data.
with open("requirements.txt") as f:
req = f.read().splitlines()
setup(
name='mb_capcha_ocr',
version=version,
packages=["mb_capcha_ocr"],
package_data={
'mb_capcha_ocr': ['model.onnx'],
},
install_requires=req,
include_package_data=True
)
Sources: setup.py:4-5, setup.py:20-33
Summary of Dependency Logic
The project's architecture ensures that the high-level API (OcrModel in predict.py) remains decoupled from the training framework. While train_model/train.py relies on torch, the mb_capcha_ocr/predict.py module only requires onnxruntime, allowing for a lighter deployment in production environments.
Sources: mb_capcha_ocr/predict.py:1-10, train_model/train.py:1-12