csv writing

This commit is contained in:
Scott Pruett 2022-05-22 19:01:24 -04:00
parent 9aaff38d96
commit 601eb8d942
8 changed files with 31 additions and 10 deletions

View File

@ -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))

View File

@ -12,6 +12,7 @@ pub struct Config {
pub ocr_server_endpoint: String,
pub filter_threshold: Option<f64>,
pub use_ocr_cache: Option<bool>,
pub ocr_interval_ms: Option<u64>,
}
impl Config {

View File

@ -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/"
}

View File

@ -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)));
}
}

View File

@ -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 {

View File

@ -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)]

View File

@ -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,

View File

@ -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