EVAx10_Video_analitics/src/VisitorSessions.java

161 lines
5.6 KiB
Java

package com.it_screen.edgevision;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/** Owns visitor sessions independently of the lifetime of the camera's geometric tracks. */
final class VisitorSessions<Observation> {
static final int DEFAULT_TIMEOUT_MINUTES = 30;
private final ReIdProvider<Observation> provider;
private final Map<Long, Session> open = new HashMap<>();
private final Map<Integer, Session> byTrack = new HashMap<>();
private final Set<Integer> unresolved = new HashSet<>();
private long nextId = 1;
private long totalCreated, repeated, uncertain;
VisitorSessions(ReIdProvider<Observation> provider) { this.provider = provider; }
static final class Candidate {
final long id;
final long lastSeenMs;
Candidate(long id, long lastSeenMs) {
this.id = id;
this.lastSeenMs = lastSeenMs;
}
}
enum Kind { CREATED, UNCERTAIN, DEFERRED, RECONNECTED, CONTINUED }
static final class Assignment {
final long visitorId;
final Kind kind;
Assignment(long visitorId, Kind kind) { this.visitorId = visitorId; this.kind = kind; }
}
private static final class Session {
final long id;
long lastSeen;
Integer track;
boolean visible;
Session(long id, long now) { this.id = id; lastSeen = now; }
}
boolean matchingEnabled() { return provider.enabled(); }
int openCount() { return open.size(); }
long totalCreated() { return totalCreated; }
long repeatedCount() { return repeated; }
long uncertainCount() { return uncertain; }
boolean needsObservation(int trackId) {
Session session = byTrack.get(trackId);
return provider.enabled() && (session == null || !provider.enrollmentComplete(session.id));
}
void expire(long now, long timeoutMs) {
Iterator<Session> items = open.values().iterator();
while (items.hasNext()) {
Session session = items.next();
if (now - session.lastSeen < timeoutMs) continue;
detach(session);
items.remove();
provider.expired(session.id);
}
}
Assignment observe(int trackId, long now, Observation observation) {
return observe(trackId, now, observation, false);
}
Assignment observe(int trackId, long now, Observation observation, boolean awaitMoreEvidence) {
Session session = byTrack.get(trackId);
Kind kind = Kind.CONTINUED;
if (session == null) {
// No timer, spatial match, or "only candidate" fallback can identify a visitor.
long match = 0;
if (provider.enabled()) {
List<Candidate> candidates = new ArrayList<>();
for (Session candidate : open.values()) {
if (!candidate.visible) candidates.add(new Candidate(candidate.id, candidate.lastSeen));
}
if (awaitMoreEvidence && observation == null) return new Assignment(0, Kind.DEFERRED);
if (observation == null) match = ReIdProvider.UNCERTAIN;
else if (!candidates.isEmpty()) {
match = provider.match(observation, Collections.unmodifiableList(candidates));
Session candidate = open.get(match);
if (candidate != null && !candidate.visible) {
session = candidate;
detach(session);
} else if (awaitMoreEvidence) return new Assignment(0, Kind.DEFERRED);
}
}
if (session != null) {
kind = Kind.RECONNECTED;
repeated++;
} else if (match == ReIdProvider.UNCERTAIN) {
if (!unresolved.add(trackId)) return new Assignment(0, Kind.DEFERRED);
uncertain++;
return new Assignment(0, Kind.UNCERTAIN);
} else kind = Kind.CREATED;
unresolved.remove(trackId);
if (session == null) {
session = new Session(nextId++, now);
open.put(session.id, session);
totalCreated++;
}
session.track = trackId;
byTrack.put(trackId, session);
}
session.visible = true;
session.lastSeen = now;
if (provider.enabled()) provider.observed(session.id, observation);
return new Assignment(session.id, kind);
}
void reserveObservedTrack(int trackId) {
Session session = byTrack.get(trackId);
if (session != null) session.visible = true;
}
void temporarilyLost(int trackId) {
Session session = byTrack.get(trackId);
if (session != null) session.visible = false;
}
void trackClosed(int trackId) {
unresolved.remove(trackId);
Session session = byTrack.get(trackId);
if (session != null) detach(session);
}
void releaseTracks() {
unresolved.clear();
for (Session session : open.values()) detach(session);
}
void clear() {
closeSessions();
totalCreated = 0;
repeated = 0;
uncertain = 0;
}
void closeSessions() {
unresolved.clear();
for (Session session : open.values()) provider.expired(session.id);
byTrack.clear();
open.clear();
}
private void detach(Session session) {
if (session.track != null) byTrack.remove(session.track);
session.track = null;
session.visible = false;
}
}