From 9580bb047878c4d688be76b1b13a6700356a904e Mon Sep 17 00:00:00 2001 From: Scott Pruett Date: Fri, 3 Jun 2022 22:39:19 -0400 Subject: [PATCH] few more tests --- learned.json | 4 --- src/configs/learned.default.json | 1 - src/ocr_db.rs | 44 ++++++++++++++++++++++++----- src/test_data/test-image-4.png | Bin 0 -> 724 bytes src/test_data/test-image-blank.png | Bin 0 -> 183 bytes src/test_data/test-image-num-1.png | Bin 0 -> 322 bytes 6 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 src/test_data/test-image-4.png create mode 100644 src/test_data/test-image-blank.png create mode 100644 src/test_data/test-image-num-1.png diff --git a/learned.json b/learned.json index 23e4134..45ddc97 100644 --- a/learned.json +++ b/learned.json @@ -1,8 +1,4 @@ { - "learned_images": { - "/////8/wz/DP8Ofwx/OH8wP5A/kH+Z/5//////////8=": "47", - "//////////////////////////////////////////8=": "" - }, "learned_tracks": { "//////////+f/x//H/7f/N/A34HPn8+DD8AfwP////8=": "Magdalena Club", "/////z/wv/+/zz/vf+Z/5P/IH9gP2c+cD8CfwP////8=": "Rennvoort", diff --git a/src/configs/learned.default.json b/src/configs/learned.default.json index 3563fb1..8ecd131 100644 --- a/src/configs/learned.default.json +++ b/src/configs/learned.default.json @@ -1,4 +1,3 @@ { - "learned_images": {}, "learned_tracks": {} } \ No newline at end of file diff --git a/src/ocr_db.rs b/src/ocr_db.rs index 58d40d1..c8d694b 100644 --- a/src/ocr_db.rs +++ b/src/ocr_db.rs @@ -1,7 +1,8 @@ -use std::{collections::HashMap, sync::{RwLock}}; +use std::{collections::HashMap, sync::RwLock}; use crate::{ - config::{load_config_or_make_default, save_json_config}, ocr, + config::{load_config_or_make_default, save_json_config}, + ocr, }; use anyhow::Result; @@ -60,7 +61,10 @@ impl OcrDatabase { } pub fn learn(&self, hash: &str, val: char) { - self.learned_chars.write().unwrap().push((ImageHash::from_base64(hash).unwrap(), val)); + self.learned_chars + .write() + .unwrap() + .push((ImageHash::from_base64(hash).unwrap(), val)); } pub fn learn_phrase(&self, hashes: &[ImageHash], phrase: &str) -> Result<()> { @@ -83,17 +87,43 @@ impl OcrDatabase { pub fn ocr_char(&self, hash: &ImageHash) -> Option { let state = self.learned_chars.read().unwrap(); - let (_, c) = state.iter().min_by_key(|(learned_hash, _)| hash.dist(learned_hash))?; + let (_, c) = state + .iter() + .min_by_key(|(learned_hash, _)| hash.dist(learned_hash))?; Some(*c) } pub fn ocr_hashes(&self, hashes: &[ImageHash]) -> String { - let buffer: String = hashes.iter().filter_map(|hash| self.ocr_char(hash)).collect(); - buffer.trim_end_matches(|c: char| !c.is_alphanumeric()).to_owned() + let buffer: String = hashes + .iter() + .filter_map(|hash| self.ocr_char(hash)) + .collect(); + buffer + .trim_end_matches(|c: char| !c.is_alphanumeric()) + .to_owned() } - + pub fn ocr_image(&self, image: &RgbImage) -> String { let hashes = ocr::compute_box_hashes(image); self.ocr_hashes(&hashes) } } + +#[test] +fn test_ocr() { + let raw: RawOcrDatabase = + serde_json::from_str(include_str!("configs/ocr.default.json")).unwrap(); + let db: OcrDatabase = (&raw).into(); + + let image = image::load_from_memory(include_bytes!("test_data/test-image-3.png")).unwrap().to_rgb8(); + assert_eq!(db.ocr_image(&image), "00:30.625"); + + let image = image::load_from_memory(include_bytes!("test_data/test-image-4.png")).unwrap().to_rgb8(); + assert_eq!(db.ocr_image(&image), "00:20.296"); + + let image = image::load_from_memory(include_bytes!("test_data/test-image-num-1.png")).unwrap().to_rgb8(); + assert_eq!(db.ocr_image(&image), "1"); + + let image = image::load_from_memory(include_bytes!("test_data/test-image-blank.png")).unwrap().to_rgb8(); + assert_eq!(db.ocr_image(&image), ""); +} diff --git a/src/test_data/test-image-4.png b/src/test_data/test-image-4.png new file mode 100644 index 0000000000000000000000000000000000000000..1a28c21cb779299e2b76a4ee6c5f9348f6166e92 GIT binary patch literal 724 zcmeAS@N?(olHy`uVBq!ia0vp^+kse{g9%7(UD5Y}fq`kZr;B4q#hkY{9`;Eq@~}Ku z_UiZkBCD$&9Er)&Wz+A)KAYT^*TbvxwO?cX_uqfLRlZ2qD;bt{KDnYgDR{bvU#yZP zob!(VpI_D9IEmSh@~f&tuWjH9nI^SLOLbkMtjF|Q8ky^^@SXen?H1qNvMqwApBY@T z6WhoYwXiqB?6{lO9WC=4$ximCIlefa_I2Lye@DvxBF?^!i+8h=v&7~*{?m~RmS~PF z=PK-ZH--CWp3x(T{5ee(W#@OkSKVBE{?WnPMkiXhp6za0necbPDbv&Gck+&Kq_2&X zd2}dOeOHTne`rzF_V^=o^b3Y-P(r_mwgcYP~cFt?eo0q@6NG8 z_gBuXeb@T>%}Tp#3-v6S|F3Uo-?jc`pV=kub28$2)_hdYbP#TneJTcm8eFJ;&d6@9txtaq!>-hKhHJeTB=O-Zt@R>>LFXLvPQPdo{`SW$c=LX5ukxiwakUuG#gJLAtx(J1@|K?}g)v(n%>yl&RE1$Vcclf~c(R%l09;1cr77q?4-I>NRv5k`>sV8(*&bc!I(fTEp>s5>_ nzU+0`X9`a$DCq_V=QnrpuKs}Zr_o-()XL!L>gTe~DWM4fQ0!hf literal 0 HcmV?d00001 diff --git a/src/test_data/test-image-blank.png b/src/test_data/test-image-blank.png new file mode 100644 index 0000000000000000000000000000000000000000..bdd95c66046e656816d7582539a54e7efd73ad71 GIT binary patch literal 183 zcmeAS@N?(olHy`uVBq!ia0vp^+kse{g9%7(UD5XeNY#0|IEGZrd3%YGmqCH&fWd+N z&zqUj7Wh6(s7@2BnJfNhXUj98S>73X3u(Y8WBhZJIausS)DfUd7(8A5T-G@yGywp} C>`2G} literal 0 HcmV?d00001 diff --git a/src/test_data/test-image-num-1.png b/src/test_data/test-image-num-1.png new file mode 100644 index 0000000000000000000000000000000000000000..892eb7339588061b57ec6c0ead1bc2c211d30cd2 GIT binary patch literal 322 zcmeAS@N?(olHy`uVBq!ia0vp^JwP1A!2~3q9@UIyU|{6*ba4!+nDh3oAs3UOfJ31B zp8r#mwGO)J1U)%iJz?*G^n{rrGq+xA-fjE+VUZGRhloN@g9jsa#)n(&{WEXgZ|;k^ zK644fL?#z50Zj)C#(}1^YeV%q=! literal 0 HcmV?d00001