#ocr #onnx #paddle #ocr-lite

paddle-ocr-rs

Use Rust to call Paddle OCR models via ONNX Runtime for image text recognition

4 releases (2 breaking)

new 0.3.0 Apr 27, 2025
0.2.1 Apr 26, 2025
0.2.0 Apr 26, 2025
0.1.0 Mar 14, 2025

#256 in Machine learning

Download history 88/week @ 2025-03-09 37/week @ 2025-03-16 7/week @ 2025-03-23 6/week @ 2025-03-30 1/week @ 2025-04-13 91/week @ 2025-04-20

108 downloads per month

Apache-2.0

190KB
1K SLoC

简体中文

paddle-ocr-rs

Use Rust to call Paddle OCR models via ONNX Runtime for image text recognition.

Example

use crate::{ocr_error::OcrError, ocr_lite::OcrLite};

fn run_test() -> Result<(), OcrError> {
    let mut ocr = OcrLite::new();
    ocr.init_models(
        "./models/ch_PP-OCRv4_det_infer.onnx",
        "./models/ch_ppocr_mobile_v2.0_cls_infer.onnx",
        "./models/ch_PP-OCRv4_rec_infer.onnx",
        "./models/ppocr_keys_v1.txt",
        2,
    )?;

    println!("===test_1===");
    let res =
        ocr.detect_from_path("./test/test_1.png", 50, 1024, 0.5, 0.3, 1.6, true, false)?;
    res.text_blocks.iter().for_each(|item| {
        println!("text: {} score: {}", item.text, item.text_score);
    });
    println!("===test_2===");
    let res =
        ocr.detect_from_path("./test/test_2.png", 50, 1024, 0.5, 0.3, 1.6, true, false)?;
    res.text_blocks.iter().for_each(|item| {
        println!("text: {} score: {}", item.text, item.text_score);
    });

   // 通过 image 读取图片
    println!("===test_3===");
    let test_three_img = image::open("./docs/test_images/test_3.png").unwrap().to_rgb8();
    let res = ocr.detect(&test_three_img, 50, 1024, 0.5, 0.3, 1.6, true, false)?;
    res.text_blocks.iter().for_each(|item| {
        println!("text: {} score: {}", item.text, item.text_score);
    });

    Ok(())
}

Reference Development Environment

Dependency Version
rustc 1.84.1 (e71f9a9a9 2025-01-27)
cargo 1.84.1 (66221abde 2024-11-19)
OS Windows 11 24H2
Paddle OCR 4

Model Source

RapidOCR Docs

The code is referenced from RapidOcrOnnx, and has replaced OpenCV with image and imageproc libraries for image-related implementations.

Results Demonstration

test_1.png

test_1

text: 使用Rust 通过ONNX Runtime 调用 Paddle OCR 模型进行图片文字识别。 score: 0.95269924
text: paddle-ocr-rs score: 0.9979071

test_2.png

test_2

text: 母婴用品连锁 score: 0.99713486

test_3.png

test_3

Output Preview

text: salta sobre o cao preguicoso. score: 0.9794339
text: perezoso. A raposa marrom rapida score: 0.9970329
text: marron rapido salta sobre el perro score: 0.9995695
text: salta sopra il cane pigro. El zorro score: 0.99923337
text: paresseux. La volpe marrone rapida score: 0.9991456
text: 《rapide> saute par-dessus le chien score: 0.9685502
text: uber den faulen Hund. Le renard brun score: 0.988613
text: Der ,schnelle" braune Fuchs springt score: 0.97560924
text: from aspammer@website.com is spam. score: 0.98167914
text: & duck/goose, as 12.5% of E-mail score: 0.98472834
text: Over the $43,456.78 <lazy> #90 dog score: 0.9847551
text: The (quick) [brown] {fox} jumps! score: 0.98300403

Dependencies

~15MB
~301K SLoC