-
Notifications
You must be signed in to change notification settings - Fork 14
/
incremental_track_build.rs
168 lines (145 loc) · 5.16 KB
/
incremental_track_build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use similari::distance::euclidean;
use similari::examples::{BoxGen2, FeatGen2};
use similari::prelude::*;
use similari::track::{
MetricOutput, MetricQuery, NoopLookup, Observation, ObservationAttributes, ObservationMetric,
ObservationMetricOk, ObservationsDb, TrackAttributes, TrackAttributesUpdate, TrackStatus,
};
use similari::utils::bbox::BoundingBox;
use similari::voting::topn::TopNVoting;
use similari::voting::Voting;
use std::thread;
use std::time::Duration;
const FEAT0: u64 = 0;
const MAX_DIST: f32 = 0.1;
#[derive(Debug, Clone, Default)]
struct BBoxAttributes {
bboxes: Vec<BoundingBox>,
}
#[derive(Clone, Debug)]
struct BBoxAttributesUpdate {
bbox: BoundingBox,
}
impl TrackAttributesUpdate<BBoxAttributes> for BBoxAttributesUpdate {
fn apply(&self, attrs: &mut BBoxAttributes) -> anyhow::Result<()> {
attrs.bboxes.push(self.bbox);
Ok(())
}
}
impl TrackAttributes<BBoxAttributes, f32> for BBoxAttributes {
type Update = BBoxAttributesUpdate;
type Lookup = NoopLookup<BBoxAttributes, f32>;
fn compatible(&self, _other: &BBoxAttributes) -> bool {
true
}
fn merge(&mut self, other: &BBoxAttributes) -> anyhow::Result<()> {
self.bboxes.extend_from_slice(&other.bboxes);
Ok(())
}
fn baked(&self, _observations: &ObservationsDb<f32>) -> anyhow::Result<TrackStatus> {
Ok(TrackStatus::Ready)
}
}
#[derive(Clone, Default)]
pub struct TrackMetric;
impl ObservationMetric<BBoxAttributes, f32> for TrackMetric {
fn metric(&self, mq: &MetricQuery<BBoxAttributes, f32>) -> MetricOutput<f32> {
let (e1, e2) = (mq.candidate_observation, mq.track_observation);
Some((
f32::calculate_metric_object(&e1.attr().as_ref(), &e2.attr().as_ref()),
match (e1.feature().as_ref(), e2.feature().as_ref()) {
(Some(x), Some(y)) => Some(euclidean(x, y)),
_ => None,
},
))
}
fn optimize(
&mut self,
_feature_class: u64,
_merge_history: &[u64],
_attrs: &mut BBoxAttributes,
observations: &mut Vec<Observation<f32>>,
_prev_length: usize,
_is_merge: bool,
) -> anyhow::Result<()> {
observations.reverse();
observations.truncate(5);
observations.reverse();
Ok(())
}
fn postprocess_distances(
&self,
unfiltered: Vec<ObservationMetricOk<f32>>,
) -> Vec<ObservationMetricOk<f32>> {
unfiltered
.into_iter()
.filter(|r| r.feature_distance.unwrap() < MAX_DIST)
.collect()
}
}
fn main() {
let mut store = TrackStoreBuilder::default()
.default_attributes(BBoxAttributes::default())
.metric(TrackMetric::default())
.notifier(NoopNotifier)
.build();
let voting: TopNVoting<f32> = TopNVoting::new(1, MAX_DIST, 1);
let feature_drift = 0.01;
let pos_drift = 5.0;
let box_drift = 2.0;
let mut p1 = FeatGen2::new(0.0, 0.0, feature_drift);
let mut b1 = BoxGen2::new(100.0, 100.0, 10.0, 15.0, pos_drift, box_drift);
let mut p2 = FeatGen2::new(1.0, 1.0, feature_drift);
let mut b2 = BoxGen2::new(10.0, 10.0, 12.0, 18.0, pos_drift, box_drift);
for _ in 0..10 {
let (obj1f, obj1b) = (p1.next().unwrap(), b1.next().unwrap());
let obj1t = store
.new_track_random_id()
.observation(
ObservationBuilder::new(FEAT0)
.observation_attributes(obj1f.attr().unwrap())
.observation(obj1f.feature().as_ref().unwrap().clone())
.track_attributes_update(BBoxAttributesUpdate { bbox: obj1b })
.build(),
)
.build()
.unwrap();
let (obj2f, obj2b) = (p2.next().unwrap(), b2.next().unwrap());
let obj2t = store
.new_track_random_id()
.observation(
ObservationBuilder::new(FEAT0)
.observation_attributes(obj2f.attr().unwrap())
.observation(obj2f.feature().as_ref().unwrap().clone())
.track_attributes_update(BBoxAttributesUpdate { bbox: obj2b })
.build(),
)
.build()
.unwrap();
thread::sleep(Duration::from_millis(2));
for t in [obj1t, obj2t] {
let search_track = t.clone();
let (dists, errs) = store.foreign_track_distances(vec![search_track], FEAT0, false);
assert!(errs.all().is_empty());
let winners = voting.winners(dists);
if winners.is_empty() {
store.add_track(t).unwrap();
} else {
store
.merge_external(
winners.get(&t.get_track_id()).unwrap()[0].winner_track,
&t,
None,
false,
)
.unwrap();
}
}
}
let tracks = store.find_usable();
for (t, _) in tracks {
let t = store.fetch_tracks(&[t]);
eprintln!("Track id: {}", t[0].get_track_id());
eprintln!("Boxes: {:#?}", t[0].get_attributes());
}
}