From 601eb8d94295e5809002eb73f055768ed9757f63 Mon Sep 17 00:00:00 2001 From: Scott Pruett Date: Sun, 22 May 2022 19:01:24 -0400 Subject: [PATCH] csv writing --- scale_config.py | 1 + src/config.rs | 1 + src/configs/config.default.json | 10 +++++++++- src/control_loop.rs | 4 ++-- src/image_processing.rs | 2 +- src/ocr.rs | 2 +- src/state.rs | 13 +++++++++++++ src/stats_writer.rs | 8 +++----- 8 files changed, 31 insertions(+), 10 deletions(-) diff --git a/scale_config.py b/scale_config.py index 28ba38d..5099b3d 100644 --- a/scale_config.py +++ b/scale_config.py @@ -33,6 +33,7 @@ def main(): config = json.load(open(args.config, 'r')) for region in config['ocr_regions']: scale_region(region, from_resolution, to_resolution) + scale_region(config['track_region'], from_resolution, to_resolution) print(json.dumps(config, indent=4)) diff --git a/src/config.rs b/src/config.rs index 4ef6612..3e17601 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,7 @@ pub struct Config { pub ocr_server_endpoint: String, pub filter_threshold: Option, pub use_ocr_cache: Option, + pub ocr_interval_ms: Option, } impl Config { diff --git a/src/configs/config.default.json b/src/configs/config.default.json index e31994e..8a63bb6 100644 --- a/src/configs/config.default.json +++ b/src/configs/config.default.json @@ -2,7 +2,7 @@ "ocr_regions": [ { "name": "lap", - "x": 2290, + "x": 2300, "y": 46, "width": 145, "height": 90 @@ -43,5 +43,13 @@ "height": 43 } ], + "track_region": { + "name": "track", + "x": 2020, + "y": 1030, + "width": 540, + "height": 410, + "threshold": 0.85 + }, "ocr_server_endpoint": "https://tesserver.spruett.dev/" } \ No newline at end of file diff --git a/src/control_loop.rs b/src/control_loop.rs index dbd8865..2271587 100644 --- a/src/control_loop.rs +++ b/src/control_loop.rs @@ -13,7 +13,7 @@ use crate::{ capture, image_processing::{self, hash_image, Region, extract_and_filter}, ocr, - state::{AppState, DebugOcrFrame, ParsedFrame, RaceState, SharedAppState}, config::Config, + state::{AppState, DebugOcrFrame, ParsedFrame, RaceState, SharedAppState}, }; fn is_finished_lap(state: &AppState, frame: &ParsedFrame) -> bool { @@ -172,6 +172,6 @@ pub fn run_control_loop(state: SharedAppState) { if let Err(e) = run_loop_once(&mut capturer, &state) { eprintln!("Error in control loop: {:?}", e) } - thread::sleep(Duration::from_millis(500)); + thread::sleep(Duration::from_millis(state.lock().unwrap().config.ocr_interval_ms.unwrap_or(500))); } } diff --git a/src/image_processing.rs b/src/image_processing.rs index 7188da3..9867da6 100644 --- a/src/image_processing.rs +++ b/src/image_processing.rs @@ -1,7 +1,7 @@ use image::{codecs::png::PngEncoder, ColorType, ImageEncoder, Rgb, RgbImage}; use serde::{Deserialize, Serialize}; -use crate::config::Config; + #[derive(Clone, Deserialize, Serialize)] pub struct Region { diff --git a/src/ocr.rs b/src/ocr.rs index 43d4b02..092c164 100644 --- a/src/ocr.rs +++ b/src/ocr.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use crate::{ config::{Config, LearnedConfig}, - image_processing::{extract_region, filter_to_white, hash_image, extract_and_filter}, + image_processing::{hash_image, extract_and_filter}, }; #[derive(Serialize, Deserialize, Debug)] diff --git a/src/state.rs b/src/state.rs index 4c505ef..3bd12c5 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,6 +2,7 @@ use std::{sync::{Arc, Mutex, RwLock}, time::{Duration, Instant, SystemTime}, col use egui_extras::RetainedImage; use image::RgbImage; +use time::{OffsetDateTime, format_description}; use crate::config::{Config, LearnedConfig}; @@ -72,6 +73,18 @@ pub struct RaceState { pub track: String, } +impl RaceState { + pub fn name(&self) -> String { + let race_time = self.race_time.unwrap_or_else(SystemTime::now); + let race_time: OffsetDateTime = race_time.into(); + let mut name = race_time.format(&format_description::parse("[year]-[month]-[day]-[hour]:[minute]").unwrap()).unwrap(); + if !self.track.is_empty() { + name += &format!(" ({})", self.track); + } + name + } +} + pub struct DebugOcrFrame { pub image: RetainedImage, pub rgb_image: RgbImage, diff --git a/src/stats_writer.rs b/src/stats_writer.rs index c78dc22..3264691 100644 --- a/src/stats_writer.rs +++ b/src/stats_writer.rs @@ -1,6 +1,6 @@ use std::{ io::BufWriter, - time::{Duration, Instant, SystemTime}, + time::{Duration, SystemTime}, }; use crate::state::RaceState; @@ -9,9 +9,7 @@ use anyhow::Result; use time::{OffsetDateTime, format_description}; pub fn export_race_stats(race_stats: &mut RaceState) -> Result<()> { - let race_time = race_stats.race_time.unwrap_or_else(|| SystemTime::now()); - let race_time: OffsetDateTime = race_time.into(); - let race_time: String = race_time.format(&format_description::parse("[year]-[month]-[day]-[hour]:[minute]")?)?; + let race_name = race_stats.name(); let file = std::fs::OpenOptions::new() .create(true) @@ -22,7 +20,7 @@ pub fn export_race_stats(race_stats: &mut RaceState) -> Result<()> { for lap in &race_stats.laps { csv_writer.write_record(vec![ - race_time.clone(), + race_name.clone(), race_stats.track.clone(), race_stats.car.clone(), lap.lap