use crate::{MarkerBoardDetectionResult, MarkerBoardDetector, MarkerBoardParams};
use calib_targets_chessboard::ChessCorner;
use calib_targets_core::io::{self, IoError};
use calib_targets_core::{DetectorConfig, GridAlignment, TargetDetection};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
pub type MarkerBoardIoError = IoError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarkerBoardDetectConfig {
pub image_path: String,
#[serde(default)]
pub output_path: Option<String>,
#[serde(default)]
pub chess: DetectorConfig,
pub marker: MarkerBoardParams,
}
impl MarkerBoardDetectConfig {
pub fn load_json(path: impl AsRef<Path>) -> Result<Self, MarkerBoardIoError> {
io::load_json(path)
}
pub fn write_json(&self, path: impl AsRef<Path>) -> Result<(), MarkerBoardIoError> {
io::write_json(self, path)
}
pub fn output_path(&self) -> PathBuf {
self.output_path
.as_ref()
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("marker_board_detect_report.json"))
}
pub fn build_detector(&self) -> MarkerBoardDetector {
let params = self.marker.clone();
MarkerBoardDetector::new(params)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarkerBoardDetectReport {
pub image_path: String,
pub config_path: String,
pub num_raw_corners: usize,
pub raw_corners: Vec<ChessCorner>,
#[serde(default)]
pub detection: Option<TargetDetection>,
#[serde(default)]
pub alignment: Option<GridAlignment>,
#[serde(default)]
pub error: Option<String>,
}
impl MarkerBoardDetectReport {
pub fn new(
cfg: &MarkerBoardDetectConfig,
config_path: &Path,
raw_corners: Vec<ChessCorner>,
) -> Self {
Self {
image_path: cfg.image_path.clone(),
config_path: config_path.to_string_lossy().into_owned(),
num_raw_corners: raw_corners.len(),
raw_corners,
detection: None,
alignment: None,
error: None,
}
}
pub fn set_detection(&mut self, res: MarkerBoardDetectionResult) {
self.detection = Some(res.target_detection());
self.alignment = res.alignment;
self.error = None;
}
pub fn load_json(path: impl AsRef<Path>) -> Result<Self, MarkerBoardIoError> {
io::load_json(path)
}
pub fn write_json(&self, path: impl AsRef<Path>) -> Result<(), MarkerBoardIoError> {
io::write_json(self, path)
}
}