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 { 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() }