diff options
Diffstat (limited to 'src/ocr.rs')
-rw-r--r-- | src/ocr.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/ocr.rs b/src/ocr.rs new file mode 100644 index 0000000..bc7e535 --- /dev/null +++ b/src/ocr.rs @@ -0,0 +1,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() +} |