aboutsummaryrefslogtreecommitdiff
path: root/src/ocr.rs
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-12-04 17:35:02 -0700
committerMelody Horn <melody@boringcactus.com>2021-12-04 17:35:02 -0700
commit874077bf87e15208ba4d7bfab0095c3a60e4be17 (patch)
treef0e9ca4d4e11b8a2b72d1ceb5af98e0fb26a8333 /src/ocr.rs
parentce5be7f783ac7c6589b501433caafd717c6e119f (diff)
downloadqueue-go-brrr-874077bf87e15208ba4d7bfab0095c3a60e4be17.tar.gz
queue-go-brrr-874077bf87e15208ba4d7bfab0095c3a60e4be17.zip
cock
Diffstat (limited to 'src/ocr.rs')
-rw-r--r--src/ocr.rs39
1 files changed, 22 insertions, 17 deletions
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<u32> {
let match_values = imageproc::template_matching::match_template(
@@ -31,11 +40,7 @@ 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| {
- 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))
})