safesight/control/tools/gallery_builder/aggregate.py
tian 98e375dc36 chore: 合并 safesight-control 仓库为 control/ 子目录
项目合并为单仓库:设备端(根) + 管理端(control/)。
两端 git 历史完整保留(git subtree 合并)。

git-subtree-dir: control
git-subtree-mainline: 923ab10d5c
git-subtree-split: 70738edb31
2026-08-02 11:13:26 +08:00

21 lines
586 B
Python

from __future__ import annotations
import numpy as np
def l2_normalize(v: np.ndarray, eps: float = 1e-12) -> np.ndarray:
v = np.asarray(v, dtype=np.float32)
n = float(np.linalg.norm(v))
if n < eps:
return v * 0.0
return v / n
def compute_centroid(embs: np.ndarray) -> np.ndarray:
"""embs: float32 shape (K,D), expected already L2-normalized per row."""
embs = np.asarray(embs, dtype=np.float32)
if embs.ndim != 2 or embs.shape[0] < 1:
raise ValueError("embs must be (K,D) with K>=1")
c = embs.mean(axis=0)
return l2_normalize(c)