Skip to main content

Layout

Struct Layout 

Source
pub struct Layout {
    pub threshold: f32,
    /* private fields */
}
Expand description

载入好的版面模型。

Fields§

§threshold: f32

分数阈值;缺省同参考实现,可按需调整。

Implementations§

Source§

impl Layout

Source

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

dir 是模型目录:model.safetensors、config.json、preprocessor_config.json,可选 inference.yml。

Examples found in repository?
examples/layout_check.rs (line 55)
48fn main() {
49    let a: Vec<String> = std::env::args().skip(1).collect();
50    assert!(
51        a.len() == 3 || a.len() == 4,
52        "用法:layout_check <模型目录> <图> <输出> [分数阈值]"
53    );
54    let t = Instant::now();
55    let mut m = pagesift_ocr::layout::Layout::load(std::path::Path::new(&a[0])).unwrap();
56    if let Some(th) = a.get(3) {
57        m.threshold = th.parse().unwrap();
58    }
59    eprintln!("载入 {:.0} ms", t.elapsed().as_secs_f64() * 1e3);
60    let img = read_pnm(&a[1]);
61    // 设了 LAYOUT_RAW 就打印原始输出的左上角,和参考实现的集成测试对照
62    if std::env::var_os("LAYOUT_RAW").is_some() {
63        let r = m.raw(&img).unwrap();
64        let (_, k) = r.logits.tc();
65        let (q, _) = r.order.tc();
66        for i in 0..3 {
67            println!("logits {:?}", &r.logits.data[i * k..i * k + 3]);
68            println!("boxes {:?}", &r.boxes[i][..3]);
69            println!("order {:?}", &r.order.data[i * q..i * q + 3]);
70        }
71    }
72    let t = Instant::now();
73    let rs = m.detect(&img).unwrap();
74    let ms = t.elapsed().as_secs_f64() * 1e3;
75    eprintln!(
76        "{}: {}×{},{} 个区域,{ms:.0} ms",
77        a[1],
78        img.width,
79        img.height,
80        rs.len()
81    );
82    let mut s = String::new();
83    for r in &rs {
84        writeln!(
85            s,
86            "{:.4} {:.4} {:.4} {:.4} {:.4} {} {}",
87            r.x0, r.y0, r.x1, r.y1, r.score, r.label, r.kind
88        )
89        .unwrap();
90    }
91    std::fs::write(&a[2], s).unwrap();
92}
Source

pub fn detect(&self, img: &Image) -> Result<Vec<Region>, String>

检测整页图的版面区域,按预测的阅读顺序返回。

Examples found in repository?
examples/layout_check.rs (line 73)
48fn main() {
49    let a: Vec<String> = std::env::args().skip(1).collect();
50    assert!(
51        a.len() == 3 || a.len() == 4,
52        "用法:layout_check <模型目录> <图> <输出> [分数阈值]"
53    );
54    let t = Instant::now();
55    let mut m = pagesift_ocr::layout::Layout::load(std::path::Path::new(&a[0])).unwrap();
56    if let Some(th) = a.get(3) {
57        m.threshold = th.parse().unwrap();
58    }
59    eprintln!("载入 {:.0} ms", t.elapsed().as_secs_f64() * 1e3);
60    let img = read_pnm(&a[1]);
61    // 设了 LAYOUT_RAW 就打印原始输出的左上角,和参考实现的集成测试对照
62    if std::env::var_os("LAYOUT_RAW").is_some() {
63        let r = m.raw(&img).unwrap();
64        let (_, k) = r.logits.tc();
65        let (q, _) = r.order.tc();
66        for i in 0..3 {
67            println!("logits {:?}", &r.logits.data[i * k..i * k + 3]);
68            println!("boxes {:?}", &r.boxes[i][..3]);
69            println!("order {:?}", &r.order.data[i * q..i * q + 3]);
70        }
71    }
72    let t = Instant::now();
73    let rs = m.detect(&img).unwrap();
74    let ms = t.elapsed().as_secs_f64() * 1e3;
75    eprintln!(
76        "{}: {}×{},{} 个区域,{ms:.0} ms",
77        a[1],
78        img.width,
79        img.height,
80        rs.len()
81    );
82    let mut s = String::new();
83    for r in &rs {
84        writeln!(
85            s,
86            "{:.4} {:.4} {:.4} {:.4} {:.4} {} {}",
87            r.x0, r.y0, r.x1, r.y1, r.score, r.label, r.kind
88        )
89        .unwrap();
90    }
91    std::fs::write(&a[2], s).unwrap();
92}
Source

pub fn raw(&self, img: &Image) -> Result<Raw, String>

网络的原始输出(后处理之前),用来和参考实现逐数对照。

Examples found in repository?
examples/layout_check.rs (line 63)
48fn main() {
49    let a: Vec<String> = std::env::args().skip(1).collect();
50    assert!(
51        a.len() == 3 || a.len() == 4,
52        "用法:layout_check <模型目录> <图> <输出> [分数阈值]"
53    );
54    let t = Instant::now();
55    let mut m = pagesift_ocr::layout::Layout::load(std::path::Path::new(&a[0])).unwrap();
56    if let Some(th) = a.get(3) {
57        m.threshold = th.parse().unwrap();
58    }
59    eprintln!("载入 {:.0} ms", t.elapsed().as_secs_f64() * 1e3);
60    let img = read_pnm(&a[1]);
61    // 设了 LAYOUT_RAW 就打印原始输出的左上角,和参考实现的集成测试对照
62    if std::env::var_os("LAYOUT_RAW").is_some() {
63        let r = m.raw(&img).unwrap();
64        let (_, k) = r.logits.tc();
65        let (q, _) = r.order.tc();
66        for i in 0..3 {
67            println!("logits {:?}", &r.logits.data[i * k..i * k + 3]);
68            println!("boxes {:?}", &r.boxes[i][..3]);
69            println!("order {:?}", &r.order.data[i * q..i * q + 3]);
70        }
71    }
72    let t = Instant::now();
73    let rs = m.detect(&img).unwrap();
74    let ms = t.elapsed().as_secs_f64() * 1e3;
75    eprintln!(
76        "{}: {}×{},{} 个区域,{ms:.0} ms",
77        a[1],
78        img.width,
79        img.height,
80        rs.len()
81    );
82    let mut s = String::new();
83    for r in &rs {
84        writeln!(
85            s,
86            "{:.4} {:.4} {:.4} {:.4} {:.4} {} {}",
87            r.x0, r.y0, r.x1, r.y1, r.score, r.label, r.kind
88        )
89        .unwrap();
90    }
91    std::fs::write(&a[2], s).unwrap();
92}

Trait Implementations§

Source§

impl Clone for Layout

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Layout

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.