Skip to main content

Rec

Struct Rec 

Source
pub struct Rec { /* private fields */ }
Expand description

载入好的识别模型。

Implementations§

Source§

impl Rec

Source

pub fn load(dir: &Path) -> Result<Rec, String>

dir 是模型目录:model.safetensors、config.json、preprocessor_config.json(含字符表)。

Examples found in repository?
examples/rec_check.rs (line 41)
38fn main() {
39    let a: Vec<String> = std::env::args().skip(1).collect();
40    let t = Instant::now();
41    let rec = pagesift_ocr::rec::Rec::load(std::path::Path::new(&a[0])).unwrap();
42    println!("载入 {:.0} ms", t.elapsed().as_secs_f64() * 1e3);
43    for p in &a[1..] {
44        let img = read_ppm(p);
45        let t = Instant::now();
46        let (text, score) = rec.recognize(&img).unwrap();
47        let ms = t.elapsed().as_secs_f64() * 1e3;
48        let name = std::path::Path::new(p)
49            .file_name()
50            .unwrap()
51            .to_string_lossy();
52        println!(
53            "{name}\t{}x{}\t{ms:.0} ms\t{score:.3}\t{text}",
54            img.width, img.height
55        );
56    }
57}
Source

pub fn recognize(&self, line: &Image) -> Result<(String, f32), String>

识别一行字的图(已裁出、拉正成水平的一行)。返回文字和置信度。

Examples found in repository?
examples/rec_check.rs (line 46)
38fn main() {
39    let a: Vec<String> = std::env::args().skip(1).collect();
40    let t = Instant::now();
41    let rec = pagesift_ocr::rec::Rec::load(std::path::Path::new(&a[0])).unwrap();
42    println!("载入 {:.0} ms", t.elapsed().as_secs_f64() * 1e3);
43    for p in &a[1..] {
44        let img = read_ppm(p);
45        let t = Instant::now();
46        let (text, score) = rec.recognize(&img).unwrap();
47        let ms = t.elapsed().as_secs_f64() * 1e3;
48        let name = std::path::Path::new(p)
49            .file_name()
50            .unwrap()
51            .to_string_lossy();
52        println!(
53            "{name}\t{}x{}\t{ms:.0} ms\t{score:.3}\t{text}",
54            img.width, img.height
55        );
56    }
57}
Source

pub fn input_channels(&self) -> usize

网络输入的通道数。

Source

pub fn input_height(&self) -> usize

网络输入的高。

Source

pub fn preprocess(&self, line: &Image) -> Result<Tensor, String>

预处理一行图成网络输入 [3, 48, W]。

Source

pub fn logits_on<E: Exec>(&self, e: &E, x: &E::T) -> E::T

网络部分:[3, 48, W] → [T, 字符表] 的 logits,在执行器上算。

Source

pub fn decode(&self, rows: &[(u32, f32)]) -> (String, f32)

CTC 贪心解码:输入每步的(最大类,其概率)。先去相邻重复、再去 blank; 置信度是保留下来的各字最大概率的平均。

Auto Trait Implementations§

§

impl Forget for Rec

§

impl Freeze for Rec

§

impl RefUnwindSafe for Rec

§

impl Send for Rec

§

impl Sync for Rec

§

impl Unpin for Rec

§

impl UnsafeUnpin for Rec

§

impl UnwindSafe for Rec

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.