Skip to content

Commit 88fc8d5

Browse files
authored
feat: add typed cross-collection search (#9)
1 parent b33c1f3 commit 88fc8d5

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎livexface/__init__.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
VerifyResult,
88
FaceMatch,
99
IdentifyResult,
10+
CrossCollectionSearchMatch,
11+
CrossCollectionSearchResult,
12+
SkippedCollection,
1013
LivenessResult,
1114
LivenessChallenge,
1215
ActiveLivenessResult,
@@ -34,6 +37,9 @@
3437
"VerifyResult",
3538
"FaceMatch",
3639
"IdentifyResult",
40+
"CrossCollectionSearchMatch",
41+
"CrossCollectionSearchResult",
42+
"SkippedCollection",
3743
"LivenessResult",
3844
"LivenessChallenge",
3945
"ActiveLivenessResult",

‎livexface/client.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Face,
1919
VerifyResult,
2020
IdentifyResult,
21+
CrossCollectionSearchResult,
2122
LivenessResult,
2223
ActiveLivenessResult,
2324
LivenessSession,
@@ -337,6 +338,24 @@ def identify(
337338
)
338339
return IdentifyResult.from_dict(resp)
339340

341+
def search(
342+
self,
343+
image: ImageInput,
344+
collection_ids: Sequence[str] | None = None,
345+
top_k: int = 5,
346+
threshold: float | None = None,
347+
) -> CrossCollectionSearchResult:
348+
"""Search for matching faces across multiple or all collections."""
349+
fname, fbytes, ftype = _to_bytes_tuple(image)
350+
files = {"image": (fname, fbytes, ftype)}
351+
data: dict[str, str] = {"top_k": str(top_k)}
352+
if collection_ids:
353+
data["collection_ids"] = ",".join(collection_ids)
354+
if threshold is not None:
355+
data["threshold"] = str(threshold)
356+
resp = self._c._request("POST", "/search", files=files, data=data)
357+
return CrossCollectionSearchResult.from_dict(resp)
358+
340359
def liveness(self, collection_id: str, image: ImageInput) -> LivenessResult:
341360
"""Passive liveness detection — check whether the face in the image is live."""
342361
fname, fbytes, ftype = _to_bytes_tuple(image)

‎livexface/types.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,55 @@ def from_dict(cls, d: dict[str, Any]) -> "IdentifyResult":
8484
)
8585

8686

87+
@dataclass
88+
class CrossCollectionSearchMatch(FaceMatch):
89+
collection_id: str = ""
90+
91+
@classmethod
92+
def from_dict(cls, d: dict[str, Any]) -> "CrossCollectionSearchMatch":
93+
return cls(
94+
face_id=d.get("faceId", ""),
95+
external_id=d.get("externalId", ""),
96+
confidence=float(d.get("confidence", 0.0)),
97+
metadata=d.get("metadata"),
98+
collection_id=d.get("collectionId", ""),
99+
)
100+
101+
102+
@dataclass
103+
class SkippedCollection:
104+
id: str
105+
name: str
106+
reason: str
107+
108+
@classmethod
109+
def from_dict(cls, d: dict[str, Any]) -> "SkippedCollection":
110+
return cls(
111+
id=d.get("id", ""),
112+
name=d.get("name", ""),
113+
reason=d.get("reason", ""),
114+
)
115+
116+
117+
@dataclass
118+
class CrossCollectionSearchResult:
119+
matches: list[CrossCollectionSearchMatch]
120+
query_time_ms: int
121+
collections_searched: int
122+
skipped_collections: list[SkippedCollection]
123+
124+
@classmethod
125+
def from_dict(cls, d: dict[str, Any]) -> "CrossCollectionSearchResult":
126+
return cls(
127+
matches=[CrossCollectionSearchMatch.from_dict(m) for m in d.get("matches", [])],
128+
query_time_ms=d.get("queryTimeMs", 0),
129+
collections_searched=d.get("collectionsSearched", 0),
130+
skipped_collections=[
131+
SkippedCollection.from_dict(c) for c in d.get("skippedCollections", [])
132+
],
133+
)
134+
135+
87136
@dataclass
88137
class LivenessResult:
89138
"""Result of a passive liveness check.

‎tests/test_contract.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"faces.delete": (("c1", "f1"), {}, NO_CONTENT),
5151
"faces.verify": (("c1", IMG, "f1"), {"threshold": 0.5}, DATA),
5252
"faces.identify": (("c1", IMG), {"top_k": 3, "threshold": 0.5}, DATA),
53+
"faces.search": ((IMG,), {"collection_ids": ["c1", "c2"], "top_k": 3, "threshold": 0.5}, DATA),
5354
"faces.liveness": (("c1", IMG), {}, DATA),
5455
"faces.active_liveness": (("c1", [IMG] * 5), {}, DATA),
5556
"faces.create_liveness_session": (

‎tests/test_errors.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,21 @@ def test_api_error_exposes_details(mocker: Any) -> None:
2929
assert exc.value.code == "MULTIPLE_FACES"
3030
assert exc.value.details == {"faceCount": 2, "faces": []}
3131
assert exc.value.request_id == "r-1"
32+
33+
34+
def test_search_parses_skips_and_typed_profile_mismatch(mocker: Any) -> None:
35+
client = LiveXFace(api_key="lxf_test", base_url="http://api.test/api/v1")
36+
ok = MagicMock(status_code=200, ok=True)
37+
ok.json.return_value = {"success": True, "data": {"matches": [], "queryTimeMs": 7, "collectionsSearched": 1, "skippedCollections": [{"id": "c2", "name": "Legacy", "reason": "embedding_profile_mismatch"}]}}
38+
request = mocker.patch.object(client._session, "request", return_value=ok)
39+
result = client.faces.search(b"img", collection_ids=["c1", "c2"])
40+
assert result.skipped_collections[0].reason == "embedding_profile_mismatch"
41+
assert request.call_args.kwargs["data"]["collection_ids"] == "c1,c2"
42+
43+
conflict = MagicMock(status_code=409, ok=False)
44+
conflict.json.return_value = {"success": False, "error": {"code": "EMBEDDING_PROFILE_MISMATCH", "message": "no compatible collections"}}
45+
request.return_value = conflict
46+
with pytest.raises(LiveXFaceApiError) as exc:
47+
client.faces.search(b"img")
48+
assert exc.value.status_code == 409
49+
assert exc.value.code == "EMBEDDING_PROFILE_MISMATCH"

0 commit comments

Comments
 (0)