EVAx10_Video_analitics/tests/PersonTrackerTest.java

144 lines
6.2 KiB
Java

package com.it_screen.edgevision;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
/** Temporal regression cases with labelled person detections at a simulated 5 FPS. */
public final class PersonTrackerTest {
private static final long STEP = 200;
private static final class Scenario {
long step = STEP;
final PersonTracker tracker = new PersonTracker();
final AudienceAggregator totals = new AudienceAggregator();
final Map<String, Integer> lastIds = new HashMap<>();
long time;
int switches;
void frame(Labelled... people) {
List<PersonTracker.Event> events = tracker.update(
Arrays.stream(people).map(p -> p.detection).collect(java.util.stream.Collectors.toList()),
time, 1000, 10000);
for (PersonTracker.Event event : events) {
if (event.state == PersonTracker.State.ACTIVE && event.previous != PersonTracker.State.TEMPORARILY_LOST)
totals.countVisit(event.track.analytics);
}
for (Labelled person : people) {
PersonTracker.Track assigned = tracker.tracks().stream()
.filter(track -> track.observed == person.detection.box && track.lastSeen == time)
.findFirst().orElse(null);
if (assigned == null) continue;
Integer previous = lastIds.put(person.truth, assigned.id);
if (previous != null && previous != assigned.id) switches++;
}
time += step;
}
void repeat(int count, Labelled... people) {
for (int i = 0; i < count; i++) frame(people);
}
void check(String name, int visits, int losses, int restores, int closed, int expectedSwitches) {
System.out.printf("%s | %d/%d | %d/%d | %d/%d | %d/%d | %d/%d%n", name,
totals.visits, visits, tracker.lostEvents, losses, tracker.restored, restores,
tracker.closed, closed, switches, expectedSwitches);
if (totals.visits != visits || tracker.lostEvents != losses || tracker.restored != restores ||
tracker.closed != closed || switches != expectedSwitches) throw new AssertionError(name);
}
}
private static final class Labelled {
final String truth;
final PersonTracker.Detection detection;
Labelled(PersonTracker.Box box, float score) {
truth = "A";
detection = new PersonTracker.Detection(box, score);
}
Labelled(String truth, double center, float score) {
this.truth = truth;
float x = (float) center;
detection = new PersonTracker.Detection(new PersonTracker.Box(x-.095f,.15f,x+.095f,.85f),score);
}
}
private static Labelled a(double x) { return new Labelled("A", x, .92f); }
private static Labelled b(double x) { return new Labelled("B", x, .90f); }
public static void main(String[] args) {
System.out.println("Case | Visits act/exp | Lost act/exp | Restored act/exp | Closed act/exp | Switches act/exp");
stationary("Stationary five minutes", 1500, .92f);
stationary("Turn head (weaker body observations)", 25, .32f);
stationary("Look down (weaker body observations)", 25, .29f);
stationary("Cover face (weaker body observations)", 25, .27f);
Scenario moving = new Scenario();
for (int i = 0; i < 39; i++) moving.frame(a(.18 + i*.015));
moving.check("Traverse camera", 1,0,0,0,0);
Scenario approach = new Scenario();
approach.step = 333;
for (int i = 0; i < 31; i++) {
float distance = 5 - 4 * i / 30f;
float halfWidth = .22f / distance, personHeight = .82f / distance;
approach.frame(new Labelled(new PersonTracker.Box(.48f - halfWidth, .93f - personHeight,
.48f + halfWidth, .93f), .90f));
}
approach.check("Approach from 5 m to 1 m at 3 FPS", 1, 0, 0, 0, 0);
Scenario crossing = new Scenario();
crossing.repeat(8, a(.2), b(.8));
for (int i = 0; i <= 24; i++) {
double x = .20 + .025*i, y = .80 - .025*i;
if (i == 12) crossing.frame(a(x));
else if (i % 2 == 0) crossing.frame(a(x),b(y));
else crossing.frame(b(y),a(x));
}
crossing.check("Crossing with one hidden observation", 2,1,1,0,0);
Scenario blocked = new Scenario();
blocked.repeat(10,a(.23),b(.72));
blocked.repeat(14,a(.23));
blocked.repeat(8,a(.23),b(.72));
blocked.check("Temporary occlusion 2.8 s", 2,1,1,0,0);
Scenario returner = new Scenario();
returner.repeat(10,a(.45));
returner.repeat(25);
returner.repeat(8,a(.49));
returner.check("Exit 5 s and return nearby", 1,1,1,0,0);
Scenario exits = new Scenario();
exits.repeat(10,a(.45));
exits.repeat(55);
exits.check("Exit permanently", 1,1,0,1,0);
exits.repeat(10,new Labelled("new visitor after timeout", .45,.90f));
exits.check("Fresh visit after timeout", 2,1,0,1,0);
Scenario entrants = new Scenario();
entrants.repeat(9,a(.15),b(.47),new Labelled("C",.78,.89f));
entrants.check("Three simultaneous entrants", 3,0,0,0,0);
Scenario analytics = new Scenario();
analytics.repeat(8,a(.4));
VisitorAnalytics stats = analytics.tracker.tracks().get(0).analytics;
if (stats.countedAge != 6 || stats.countedGender != 2) throw new AssertionError("unstable initial buckets");
for (int age : new int[]{32, 34, 80, 33, 24}) {
stats.sample(age, "Female", .90f, analytics.time);
analytics.totals.observe(stats);
}
analytics.check("Demographic changes", 1,0,0,0,0);
if (analytics.totals.ages[2] != 1 || analytics.totals.genders[1] != 1)
throw new AssertionError("stable demographic buckets");
}
private static void stationary(String name, int frames, float score) {
Scenario example = new Scenario();
example.repeat(8,a(.5));
example.repeat(frames,new Labelled("A",.5,score));
example.check(name,1,0,0,0,0);
}
}