#onnx #ocr #paddle

paddle-ocr-rs

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

13 releases (4 breaking)

Uses new Rust 2024

0.6.1 Aug 31, 2025
0.6.0 Jun 25, 2025
0.5.2 Jun 15, 2025
0.3.4 May 16, 2025
0.1.0 Mar 14, 2025

#401 in Machine learning

Download history 299/week @ 2025-12-05 342/week @ 2025-12-12 88/week @ 2025-12-19 103/week @ 2025-12-26 73/week @ 2026-01-02 46/week @ 2026-01-09 46/week @ 2026-01-16 53/week @ 2026-01-23 109/week @ 2026-01-30 272/week @ 2026-02-06 168/week @ 2026-02-13 64/week @ 2026-02-20 60/week @ 2026-02-27 85/week @ 2026-03-06 150/week @ 2026-03-13 94/week @ 2026-03-20

395 downloads per month

Apache-2.0

320KB
1.5K 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-OCRv5_mobile_det.onnx",
        "./models/ch_ppocr_mobile_v2.0_cls_infer.onnx",
        "./models/ch_PP-OCRv5_rec_mobile_infer.onnx",
        2,
    )?;

    println!("===test_1===");
    let res = ocr.detect_from_path(
        "./docs/test_images/test_1.png",
        50,
        1024,
        0.5,
        0.3,
        1.6,
        false,
        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(
        "./docs/test_images/test_2.png",
        50,
        1024,
        0.5,
        0.3,
        1.6,
        false,
        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(())
}

// 某些情况下角度纠正会得出错误结果,支持角度纠正回退,当角度纠正后的文本识别得分低于指定值(或为 NaN)时,将使用进行角度纠正前的图片进行识别
fn run_test_angle_rollback() -> 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",
        2,
    )?;


    println!("===test_angle_ori===");
    let test_img = image::open("./docs/test_images/test_4.png")
        .unwrap()
        .to_rgb8();
    let res = ocr.detect(&test_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);
    });


    println!("===test_angle_rollback===");
    let test_img = image::open("./docs/test_images/test_4.png")
        .unwrap()
        .to_rgb8();
    let res =
        ocr.detect_angle_rollback(&test_img, 50, 1024, 0.5, 0.3, 1.6, true, false, 0.8)?;
    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

Documentation

Error Handling

Static Linking Reference

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

~27MB
~464K SLoC