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
|
use image::{DynamicImage, GrayImage, ImageFormat};
use imageproc::template_matching::MatchTemplateMethod;
const REFERENCE_PNGS: [&[u8]; 10] = [
include_bytes!("../data/0.png"),
include_bytes!("../data/1.png"),
include_bytes!("../data/2.png"),
include_bytes!("../data/3.png"),
include_bytes!("../data/4.png"),
include_bytes!("../data/5.png"),
include_bytes!("../data/6.png"),
include_bytes!("../data/7.png"),
include_bytes!("../data/8.png"),
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.9)
.map(|(x, _y, _pix)| x)
.collect()
}
pub fn ocr(image: DynamicImage) -> u32 {
let grayscale_image = image::imageops::grayscale(&image);
let mut digit_x_positions: Vec<(u8, u32)> = (0..10)
.flat_map(|i| {
let png_i =
image::load_from_memory_with_format(REFERENCE_PNGS[i as usize], ImageFormat::Png)
.unwrap();
let grey_png_i = image::imageops::grayscale(&png_i);
x_matches(&grayscale_image, &grey_png_i)
.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().unwrap()
}
|