Face unlock window: live camera preview, Snap, thumbnails, one password on Apply
CI / test (push) Failing after 12m30s
CI / test (push) Failing after 12m30s
Settings > Session "Add or remove faces…" (and "Remove…" on a face) opens a compositor-drawn Face unlock window instead of running root steps one by one: - chandra-face camera runs as the user (no password): it streams a small live picture with the face box and a cropped thumbnail per snap; the snaps' embeddings stay in that process, nothing is written to disk. - Click a snap to drop it; mark saved faces Remove/Keep. - Apply saves the snaps as one face and removes the marked ones in one root `chandra-face apply` (JSON on stdin, validated) through run0/pkexec: one password prompt. - New raster host function image-pixels (host-wasm, wasmhost, native) for the window's pictures. Tests: camera session and apply in face/test_face.py; the window end to end against a fake tool in compositor/host/face_test.mbt. Docs explain why the change itself still needs root. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hhiJfJNAYqWzQiuKMf
This commit is contained in:
74 files changed
+2617
-89
No files matched your search
@@ -240,6 +240,114 @@ class FaceTests(unittest.TestCase):
|
||||
self.assertIn("problem no camera at /dev/no-such-camera", self.env.run("status")[1])
|
||||
|
||||
|
||||
class Session:
|
||||
"""`chandra-face camera` as the "Add a face" window runs it: commands in, lines and pictures out."""
|
||||
|
||||
def __init__(self, env):
|
||||
self.p = subprocess.Popen(
|
||||
[sys.executable, "-I", TOOL, "camera", "--config", env.conf, "--machine", "--no-elevate",
|
||||
"--user", ME], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
|
||||
def send(self, line):
|
||||
self.p.stdin.write(line.encode() + b"\n")
|
||||
self.p.stdin.flush()
|
||||
|
||||
def next(self, kind):
|
||||
"""Messages until one of `kind`: (header words, picture bytes or None)."""
|
||||
while True:
|
||||
line = self.p.stdout.readline()
|
||||
if not line:
|
||||
raise AssertionError("ended before %s" % kind)
|
||||
words = line.decode().rstrip("\n").split(" ")
|
||||
data = None
|
||||
if words[0] in ("frame", "snap"):
|
||||
data = self.p.stdout.read(int(words[-1]))
|
||||
if words[0] == kind:
|
||||
return words, data
|
||||
|
||||
def finish(self):
|
||||
self.p.stdin.close()
|
||||
rc = self.p.wait(10)
|
||||
self.p.stdout.close()
|
||||
return rc
|
||||
|
||||
|
||||
class CameraWindowTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.env = Env()
|
||||
|
||||
def tearDown(self):
|
||||
self.env.close()
|
||||
|
||||
def vectors(self):
|
||||
f = os.path.join(self.env.store, ME, "faces.json")
|
||||
return json.loads(slurp(f))["faces"] if os.path.exists(f) else []
|
||||
|
||||
def test_live_picture_snaps_and_one_apply(self):
|
||||
self.env.camera(["alice"], frame_delay="0.01")
|
||||
s = Session(self.env)
|
||||
words, px = s.next("frame")
|
||||
w, h = int(words[1]), int(words[2])
|
||||
self.assertEqual((w, h), (32, 32))
|
||||
self.assertEqual(len(px), w * h * 3)
|
||||
self.assertEqual(words[3:7], ["0", "0", "32", "32"]) # the face box
|
||||
s.send("snap")
|
||||
words, px = s.next("snap")
|
||||
self.assertEqual(words[1:4], ["1", "96", "96"])
|
||||
self.assertEqual(len(px), 96 * 96 * 3)
|
||||
s.send("snap")
|
||||
s.next("snap")
|
||||
s.send("drop 1")
|
||||
self.assertEqual(self.vectors(), [], "nothing is saved before Apply")
|
||||
s.send("apply ")
|
||||
words, _ = s.next("applied")
|
||||
self.assertEqual(words[1], "0")
|
||||
self.assertEqual(s.finish(), 0)
|
||||
faces = self.vectors()
|
||||
self.assertEqual(len(faces), 1)
|
||||
self.assertEqual(len(faces[0]["vectors"]), 1, "the dropped snap is not saved")
|
||||
# The saved face is recognized.
|
||||
self.env.camera(["alice", "alice"])
|
||||
rc, out = self.env.run("compare")
|
||||
self.assertEqual(rc, 0, out)
|
||||
|
||||
def test_a_snap_without_a_face_says_why(self):
|
||||
self.env.camera(["flat"], frame_delay="0.01")
|
||||
s = Session(self.env)
|
||||
words, _ = s.next("frame")
|
||||
self.assertEqual(words[3:7], ["-1"] * 4)
|
||||
s.send("snap")
|
||||
words, _ = s.next("error")
|
||||
self.assertEqual(" ".join(words[1:]), "No face in view")
|
||||
s.send("quit")
|
||||
self.assertEqual(s.finish(), 0)
|
||||
self.assertEqual(self.vectors(), [])
|
||||
|
||||
def test_apply_adds_and_removes_in_one_go(self):
|
||||
self.env.camera(["alice", "alice", "alice"])
|
||||
self.env.run("enroll", "--label", "Old")
|
||||
self.env.camera(["bob"], frame_delay="0.01")
|
||||
s = Session(self.env)
|
||||
s.send("snap")
|
||||
s.next("snap")
|
||||
s.send("apply 1")
|
||||
words, _ = s.next("done")
|
||||
self.assertEqual(" ".join(words[1:]), "Added Face 2 and removed 1 face")
|
||||
self.assertEqual(s.next("applied")[0][1], "0")
|
||||
s.finish()
|
||||
self.assertEqual([f["label"] for f in self.vectors()], ["Face 2"])
|
||||
|
||||
def test_apply_refuses_bad_data(self):
|
||||
for bad in (b"[]", b"{\"add\": [{\"vectors\": [[1, 2]]}]}",
|
||||
b"{\"add\": [{\"vectors\": [[1e999, 0, 0, 0]]}]}", b"{\"remove\": [7]}", b"nonsense"):
|
||||
buf = io.StringIO()
|
||||
with redirect_stdout(buf):
|
||||
cfg = cf.Config(self.env.conf)
|
||||
with self.assertRaises(cf.Fail, msg=bad):
|
||||
cf.cmd_apply(cfg, ME, cf.Out(True), bad)
|
||||
self.assertEqual(self.vectors(), [])
|
||||
|
||||
|
||||
@unittest.skipUnless(os.environ.get("CHANDRA_FACE_MODELS"), "CHANDRA_FACE_MODELS not set")
|
||||
class OpenCVTests(unittest.TestCase):
|
||||
def test_no_face_in_noise_or_flat_frames(self):
|
||||
|
||||
Reference in new issue
Block a user