aboutsummaryrefslogtreecommitdiff
path: root/src/ocr.rs
blob: 14021ccde04e199d4abfb4ebab3a257e7d97588c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use image::{DynamicImage, GrayImage, ImageFormat};
use imageproc::template_matching::MatchTemplateMethod;
use lazy_static::lazy_static;

lazy_static! {
    static ref REFERENCES: [GrayImage; 10] = {
        let parse = |data: &[u8]| {
            let png = image::load_from_memory_with_format(data, ImageFormat::Png).unwrap();
            image::imageops::grayscale(&png)
        };
        [
            parse(include_bytes!("../data/0.png")),
            parse(include_bytes!("../data/1.png")),
            parse(include_bytes!("../data/2.png")),
            parse(include_bytes!("../data/3.png")),
            parse(include_bytes!("../data/4.png")),
            parse(include_bytes!("../data/5.png")),
            parse(include_bytes!("../data/6.png")),
            parse(include_bytes!("../data/7.png")),
            parse(include_bytes!("../data/8.png")),
            parse(include_bytes!("../data/9.png")),
        ]
    };
}

fn x_matches(image: &GrayImage, template: &GrayImage) -> Vec<u32> {
    let match_values = imageproc::template_matching::match_template(
        image,
        template,
        MatchTemplateMethod::CrossCorrelationNormalized,
    );
    match_values
        .enumerate_pixels()
        .filter(|(_x, _y, pix)| pix.0[0] > 0.95)
        .map(|(x, _y, _pix)| x)
        .collect()
}

pub fn ocr(image: DynamicImage) -> Option<u32> {
    let grayscale_image = image::imageops::grayscale(&image);
    let mut digit_x_positions: Vec<(u8, u32)> = (0..10)
        .flat_map(|i| {
            x_matches(&grayscale_image, &REFERENCES[i as usize])
                .into_iter()
                .map(move |x| (i, x))
        })
        .collect();
    digit_x_positions.sort_by_key(|&(_i, x)| x);
    let digits: String = digit_x_positions
        .into_iter()
        .map(|(i, _x)| format!("{}", i))
        .collect();
    dbg!(&digits);
    digits.parse().ok()
}