Integrating with Backend Systems
Relevant source files
The following files were used as context for generating this wiki page:
Integrating the MBBank CAPTCHA OCR system into backend environments involves leveraging the OcrModel class to process image data and return predicted text. The system is designed as a modular library that can be installed via setuptools and utilized within any Python-based backend service to automate the resolution of alphanumeric CAPTCHAs.
The integration focuses on a two-stage lifecycle: the training phase, which produces an ONNX model, and the inference phase, which uses a lightweight runtime to perform predictions without requiring heavy deep learning frameworks like PyTorch in the production environment.
Sources: README.md:1-10, setup.py:1-29
Core Architecture for Integration
The backend integration relies on the onnxruntime for high-performance inference. The architecture separates the complex neural network definition (used during training) from the execution logic used in production.
Inference Pipeline
The inference pipeline transforms raw image data into a format suitable for the ONNX model. This involves converting images to grayscale, resizing to a fixed dimension, and normalizing pixel values.
The diagram shows the sequential data transformations required before the model can process the input.
Sources: mb_capcha_ocr/predict.py:20-28
Model Component Overview
The following table describes the primary components involved in the integration:
| Component | Description | Source File |
|---|---|---|
OcrModel (Inference) | Wrapper for ONNX runtime to perform predictions. | mb_capcha_ocr/predict.py |
InferenceSession | The ONNX runtime engine that loads model.onnx. | mb_capcha_ocr/predict.py |
chars List | The sorted list of alphanumeric characters (0-9, a-z, A-Z). | train_model/core.py |
model.onnx | The exported weight file used for backend deployment. | setup.py |
Sources: mb_capcha_ocr/predict.py:11-18, train_model/core.py:14-16, setup.py:24-26
Implementation Details
API for Prediction
Backend systems interact with the OCR module through the predict method. This method accepts a PIL Image object and returns a string.
# Implementation of prediction logic
def predict(self, img_data: Image):
img = img_data.convert("L")
img = img.resize((160, 50))
img = np.array(img, dtype=np.float32) / 255.0
img = np.expand_dims(img, axis=0) # Add batch dimension
img = np.expand_dims(img, axis=0) # Add channel dimension
input_name = self.session.get_inputs()[0].name
pred = self.session.run(None, {input_name: img})[0]
pred_labels = np.argmax(pred, axis=2)
pred_text = [''.join([self.chars[c] for c in pred_label]) for pred_label in pred_labels]
return pred_text[0]
Sources: mb_capcha_ocr/predict.py:20-33
Data Flow for Backend Requests
When a backend service receives a CAPTCHA image, it follows a specific sequence of calls to the mb_capcha_ocr library.
The sequence diagram illustrates the interaction between the host backend and the OCR components.
Sources: mb_capcha_ocr/predict.py:15-33, README.md:46-52
Configuration and Requirements
To integrate this system, the backend environment must satisfy specific dependencies. The inference environment is kept minimal by using onnxruntime instead of torch.
Dependency Configuration
The backend requires the following packages for operation:
onnxruntime: For executing the model.Pillow: For image manipulation.numpy: For numerical processing.
Sources: requirements.txt:1-5, setup.py:6-7
Model Parameters
The model expects inputs and produces outputs based on predefined constraints in the training core:
- Max CAPTCHA Length: 6 characters.
- Input Dimensions: 160x50 pixels.
- Character Set: Alphanumeric (digits + ascii_letters).
Sources: train_model/core.py:14-17, mb_capcha_ocr/predict.py:13
Conclusion
Integrating mb_capcha_ocr into a backend system provides a streamlined way to resolve CAPTCHAs. By utilizing an ONNX-based inference class, the integration remains lightweight and efficient, requiring only standard image processing libraries and a runtime environment to process automated requests.