Skip to content

Commit

Permalink
Update properties when not running, and cleanup non-existant properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Apr 4, 2022
1 parent e616b72 commit 912801a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
31 changes: 22 additions & 9 deletions ofps-suite/src/app/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,24 @@ impl MotionDetectionState {
self.decoder_times.push(timer.elapsed());
out.frames = self.frames;

let mut detector_properties = out.detector_properties.take().unwrap_or_default();
let mut decoder_properties = out.decoder_properties.take().unwrap_or_default();

transfer_props(
self.detector.props_mut(),
&settings.detector_properties,
&mut out.detector_properties,
&mut detector_properties,
);

transfer_props(
self.decoder.props_mut(),
&settings.decoder_properties,
&mut out.decoder_properties,
&mut decoder_properties,
);

out.detector_properties = Some(detector_properties);
out.decoder_properties = Some(decoder_properties);

let timer = Instant::now();
out.motion = self.detector.detect_motion(&out.motion_vectors);
self.detector_times.push(timer.elapsed());
Expand Down Expand Up @@ -183,8 +189,8 @@ struct MotionDetectionOutput {
motion_ranges: Vec<(usize, usize)>,
detector_times: Vec<Duration>,
decoder_times: Vec<Duration>,
decoder_properties: BTreeMap<String, Property>,
detector_properties: BTreeMap<String, Property>,
decoder_properties: Option<BTreeMap<String, Property>>,
detector_properties: Option<BTreeMap<String, Property>>,
}

impl MotionDetectionOutput {
Expand Down Expand Up @@ -324,7 +330,7 @@ impl OfpsCtxApp for MotionDetectionApp {
ui,
"decoder_properties",
&mut self.settings.worker.decoder_properties,
Some(&state.decoder_properties),
state.decoder_properties.as_ref(),
);

std::mem::drop(app_state_lock);
Expand Down Expand Up @@ -497,8 +503,15 @@ impl OfpsCtxApp for MotionDetectionApp {

ui.label("Motion:");

if let Some((motion, _)) = &state.motion {
ui.label(format!("{} blocks", motion));
if let Some((motion, mf)) = &state.motion {
let (w, h) = mf.dim();
let sz = w * h;
let motion = if sz == 0 {
0.0
} else {
*motion as f32 / sz as f32
};
ui.label(format!("{motion}%"));
} else {
ui.label("None");
}
Expand All @@ -510,7 +523,7 @@ impl OfpsCtxApp for MotionDetectionApp {
detector_props_ui(
ui,
&mut self.settings,
output.as_ref().map(|a| &a.detector_properties),
output.as_ref().and_then(|a| a.detector_properties.as_ref()),
);

ui.label("Dominant motion:");
Expand Down Expand Up @@ -564,7 +577,7 @@ impl OfpsCtxApp for MotionDetectionApp {
detector_props_ui(
ui,
&mut self.settings,
output.as_ref().map(|a| &a.detector_properties),
output.as_ref().and_then(|a| a.detector_properties.as_ref()),
);

false
Expand Down
22 changes: 18 additions & 4 deletions ofps-suite/src/app/tracking/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::utils::{
camera_controller::CameraController,
perf_stats::{perf_stats_options, perf_stats_windows, DrawPerfStats},
properties::{properties_grid_ui, properties_ui},
properties::{properties_grid_ui, properties_ui, transfer_props},
ui_misc::{jlabel, realtime_processing, realtime_processing_fn, transparent_windows},
};
use super::widgets::{
Expand Down Expand Up @@ -545,7 +545,7 @@ impl OfpsCtxApp for MotionTrackingApp {
properties_ui(
ui,
&mut settings.decoder_properties,
state.as_ref().map(|s| &s.decoder_properties),
state.as_ref().and_then(|s| s.decoder_properties.as_ref()),
);

clicked_close
Expand Down Expand Up @@ -674,11 +674,25 @@ impl OfpsCtxApp for MotionTrackingApp {
);
ui.end_row();

// If there is no app state, then update the properties here, as
// opposed to the worker.
if app_state_lock.is_none() {
if let Ok(Some(est)) = est.lock().as_deref_mut() {
transfer_props(
est.props_mut(),
&settings.properties.clone(),
&mut settings.properties,
);
}
}

let props =
app_state_lock.as_ref().and_then(|s| s.estimators.get(i));

let props =
props.map(Option::as_ref).flatten().map(|s| &s.properties);
let props = props
.map(Option::as_ref)
.flatten()
.and_then(|s| s.properties.as_ref());

properties_ui(ui, &mut settings.properties, props);

Expand Down
16 changes: 8 additions & 8 deletions ofps-suite/src/app/tracking/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct EstimatorState {
pub times: Vec<Duration>,
pub layered_frames: Vec<(usize, Arc<Mutex<FrameState>>)>,
pub clear_count: usize,
pub properties: BTreeMap<String, Property>,
pub properties: Option<BTreeMap<String, Property>>,
}

impl EstimatorState {
Expand Down Expand Up @@ -332,7 +332,7 @@ impl TrackingState {
let (motion_vectors, frame, frame_height, time) =
match self.decoder.results.as_mut().unwrap().recv() {
Ok(DecoderResult { frame, time, props }) => {
out.decoder_properties = props;
out.decoder_properties = Some(props);
match frame {
Ok((mv, f, fh)) => (mv, f, fh, time),
Err(_) => return false,
Expand All @@ -355,11 +355,11 @@ impl TrackingState {
.par_bridge()
.for_each(|(estimator_state, (estimator, _, est_settings))| {
if let Ok(Some(estimator)) = estimator.lock().as_deref_mut() {
transfer_props(
estimator.props_mut(),
&est_settings.properties,
&mut estimator_state.properties,
);
let mut props = estimator_state.properties.take().unwrap_or_default();

transfer_props(estimator.props_mut(), &est_settings.properties, &mut props);

estimator_state.properties = Some(props);

let timer = Instant::now();
if let Ok((frot, tr)) = estimator.estimate(&motion_vectors, camera, None) {
Expand Down Expand Up @@ -425,7 +425,7 @@ impl Workable for TrackingState {
pub struct TrackingOutput {
pub estimators: Vec<Option<EstimatorState>>,
pub decoder_times: Vec<Duration>,
pub decoder_properties: BTreeMap<String, Property>,
pub decoder_properties: Option<BTreeMap<String, Property>>,
}

#[derive(Clone)]
Expand Down
4 changes: 4 additions & 0 deletions ofps-suite/src/app/utils/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub fn properties_ui(
settings_properties: &mut BTreeMap<String, Property>,
in_properties: Option<&BTreeMap<String, Property>>,
) {
if let Some(in_properties) = in_properties {
settings_properties.clear();
}

// Add any properties available
if let Some(in_properties) = in_properties {
for (n, p) in in_properties {
Expand Down

0 comments on commit 912801a

Please sign in to comment.