From 874077bf87e15208ba4d7bfab0095c3a60e4be17 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 4 Dec 2021 17:35:02 -0700 Subject: cock --- src/ocr.rs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src/ocr.rs') diff --git a/src/ocr.rs b/src/ocr.rs index cde116f..14021cc 100644 --- a/src/ocr.rs +++ b/src/ocr.rs @@ -1,18 +1,27 @@ use image::{DynamicImage, GrayImage, ImageFormat}; use imageproc::template_matching::MatchTemplateMethod; +use lazy_static::lazy_static; -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"), -]; +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 { let match_values = imageproc::template_matching::match_template( @@ -31,11 +40,7 @@ pub fn ocr(image: DynamicImage) -> Option { 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) + x_matches(&grayscale_image, &REFERENCES[i as usize]) .into_iter() .map(move |x| (i, x)) }) -- cgit v1.2.3