Metadata-Version: 2.5
Name: facekey
Version: 0.0.1
Summary: Face-ID-style unlock for your app: enroll a face from several angles, then authenticate against a live camera frame.
Project-URL: Homepage, https://github.com/Adm145/facekey
Project-URL: Issues, https://github.com/Adm145/facekey/issues
Author: Adm145
License: MIT
License-File: LICENSE
Keywords: authentication,biometrics,face id,face recognition,face unlock,insightface,liveness
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Classifier: Topic :: Security
Requires-Python: >=3.9
Requires-Dist: insightface>=0.7.3
Requires-Dist: numpy>=1.21
Requires-Dist: onnxruntime>=1.15
Requires-Dist: opencv-python-headless>=4.7
Requires-Dist: platformdirs>=3.0
Provides-Extra: camera
Requires-Dist: opencv-python>=4.7; extra == 'camera'
Provides-Extra: crypto
Requires-Dist: cryptography>=41; extra == 'crypto'
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Provides-Extra: gpu
Requires-Dist: onnxruntime-gpu>=1.15; extra == 'gpu'
Description-Content-Type: text/markdown

# facekey

> ⚠️ **Alpha — the API will change.** Pin an exact version if you depend on it.

Face-ID-style unlock for your Python app. Enroll a face from a few angles, then
on each unlock grab a frame from the camera, compare it to the enrolled
template, and grant access on a match.

```python
from facekey import FaceLock

lock = FaceLock()                 # single enrolled face, stored under a per-user data dir

# one-time enrollment (needs a camera; pip install "facekey[camera]")
lock.run_enrollment(camera=0, on_prompt=print)

# later, guard something
if lock.unlock(camera=0, timeout=5):
    reveal_secret()
else:
    fall_back_to_password()
```

No camera in your process? Feed frames yourself — that's the real API; the
camera helper is a thin convenience on top:

```python
import numpy as np
from facekey import FaceLock

lock = FaceLock()

session = lock.enroll()
while session.needs:                       # ["frontal", "left", "right", "up", "down"]
    frame = my_capture()                   # H x W x 3 BGR ndarray, or JPEG/PNG bytes
    result = session.capture(frame)
    print(result.reason, "still need:", session.needs)
session.commit()

auth = lock.authenticate(my_capture())
print(auth.granted, auth.score, auth.liveness)
```

## This is not a security boundary

facekey uses an ordinary RGB camera. There is no infrared or depth sensing like
a phone's Face ID. Passive liveness catches a casual printed photo or a phone
screen held up to the lens, but it is beatable by replay video, masks, and
deepfakes.

Treat it as a **convenience factor**, not a lock:

- Never make it the *only* thing standing between someone and sensitive data.
- Always ship a PIN / password fallback.
- Store only embeddings, never the enrollment images (facekey does this).
- Let people delete their template (`lock.remove()`).
- Get explicit consent before enrolling anyone. Face templates are biometric
  data and are regulated (EU GDPR Art. 9, Illinois BIPA, others). Consent,
  retention limits, and deletion are your responsibility.

## Install

```bash
pip install facekey                 # core: frame-in, decision-out
pip install "facekey[camera]"       # + OpenCV highgui for the camera helper / preview
pip install "facekey[gpu]"          # + onnxruntime-gpu
pip install "facekey[crypto]"       # + encryption at rest for stored templates
```

First use downloads the InsightFace model pack (~300 MB) into a local cache.
Do it ahead of time with:

```bash
facekey download-models
```

`insightface` may build from source on some platforms; you need a C/C++
toolchain if no wheel is available for your Python/OS.

## EMBEDDER LICENSING — read before shipping commercially

The default recognition backend is InsightFace **`buffalo_l`**. Its *code* is
MIT, but its *pretrained weights* are released **for non-commercial research use
only** (the training datasets forbid commercial use). Commercial use needs a
license from InsightFace: `recognition-oss-pack@insightface.ai`.

facekey emits a `FaceKeyLicenseWarning` the first time the default backend
loads. To use facekey in a commercial product without that license, supply a
differently-licensed embedder:

```python
from facekey import FaceLock
from facekey.embedders import Embedder   # implement this protocol over your own model

lock = FaceLock(embedder=MyPermissiveArcFace())
```

Suppress the warning (once you understand the terms) with
`FACEKEY_SUPPRESS_LICENSE_WARNING=1` in the environment.

## API sketch

| Call | Purpose |
|---|---|
| `FaceLock(store=None, *, threshold=0.45, liveness=True, model="buffalo_l", max_enrollees=1, gpu=False)` | Construct a lock. `store`: `None` → per-user data dir, a path, or a `Store` instance. |
| `lock.enroll(label="default") -> EnrollmentSession` | Start guided multi-angle enrollment. |
| `session.capture(frame) -> CaptureResult` / `session.needs` / `session.commit()` | Feed frames until every pose bucket is covered, then persist. |
| `lock.authenticate(frame, enrollee=None) -> AuthResult` | 1:1 verify. `enrollee` optional while only one face is enrolled. |
| `lock.identify(frame) -> AuthResult` | 1:N — best match across all enrolled faces. |
| `lock.unlock(camera=0, timeout=5) -> bool` | Camera helper: grab frames until match or timeout. |
| `lock.run_enrollment(camera=0, on_prompt=print)` | Camera helper: drives the "turn your head" flow. |
| `lock.enrolled()` / `lock.is_enrolled()` / `lock.remove(label="default")` | Manage templates. |

`max_enrollees` is `1` today. The store, matching, and `identify()` are already
multi-face internally — raise the cap (up to a handful) when you want a shared
device.

## Tuning the threshold

`threshold` is cosine similarity on L2-normalized embeddings; higher is
stricter. `0.45` is a deliberately conservative default for an auth use case.
Lower it if legitimate users are rejected too often (higher false-accept rate),
raise it to make impostor matches harder (higher false-reject rate). Measure on
your own camera and lighting before trusting a number.

## Status

Alpha. `matching`, `stores`, `enrollment`, `models`, `errors` are implemented;
`embedders.insightface`, `liveness`, and `camera` are minimal and expected to
change. Passive liveness currently ships a lightweight texture heuristic
(`HeuristicLiveness`); a MiniFASNet backend is the planned upgrade.

## License

MIT for the source (see `LICENSE`). Model weights carry their own terms — see
EMBEDDER LICENSING above.
