paddle_ocr_rs/
ocr_result.rs

1use std::fmt::{self, Write};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
6pub struct Point {
7    pub x: u32,
8    pub y: u32,
9}
10
11#[derive(Debug, Serialize, Deserialize)]
12pub struct TextBox {
13    pub points: Vec<Point>,
14    pub score: f32,
15}
16
17impl fmt::Display for TextBox {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        write!(
20            f,
21            "TextBox [score({}), [x: {}, y: {}], [x: {}, y: {}], [x: {}, y: {}], [x: {}, y: {}]]",
22            self.score,
23            self.points[0].x,
24            self.points[0].y,
25            self.points[1].x,
26            self.points[1].y,
27            self.points[2].x,
28            self.points[2].y,
29            self.points[3].x,
30            self.points[3].y,
31        )
32    }
33}
34
35#[derive(Debug, Default)]
36pub struct Angle {
37    pub index: i32,
38    pub score: f32,
39}
40
41impl fmt::Display for Angle {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        let header = if self.index >= 0 {
44            "Angle"
45        } else {
46            "AngleDisabled"
47        };
48        write!(
49            f,
50            "{}[Index({}), Score({})]",
51            header, self.index, self.score
52        )
53    }
54}
55
56#[derive(Debug, Default)]
57pub struct TextLine {
58    pub text: String,
59    pub text_score: f32,
60}
61
62impl fmt::Display for TextLine {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(
65            f,
66            "TextLine[Text({}),TextScore({})]",
67            self.text, self.text_score
68        )
69    }
70}
71
72#[derive(Debug, Serialize, Deserialize)]
73pub struct TextBlock {
74    pub box_points: Vec<Point>,
75    pub box_score: f32,
76
77    pub angle_index: i32,
78    pub angle_score: f32,
79
80    pub text: String,
81    pub text_score: f32,
82}
83
84#[derive(Serialize, Deserialize)]
85pub struct OcrResult {
86    pub text_blocks: Vec<TextBlock>,
87}
88
89impl fmt::Display for OcrResult {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        let mut str_builder = String::with_capacity(0);
92        for text_block in &self.text_blocks {
93            write!(
94                str_builder,
95                "TextBlock[BoxPointsLen({}), BoxScore({}), AngleIndex({}), AngleScore({}), Text({}), TextScore({})]",
96                text_block.box_points.len(),
97                text_block.box_score,
98                text_block.angle_index,
99                text_block.angle_score,
100                text_block.text,
101                text_block.text_score
102            )?;
103        }
104        f.write_str(&str_builder)
105    }
106}