ddddocr-rs is a Rust implementation of the popular Python ddddocr library. It provides offline local captcha recognition using deep learning models trained on synthetic data. The library is designed for testing captcha difficulty and works completely offline without any network calls.
This Rust port maintains the same recognition capabilities while offering Rust's performance and safety benefits.
- 🔒 Offline Recognition - No network calls required, completely local processing
- 🎯 Multiple Captcha Types - Supports text-based and character-based captchas
- ⚡ High Performance - Built with Rust for speed and efficiency
- 🛡️ Type Safe - Full Rust type safety with custom error handling
- 🔧 Simple API - Easy to use with minimal setup
- 🚀 Async Support - Built-in async inference support
Add this to your Cargo.toml:
cargo add ddddocruse ddddocr::DdddOcr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize the OCR with the model file
let mut ocr = DdddOcr::new("ddddocr.onnx")?;
// Read the captcha image
let image_bytes = std::fs::read("captcha.png")?;
// Perform recognition
let result = ocr.classification(&image_bytes).await?;
println!("Recognized text: {}", result);
Ok(())
}The library provides a custom error type for better error handling:
use ddddocr::{DdddOcr, DdddOcrError};
async fn recognize_captcha(image_data: &[u8]) -> Result<String, DdddOcrError> {
let mut ocr = DdddOcr::new("ddddocr.onnx")?;
ocr.classification(image_data).await
}
#[tokio::main]
async fn main() {
let image_data = std::fs::read("test.png").expect("Failed to read image");
match recognize_captcha(&image_data).await {
Ok(text) => println!("Recognized: {}", text),
Err(e) => eprintln!("Error: {}", e),
}
}You need to have the ddddocr.onnx model file. You can obtain it from the original ddddocr repository or use your own trained model.
The library processes images through the following pipeline:
- Image Decoding - Reads image from bytes (PNG, JPG, etc.)
- Resizing - Resizes to height=64 while maintaining aspect ratio
- Grayscale Conversion - Converts to single channel
- Normalization - Normalizes pixel values:
(pixel/255.0 - 0.5) / 0.5 - Inference - Runs ONNX model for prediction
- CTC Decoding - Decodes output using CTC (Connectionist Temporal Classification)
Main struct for OCR operations.
Creates a new DdddOcr instance by loading an ONNX model.
- Parameters:
model_path: Path to the ONNX model file (.onnx)
- Returns:
Result<DdddOcr, DdddOcrError>
Performs OCR recognition on image data.
- Parameters:
img: Raw image bytes
- Returns:
Result<String, DdddOcrError>- The recognized text
ort- ONNX Runtime for Rustimage- Image processing librarythiserror- Error handling
This project is licensed under the MIT License - see the LICENSE file for details.
- Original ddddocr Python library by sml2h3
- ONNX Runtime for providing the inference engine
- The Rust community for excellent tooling and libraries
This library is designed for captcha difficulty testing and educational purposes. The effectiveness of recognition varies depending on captcha types and may require fine-tuning with custom models for specific use cases.