case_study
ocrblaze: an OCR service in Rust with Python's exact answers
Rust · ONNX Runtime · PP-OCRv5 · HTTP service and library
Author — port, parity suite, memory work, release
- Rust
- ONNX Runtime
- PP-OCRv5
- OpenCV (ported)
- numpy (ported)
- Docker distroless
- GitHub Actions
- crates.io

problem
The problem
The OCR service reads receipts, passports and ID cards for an HR platform. In Python it carried 20 pinned packages, about 99 MiB of wheels, and peaked at 541 MiB on its contract check after an earlier fix had already brought it down from 828. It shared a small host with other work, and on that host every hundred megabytes counted.
A rewrite is only worth it if nothing downstream notices. The parsers that pull totals and passport fields out of the text were tuned against the Python answers, so the new service had to give the same lines, boxes and scores, to the last digit, or every parser would need re-testing on real documents.
approach
What I engineered
- A step-for-step reproduction of RapidOCR 3.10 in 7,628 lines of Rust on ONNX Runtime: Pillow-exact image decode, OpenCV resize and perspective warp, a Clipper port, DB post-processing, CTC decoding and Python's own rounding, with the HTTP answers written byte for byte as the old service wrote them.
- A detector cut into 11 ONNX pieces so a 2,000-pixel page never holds four 64 MiB feature maps at once, with each squeeze-excitation mean summed in the exact lane order ONNX Runtime uses, so the tiled map is bit for bit the whole model's on arm64.
- A parity suite that tests exactly, not approximately: 138,648 rounding cases, 29,420 image operations, 34,000 geometry cases and 8,000 polygon offsets, 274 decode files, 216 end-to-end answers, and the reference forced into 22 CPU kernel settings.
- Per-CPU differences found and reproduced rather than tolerated: numpy's x86 sorting network leaving ties in its own order, OpenBLAS picking a fused multiply-add on some ARM cores and not others, GCC fusing arithmetic in the ARM wheels only.
- Memory trade-offs measured and mostly rejected: a custom allocator, a lower mmap threshold and one recogniser line per run all saved memory and cost too much time; tile size 384 was kept at about 5% more time.
result
The result
216 of 216 answers identical to the Python service on arm64 and on x86, and 1,421 of 1,421 identical across the 11 other official PP-OCRv5 recognisers. The arm64 contract-check peak fell from 541 MiB to 127–153 MiB; start-up to ready is 150–265 ms. Page time is the same as Python's, not better: almost all of it is spent inside ONNX Runtime either way.
It runs on staging; switching production over is the next step. On x86 the tiled detector is not bit-exact, so x86 runs the whole detector by default at 420 MiB, and one parity run on the production CPU itself is still to do.
- 216 / 216
- answers identical to Python, arm64 and x86
- 541 → 127–153 MiB
- contract-check peak on arm64
- 1,421 / 1,421
- identical across 11 other recognisers
- same speed
- page time vs Python, by design
Parity counts are from the public CI runs of the ocrblaze repository on GitHub's amd64 and arm64 runners in December 2026; memory is the cgroup peak of the contract check on a 2-CPU arm64 VM. The platform and its customers are not named.
work_with_me
Have a project like this?
Tell me the problem on a 20-minute call and I'll tell you how I'd approach it, the same way ocrblaze: an OCR service in Rust with Python's exact answers started.
Discuss your project