rlx_ocr/config.rs
1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! OCR configuration — detection thresholds, alphabet, decode method.
17
18/// Default character alphabet matching ocrs pretrained recognition models.
19pub const DEFAULT_ALPHABET: &str = " 0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~€ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
20
21/// Post-processing parameters for the text detection segmentation mask.
22#[derive(Clone, Debug, PartialEq)]
23pub struct DetectionParams {
24 /// Minimum area of word bounding boxes returned from connected components.
25 pub min_area: f32,
26 /// Per-pixel score threshold for classifying a pixel as text.
27 pub text_threshold: f32,
28}
29
30impl Default for DetectionParams {
31 fn default() -> Self {
32 Self {
33 min_area: 100.,
34 text_threshold: 0.2,
35 }
36 }
37}
38
39/// Method used to decode CRNN sequence outputs.
40#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
41pub enum DecodeMethod {
42 #[default]
43 Greedy,
44 BeamSearch {
45 width: u32,
46 },
47}
48
49/// Fixed input height for the recognition model (ocrs default).
50pub const RECOGNITION_INPUT_HEIGHT: u32 = 64;
51
52/// Shared OCR settings.
53#[derive(Clone, Debug)]
54pub struct OcrConfig {
55 pub detection: DetectionParams,
56 pub decode_method: DecodeMethod,
57 pub alphabet: String,
58}
59
60impl Default for OcrConfig {
61 fn default() -> Self {
62 Self {
63 detection: DetectionParams::default(),
64 decode_method: DecodeMethod::default(),
65 alphabet: DEFAULT_ALPHABET.to_string(),
66 }
67 }
68}