store compressed lap images

This commit is contained in:
Scott Pruett 2022-06-01 00:13:13 -04:00
parent a6d5af8bbd
commit b69a7dddb6
4 changed files with 15 additions and 9 deletions

View File

@ -12,7 +12,7 @@ use scrap::{Capturer, Display};
use crate::{ use crate::{
capture, capture,
config::{Config, LearnedConfig}, config::{Config, LearnedConfig},
image_processing::{self, extract_and_filter, hash_image, Region}, image_processing::{self, extract_and_filter, hash_image, Region, to_png_bytes},
ocr, ocr,
state::{AppState, DebugOcrFrame, LapState, RaceState, SharedAppState}, state::{AppState, DebugOcrFrame, LapState, RaceState, SharedAppState},
}; };
@ -111,7 +111,7 @@ fn handle_new_frame(state: &mut AppState, frame: LapState, image: RgbImage) {
if let Some(lap) = &merged.lap { if let Some(lap) = &merged.lap {
merged.lap = Some(lap - 1); merged.lap = Some(lap - 1);
} }
merged.screenshot = Some(image); merged.screenshot = Some(to_png_bytes(&image));
if let Some(race) = state.current_race.as_mut() { if let Some(race) = state.current_race.as_mut() {
if let Some(prev_lap) = race.laps.last() { if let Some(prev_lap) = race.laps.last() {

View File

@ -1,4 +1,4 @@
use image::{codecs::png::PngEncoder, ColorType, ImageEncoder, Rgb, RgbImage}; use image::{codecs::png::{PngEncoder, PngDecoder}, ColorType, ImageEncoder, Rgb, RgbImage, DynamicImage};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -74,6 +74,11 @@ pub fn to_png_bytes(image: &RgbImage) -> Vec<u8> {
buffer buffer
} }
pub fn from_png_bytes(bytes: &[u8]) -> RgbImage {
let image = image::load_from_memory(bytes).expect("expected valid image");
image.to_rgb8()
}
pub fn hash_image(image: &RgbImage) -> String { pub fn hash_image(image: &RgbImage) -> String {
let hasher = img_hash::HasherConfig::new().hash_alg(img_hash::HashAlg::Mean).hash_size(16, 16).to_hasher(); let hasher = img_hash::HasherConfig::new().hash_alg(img_hash::HashAlg::Mean).hash_size(16, 16).to_hasher();
let have_to_use_other_image_library_version = let have_to_use_other_image_library_version =

View File

@ -24,7 +24,7 @@ use eframe::{
epaint::Color32, epaint::Color32,
}; };
use egui_extras::RetainedImage; use egui_extras::RetainedImage;
use image_processing::to_png_bytes; use image_processing::{to_png_bytes, from_png_bytes};
use state::{AppState, DebugOcrFrame, LapState, OcrCache, RaceState, SharedAppState}; use state::{AppState, DebugOcrFrame, LapState, OcrCache, RaceState, SharedAppState};
use stats_writer::export_race_stats; use stats_writer::export_race_stats;
@ -296,12 +296,13 @@ fn open_debug_lap(
learned: Arc<LearnedConfig>, learned: Arc<LearnedConfig>,
ocr_cache: Arc<OcrCache>, ocr_cache: Arc<OcrCache>,
) { ) {
if let Some(screenshot) = &lap.screenshot { if let Some(screenshot_bytes) = &lap.screenshot {
let ocr_results = ocr::ocr_all_regions(screenshot, config.clone(), learned, ocr_cache); let screenshot = from_png_bytes(screenshot_bytes);
let ocr_results = ocr::ocr_all_regions(&screenshot, config.clone(), learned, ocr_cache);
let debug_lap = DebugLap { let debug_lap = DebugLap {
screenshot: RetainedImage::from_image_bytes("debug-lap", &to_png_bytes(screenshot)) screenshot: RetainedImage::from_image_bytes("debug-lap", &to_png_bytes(&screenshot))
.unwrap(), .unwrap(),
debug_regions: save_frames_from(screenshot, &*config, &ocr_results), debug_regions: save_frames_from(&screenshot, &*config, &ocr_results),
}; };
ui_state.debug_lap = Some(debug_lap); ui_state.debug_lap = Some(debug_lap);

View File

@ -19,7 +19,7 @@ pub struct LapState {
pub lap_time: Option<Duration>, pub lap_time: Option<Duration>,
pub striked: bool, pub striked: bool,
pub screenshot: Option<RgbImage>, pub screenshot: Option<Vec<u8>>,
pub debug: bool, pub debug: bool,
} }