From 874077bf87e15208ba4d7bfab0095c3a60e4be17 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 4 Dec 2021 17:35:02 -0700 Subject: cock --- Cargo.toml | 2 ++ src/lib.rs | 21 ++++++++++++++++++--- src/ocr.rs | 39 ++++++++++++++++++++++----------------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73a2795..1b6db0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,12 @@ crate-type = ["cdylib", "rlib"] [dependencies] base64 = "0.13.0" chrono = { version = "0.4.19", features = ["wasmbind"] } +futures = "0.3.18" gloo = { version = "0.4.0", features = ["futures"] } image = "0.23.14" imageproc = "0.22.0" js-sys = "0.3.55" +lazy_static = "1.4.0" wasm-bindgen = "0.2.63" wasm-bindgen-futures = "0.4.28" web-sys = { version = "0.3.55", features = [ diff --git a/src/lib.rs b/src/lib.rs index 8c8bcc3..e90202d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ use image::{DynamicImage, ImageFormat}; use std::cell::{Cell, RefCell}; use std::rc::Rc; +use futures::channel::mpsc; +use futures::prelude::*; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; use wasm_bindgen_futures::*; @@ -84,6 +86,8 @@ async fn do_boot() { let history = Rc::new(RefCell::new(history::History::default())); + let (manual_update_tx, manual_update_rx) = mpsc::channel::<()>(5); + let video: HtmlVideoElement = document .get_element_by_id("video") .unwrap() @@ -107,7 +111,9 @@ async fn do_boot() { let width = width.clone(); let height = height.clone(); let target_marker = target_marker.clone(); + let manual_update_tx = manual_update_tx.clone(); gloo::events::EventListener::new(&video, "click", move |event| { + let mut manual_update_tx = manual_update_tx.clone(); let video = also_video.clone(); let mouse_event: &MouseEvent = event.dyn_ref().unwrap(); let mouse_x = mouse_event.offset_x() as f64; @@ -120,6 +126,7 @@ async fn do_boot() { let real_y = mouse_y / fake_height * real_height - QUEUE_SIZE_HEIGHT as f64 / 2.0; queue_position.replace((real_x.round() as u32, real_y.round() as u32)); update_target_marker(&target_marker, &width, &height, &video, &queue_position); + spawn_local(async move { manual_update_tx.send(()).await.unwrap() }); }) }; video_click_listener.forget(); @@ -146,7 +153,9 @@ async fn do_boot() { let also_video = video.clone(); let canvas = canvas.clone(); let queue_position = queue_position.clone(); + let manual_update_tx = manual_update_tx.clone(); gloo::events::EventListener::new(&video, "canplay", move |_event| { + let mut manual_update_tx = manual_update_tx.clone(); let video = also_video.clone(); let _ = video.play().unwrap(); if !streaming.get() { @@ -156,16 +165,21 @@ async fn do_boot() { canvas.set_attribute("height", &format!("{}", height.get())); streaming.set(true); update_target_marker(&target_marker, &width, &height, &video, &queue_position); + spawn_local(async move { manual_update_tx.send(()).await.unwrap() }); } }) }; video_play_listener.forget(); - let update_interval = gloo::timers::callback::Interval::new(2_000, move || { + let update_stream = futures::stream_select!( + gloo::timers::future::IntervalStream::new(2_000), + manual_update_rx, + ); + let update_future = update_stream.for_each(move |_| { let history = history.clone(); let queue_position = queue_position.clone(); if !streaming.get() { - return; + return futures::future::ready(()); } let context = canvas.get_context("2d").unwrap().unwrap(); let context: CanvasRenderingContext2d = context.dyn_into().unwrap(); @@ -247,8 +261,9 @@ async fn do_boot() { .unwrap(), ) .unwrap(); + futures::future::ready(()) }); - update_interval.forget(); + spawn_local(update_future); } #[wasm_bindgen] 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