Compare multiple face observations before counting a returning visitor

This commit is contained in:
EVA x10 Development 2026-11-25 10:33:04 +01:00
parent 5e5bd53772
commit 2595e9bc39
9 changed files with 67 additions and 34 deletions

View File

@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.it_screen.edgevision" android:versionCode="14" android:versionName="0.3.9">
package="com.it_screen.edgevision" android:versionCode="15" android:versionName="0.3.10">
<uses-sdk android:minSdkVersion="30" android:targetSdkVersion="34"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.any" android:required="true"/>

View File

@ -1,4 +1,4 @@
# EVAx10 Video analitics v0.3.9
# EVAx10 Video analitics v0.3.10
A separate native Android experiment for consenting adult volunteers.
Package `com.it_screen.edgevision`. The existing FaceAPI baseline is
@ -10,10 +10,10 @@ installable as `com.it_screen.audiencepoc`.
`./build.sh` builds the ARM64, API 30+ APK with the Windows Android SDK 35
and JDK 17 via WSL. The script pins and verifies the upstream Maven AAR.
Model assets load locally and are verified before ONNX Runtime session
creation. The signed test build is `build/EVAx10-Video-analitics-0.3.9.apk`.
creation. The signed test build is `build/EVAx10-Video-analitics-0.3.10.apk`.
```text
adb install --no-incremental -r EVAx10-Video-analitics-0.3.9.apk
adb install --no-incremental -r EVAx10-Video-analitics-0.3.10.apk
adb shell am start -W -n com.it_screen.edgevision/.MainActivity
adb logcat -s EdgeVision EdgeVisionBench
```
@ -96,14 +96,14 @@ or an Android low-memory signal is red.
### Controlled Face Re-ID PoC
Build with `./build.sh --face-reid-poc`. Install
`build/EVAx10-Video-analitics-0.3.9-Face-ReID-PoC.apk` as the separate package
`build/EVAx10-Video-analitics-0.3.10-Face-ReID-PoC.apk` as the separate package
`com.it_screen.edgevision.facereidpoc`. Start requires on-device confirmation
that all adult test participants consent. Keep other people out of the camera view.
The default build excludes the SFace weights and implementation.
The Person Tracker continues to own Tracking IDs. A new confirmed track can
reopen an unexpired Visitor Session using a normalized SFace embedding. The
PoC keeps one qualifying embedding per session in RAM, overwrites it on expiry,
PoC keeps up to five qualifying, distinct embeddings per session in RAM, overwrites them on expiry,
and clears sessions on Stop, reset or exit to the background. The camera runs
without recording. The app has no Internet permission and the SFace runtime
does not write profiles. APK updates never transfer embeddings because they
@ -112,7 +112,7 @@ are never persisted. Counts shown in the PoC refer to the current camera run.
The timeout defaults to 30 minutes from the last sighting. The initial cosine
threshold is 0.55 with a margin of 0.08 to distinguish close candidates. These
are PoC settings and require field calibration. A new confirmed track waits up
to another three seconds for a usable face; short tracks that end in that window
to five seconds for a usable face or a stronger match across multiple frames; short tracks that end in that window
may be missed. An ambiguous match or missing template creates a separate session
and may overestimate Unique Visitors. Poses, poor lighting and poor body-to-face
association can prevent a match. There is no liveness check.

View File

@ -20,7 +20,7 @@ if [[ ! -f build/ort.aar ]]; then
fi
python3 tools/check-production-assets.py
manifest=AndroidManifest.xml
apk=build/EVAx10-Video-analitics-0.3.9.apk
apk=build/EVAx10-Video-analitics-0.3.10.apk
if [[ $variant == poc ]]; then
python3 tools/check-poc-assets.py
manifest=build/AndroidManifest-face-reid-poc.xml
@ -28,7 +28,7 @@ if [[ $variant == poc ]]; then
-e 's/android:name=".MainActivity"/android:name="com.it_screen.edgevision.MainActivity"/' \
-e 's/android:label="EVAx10 Video analitics"/android:label="EVAx10 Video analitics · Face Re-ID PoC"/' \
AndroidManifest.xml > "$manifest"
apk=build/EVAx10-Video-analitics-0.3.9-Face-ReID-PoC.apk
apk=build/EVAx10-Video-analitics-0.3.10-Face-ReID-PoC.apk
fi
rm -rf build/classes build/dex
mkdir -p build/classes build/dex

View File

@ -2,6 +2,7 @@ package com.it_screen.edgevision;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -12,7 +13,14 @@ final class FaceReId implements ReIdProvider<float[]> {
private static final float AMBIGUITY_MARGIN = .08f;
private static final float[] TARGET = {38.2946f, 51.6963f, 73.5318f, 51.5014f, 56.0252f, 71.7366f,
41.5493f, 92.3655f, 70.7299f, 92.2041f};
private final Map<Long, float[]> templates = new HashMap<>();
private static final int MAX_TEMPLATES = 5;
private static final int MAX_ENROLLMENT_ATTEMPTS = 12;
private final Map<Long, Enrollment> templates = new HashMap<>();
private static final class Enrollment {
final List<float[]> samples = new ArrayList<>();
int attempts;
}
private ImageModel model;
private float threshold;
@ -73,7 +81,11 @@ final class FaceReId implements ReIdProvider<float[]> {
}
@Override public boolean enabled() { return true; }
@Override public boolean hasObservation(long visitorId) { return templates.containsKey(visitorId); }
@Override public boolean enrollmentComplete(long visitorId) {
Enrollment enrollment = templates.get(visitorId);
return enrollment != null && (enrollment.samples.size() >= MAX_TEMPLATES ||
enrollment.attempts >= MAX_ENROLLMENT_ATTEMPTS);
}
@Override public long match(float[] observation, List<VisitorSessions.Candidate> candidates) {
if (observation == null) return UNCERTAIN;
@ -81,10 +93,9 @@ final class FaceReId implements ReIdProvider<float[]> {
float best = -2, runnerUp = -2;
boolean missing = false;
for (VisitorSessions.Candidate candidate : candidates) {
float[] reference = templates.get(candidate.id);
if (reference == null) { missing = true; continue; }
float similarity = 0;
for (int i = 0; i < reference.length; i++) similarity += observation[i] * reference[i];
Enrollment enrollment = templates.get(candidate.id);
if (enrollment == null) { missing = true; continue; }
float similarity = bestSimilarity(observation, enrollment);
if (similarity > best) {
runnerUp = best;
best = similarity;
@ -96,11 +107,26 @@ final class FaceReId implements ReIdProvider<float[]> {
}
@Override public void observed(long visitorId, float[] observation) {
if (observation != null && !templates.containsKey(visitorId)) templates.put(visitorId, observation.clone());
if (observation == null || enrollmentComplete(visitorId)) return;
Enrollment enrollment = templates.computeIfAbsent(visitorId, ignored -> new Enrollment());
enrollment.attempts++;
float similarity = bestSimilarity(observation, enrollment);
if (enrollment.samples.isEmpty() || (similarity >= threshold && similarity < .97f))
enrollment.samples.add(observation.clone());
}
@Override public void expired(long visitorId) {
float[] removed = templates.remove(visitorId);
if (removed != null) Arrays.fill(removed, 0);
Enrollment removed = templates.remove(visitorId);
if (removed != null) for (float[] sample : removed.samples) Arrays.fill(sample, 0);
}
private static float bestSimilarity(float[] observation, Enrollment enrollment) {
float best = -2;
for (float[] sample : enrollment.samples) {
float similarity = 0;
for (int i = 0; i < sample.length; i++) similarity += observation[i] * sample[i];
best = Math.max(best, similarity);
}
return best;
}
}

View File

@ -586,11 +586,11 @@ public final class MainActivity extends Activity {
if (track.state != PersonTracker.State.ACTIVE) continue;
float[] sample = visitorSessions.needsObservation(track.id) ?
FaceReId.embed(reId, frame, assignments.get(track)) : null;
if (FaceReId.CONTROLLED_POC && sample == null && !visitorSessions.hasSession(track.id) &&
now - track.confirmationStart < config.confirmMs + 3000L) continue;
boolean awaitMoreEvidence = FaceReId.CONTROLLED_POC &&
now - track.activeSince < 5000L;
VisitorSessions.Assignment assignment;
try {
assignment = visitorSessions.observe(track.id, now, sample);
assignment = visitorSessions.observe(track.id, now, sample, awaitMoreEvidence);
} finally {
if (sample != null) Arrays.fill(sample, 0);
}

View File

@ -39,7 +39,7 @@ final class PersonTracker {
final VisitorAnalytics analytics = new VisitorAnalytics();
State state = State.DETECTED;
Box box, observed;
long lastSeen, confirmationStart, predictionTime;
long lastSeen, confirmationStart, predictionTime, activeSince;
int hits = 1;
boolean confirmed;
@ -166,6 +166,7 @@ final class PersonTracker {
if (track.state == State.DETECTED) transition(track, State.PENDING, events);
if (!track.confirmed && track.hits >= 2 && now - track.confirmationStart >= confirmMs) {
track.confirmed = true;
track.activeSince = now;
transition(track, State.ACTIVE, events);
}
}

View File

@ -7,7 +7,7 @@ interface ReIdProvider<Observation> {
long UNCERTAIN = -1;
boolean enabled();
default boolean hasObservation(long visitorId) { return false; }
default boolean enrollmentComplete(long visitorId) { return false; }
/** Return a confidently matched candidate ID, zero for a distinct visitor, or UNCERTAIN when evidence is insufficient. */
long match(Observation observation, List<VisitorSessions.Candidate> candidates);

View File

@ -27,7 +27,7 @@ final class VisitorSessions<Observation> {
}
}
enum Kind { CREATED, UNCERTAIN, RECONNECTED, CONTINUED }
enum Kind { CREATED, UNCERTAIN, DEFERRED, RECONNECTED, CONTINUED }
static final class Assignment {
final long visitorId;
@ -51,11 +51,9 @@ final class VisitorSessions<Observation> {
boolean needsObservation(int trackId) {
Session session = byTrack.get(trackId);
return provider.enabled() && (session == null || !provider.hasObservation(session.id));
return provider.enabled() && (session == null || !provider.enrollmentComplete(session.id));
}
boolean hasSession(int trackId) { return byTrack.containsKey(trackId); }
void expire(long now, long timeoutMs) {
Iterator<Session> items = open.values().iterator();
while (items.hasNext()) {
@ -68,6 +66,10 @@ final class VisitorSessions<Observation> {
}
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) {
@ -78,13 +80,14 @@ final class VisitorSessions<Observation> {
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 (!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) {

View File

@ -9,14 +9,17 @@ public final class FaceReIdTest {
VisitorSessions<float[]> sessions = new VisitorSessions<>(provider);
long first = sessions.observe(1, 0, vector(1, 0)).visitorId;
sessions.observe(1, 1000, vector(.8f, .6f));
sessions.trackClosed(1);
VisitorSessions.Assignment returned = sessions.observe(2, 11000, vector(.8f, .6f));
check(sessions.observe(2, 120000, vector(0, -1), true).kind == VisitorSessions.Kind.DEFERRED &&
sessions.totalCreated() == 1, "first low-quality comparison does not create a duplicate");
VisitorSessions.Assignment returned = sessions.observe(2, 121000, vector(.28f, .96f), true);
check(returned.kind == VisitorSessions.Kind.RECONNECTED && returned.visitorId == first,
"return with a different track and moderately changed vector");
check(sessions.totalCreated() == 1 && sessions.repeatedCount() == 1, "deduplicated counters");
sessions.trackClosed(2);
VisitorSessions.Assignment unknown = sessions.observe(3, 12000, null);
VisitorSessions.Assignment unknown = sessions.observe(3, 122000, null);
check(unknown.kind == VisitorSessions.Kind.UNCERTAIN && sessions.uncertainCount() == 1,
"missing observation leaves comparison uncertain");
sessions.trackClosed(3);
@ -26,11 +29,11 @@ public final class FaceReIdTest {
provider.observed(20, vector(1, 0));
check(provider.match(vector(1, 0), pair) == ReIdProvider.UNCERTAIN, "tied candidates stay uncertain");
provider.expired(20);
check(provider.match(vector(0, 1), pair.subList(0, 1)) == 0, "distinct embedding");
sessions.expire(72000, 60000);
check(sessions.openCount() == 0 && !provider.hasObservation(first), "timeout removes the embedding");
check(provider.match(vector(0, -1), pair.subList(0, 1)) == 0, "distinct embedding");
sessions.expire(182000, 60000);
check(sessions.openCount() == 0 && provider.match(vector(1, 0), pair.subList(0, 1)) == ReIdProvider.UNCERTAIN, "timeout removes the embedding");
sessions.observe(4, 72000, vector(1, 0));
sessions.observe(4, 182000, vector(1, 0));
sessions.closeSessions();
check(sessions.openCount() == 0 && sessions.totalCreated() == 3, "stop closes sessions, retaining count");
System.out.println("Face Re-ID tests passed");