diff --git a/app/service/face_embedding.py b/app/service/face_embedding.py index 43b9aa5a..c34c50e8 100644 --- a/app/service/face_embedding.py +++ b/app/service/face_embedding.py @@ -124,43 +124,27 @@ async def compute_average_embedding( embeddings: list[np.ndarray] = [] for payload in payloads: - image = self._decode_image(payload) - - if ( - self.face_embedding.model is None - or not self.face_embedding._initialized # type: ignore - ): - raise RuntimeError("Face embedding model not ready") - image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) - faces: list[FaceStub] = self.face_embedding.model.get(image_rgb) # type: ignore + # Single detection pass — model.get() already returns embeddings + faces: list[FaceStub] = await asyncio.to_thread( + self.face_embedding.model.get, image_rgb # type: ignore + ) if not faces: raise AppException.bad_request( f"No faces detected in image {payload['filename']}" ) - bboxes: list[BBox] = [] + face = faces[0] - for face in faces: - fx1, fy1, fx2, fy2 = face.bbox - bboxes.append((int(fx1), int(fy1), int(fx2), int(fy2))) - - try: - embedding = await asyncio.to_thread( - self.face_embedding.embed, - image, - bboxes, - ) - - except (ValueError, RuntimeError) as exc: + if face.embedding is None: raise AppException.bad_request( - f"Face embedding failed for {payload['filename']}: {exc}" - ) from exc + f"Failed to generate embedding for {payload['filename']}" + ) - embeddings.append(np.array(embedding, dtype=np.float32)) + embeddings.append(face.embedding.astype(np.float32)) stacked = np.stack(embeddings, axis=0) averaged = np.mean(stacked, axis=0)