Skip to content

armgit / facex

Public Imported from GitHub
0

Full face stack that runs entirely in the browser. Detection, 576-point 3D mesh, recognition, anti-spoof, smile — all WebAssembly, zero server. Apache 2.0.

Skip disjoint boxes when checking video tracks for overlap

Baurzhan Atynov4d1710cbe503
diff --git a/python/facex/video.py b/python/facex/video.py
index a64489e..5a30b5e 100644
--- a/python/facex/video.py
+++ b/python/facex/video.py
@@ -1,4 +1,5 @@
"""Embedding refresh for one camera with externally assigned track IDs."""
+from bisect import bisect_left, bisect_right
import math
import numpy as np
@@ -13,6 +14,36 @@ def _iou(a, b):
return intersection / union if union > 0 else 0.
+def _ambiguous_tracks(boxes, tracks):
+ flags = [bool(face.get("ambiguous")) for face in tracks]
+ if len(boxes) < 2 or all(flags):
+ return flags
+
+ order = sorted(range(len(boxes)), key=lambda i: boxes[i][0])
+ left = [boxes[i][0] for i in order]
+ max_right = []
+ furthest = -math.inf
+ for i in order:
+ furthest = max(furthest, boxes[i][2])
+ max_right.append(furthest)
+
+ for i, box in enumerate(boxes):
+ if flags[i]:
+ continue
+ # Outside this range the horizontal intersection has zero width.
+ begin = bisect_right(max_right, box[0])
+ end = bisect_left(left, box[2])
+ for k in range(begin, end):
+ j = order[k]
+ other = boxes[j]
+ if i == j or other[2] <= box[0] or other[3] <= box[1] or box[3] <= other[1]:
+ continue
+ if _iou(box, other) > .15:
+ flags[i] = flags[j] = True
+ break
+ return flags
+
+
def _appearance(rgb, box):
h, w = rgb.shape[:2]
x1, y1, x2, y2 = box
@@ -52,14 +83,14 @@ class VideoEmbeddings:
ids = [f["track_id"] for f in tracks]
if len(ids) != len(set(ids)):
raise ValueError("Duplicate track ID in one frame")
- next_tracks, results = {}, []
- for i, face in enumerate(tracks):
- key, box = ids[i], tuple(face["bbox"])
+ boxes = [tuple(face["bbox"]) for face in tracks]
+ for box in boxes:
if len(box) != 4 or not all(map(math.isfinite, box)) or box[2] <= box[0] or box[3] <= box[1]:
raise ValueError("Invalid track box")
+ ambiguity = _ambiguous_tracks(boxes, tracks)
+ next_tracks, results = {}, []
+ for key, face, box, ambiguous in zip(ids, tracks, boxes, ambiguity):
signature, quality = _appearance(rgb, box)
- ambiguous = bool(face.get("ambiguous")) or any(
- j != i and _iou(box, other["bbox"]) > .15 for j, other in enumerate(tracks))
prev = self._tracks.get(key)
uncertain = face["score"] < .90 or min(box[2]-box[0], box[3]-box[1]) < 40
refresh = (uncertain or prev is None or self._last_frame != frame_number-1 or ambiguous or
diff --git a/tests/test_video_overlap.py b/tests/test_video_overlap.py
new file mode 100644
index 0000000..b845692
--- /dev/null
+++ b/tests/test_video_overlap.py
@@ -0,0 +1,34 @@
+"""Compare the video cache overlap index with exhaustive pairwise checks."""
+import random
+import unittest
+
+from facex.video import _ambiguous_tracks, _iou
+
+
+class VideoOverlapTest(unittest.TestCase):
+ def check_boxes(self, boxes, marked=()):
+ tracks = [{"ambiguous": i in marked} for i in range(len(boxes))]
+ expected = [track["ambiguous"] or any(
+ j != i and _iou(box, other) > .15 for j, other in enumerate(boxes)
+ ) for i, (box, track) in enumerate(zip(boxes, tracks))]
+ self.assertEqual(_ambiguous_tracks(boxes, tracks), expected)
+
+ def test_overlap_boundaries(self):
+ # Two widths of 23 with intersection 6 have IoU exactly 0.15.
+ for offset in (17, 16.999999, 17.000001, 23, 0):
+ self.check_boxes([(0, 0, 23, 8), (offset, 0, offset + 23, 8)])
+ self.check_boxes([(0, 0, 100, 10), (10, 10, 15, 20), (78, 0, 99, 10)], {0})
+
+ def test_random_layouts(self):
+ rng = random.Random(128)
+ for n in (0, 1, 2, 8, 32, 128):
+ for extent in (8, 100, 1000):
+ boxes = []
+ for _ in range(n):
+ x, y = rng.uniform(-extent, extent), rng.uniform(-extent, extent)
+ boxes.append((x, y, x + rng.uniform(1, 100), y + rng.uniform(1, 100)))
+ self.check_boxes(boxes, range(0, n, 9))
+
+
+if __name__ == "__main__":
+ unittest.main()