From 3b60a21ebe28c1617d321c98069dfdad9d573c12 Mon Sep 17 00:00:00 2001 From: jerinpeter Date: Tue, 19 May 2026 14:22:30 -0700 Subject: [PATCH 1/4] update selfie-logic --- .gitignore | 1 + .../face_recog_stream/__init__.py | 1 + .../face_recog_stream/gallery.py | 107 +++- .../face_recog_stream/http_api.py | 574 ++++++++++++++++-- .../anonymizationSys/face_recog_stream/run.py | 32 + .../face_recog_stream/selfie_logic.py | 335 ++++++++++ 6 files changed, 994 insertions(+), 56 deletions(-) create mode 100644 src/om1_vlm/anonymizationSys/face_recog_stream/selfie_logic.py diff --git a/.gitignore b/.gitignore index 2c7af04..9fdd4dc 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ __pycache__ *.engine *.onnx venv/ +.venv/ diff --git a/src/om1_vlm/anonymizationSys/face_recog_stream/__init__.py b/src/om1_vlm/anonymizationSys/face_recog_stream/__init__.py index 662d8f2..a75ffc0 100644 --- a/src/om1_vlm/anonymizationSys/face_recog_stream/__init__.py +++ b/src/om1_vlm/anonymizationSys/face_recog_stream/__init__.py @@ -10,6 +10,7 @@ "scrfd", "gallery", "io", + "selfie_logic", "http_api", "who_tracker", "yolo_pose", diff --git a/src/om1_vlm/anonymizationSys/face_recog_stream/gallery.py b/src/om1_vlm/anonymizationSys/face_recog_stream/gallery.py index 92c1a48..05b6fbb 100644 --- a/src/om1_vlm/anonymizationSys/face_recog_stream/gallery.py +++ b/src/om1_vlm/anonymizationSys/face_recog_stream/gallery.py @@ -282,13 +282,109 @@ def add_aligned_snapshot( cv2.imwrite(absf, img_112_bgr, [cv2.IMWRITE_JPEG_QUALITY, 95]) # Embed & append - vec = self._embed_single(img_112_bgr) + vec = self.embed_aligned(img_112_bgr) row = self._append_vectors(vec[None, :]) self._index["items"][rel] = {"row": row, "label": label} self._save_index() self._recompute_stats() return rel + def add_aligned_no_stats( + self, + label: str, + img_112_bgr: np.ndarray, + vec: Optional[np.ndarray] = None, + fname_hint: Optional[str] = None, + ) -> str: + """ + Add a 112×112 aligned snapshot WITHOUT recomputing stats. + + Designed for multi-shot batch enrollment (e.g. /selfie collects N frames + and saves them all in one go). Behaves like ``add_aligned_snapshot`` but: + + - Skips the trailing ``_recompute_stats()`` call. Caller MUST invoke + ``recompute_stats()`` after the batch finishes; otherwise stats.json + stays stale until the next refresh. + - If ``vec`` is provided, uses it directly (must be L2-normalized). + Avoids re-embedding the same crop when the caller already has it + (e.g. /selfie pipeline embedded it for dedup/novelty/consistency). + + Parameters + ---------- + label : str + Identity name (subfolder under gallery). + img_112_bgr : np.ndarray + Aligned BGR crop of shape ``(112,112,3)`` (will be resized if needed). + vec : np.ndarray, optional + Pre-computed L2-normalized embedding of shape ``(dim,)``. If + supplied and valid, the crop is not re-embedded. If shape/norm + looks wrong, falls back to a fresh embedding. + fname_hint : str, optional + Preferred filename (``.jpg`` appended if missing), by default ``None``. + + Returns + ------- + str + Relative path of the saved aligned image + (e.g., ``"wendy/aligned/2026-05-19T18-30-00_00.jpg"``). + """ + if img_112_bgr is None or img_112_bgr.size == 0: + raise ValueError("empty snapshot") + + if img_112_bgr.shape[:2] != (self.aligned_size, self.aligned_size): + img_112_bgr = cv2.resize( + img_112_bgr, (self.aligned_size, self.aligned_size) + ) + + # Save to gallery/