Face unlock: screen fill-light for low light; fix Quad-Bayer AE ramp
CI / test (push) Failing after 51s

- Boost the backlight to 90% while enrolling with the light on or while sign-in is scanning, then restore the previous level when that ends or the window closes.
- Add a Light: On/Off toggle (L key) to the Face window; the backdrop turns soft white while the light is on.
- Quad-Bayer AE: compute the low-light gain once and cap it by the highlight limit, so ramp-up and highlight back-off agree.
- Tests: backlight round-trip through the Face window, Quad-Bayer debayer color/low-light/dark-frame cases, and the AE ramp, back-off and restore order.
This commit is contained in:
nandi committed 2026-11-28 18:32:15 -08:00
1 parent c5b96e2c70
commit 40c5d6722c
8 files changed
+266 -12

No files matched your search

+4 -5
View File
@@ -711,12 +711,11 @@ class QuadSensorAE:
return False
if 36.0 <= raw_p50 <= 64.0 and raw_p95 <= 235.0:
return False
ratio = min(4.0, max(0.25, 48.0 / max(raw_p50, 0.5)))
if raw_p95 > 240.0:
ratio = max(0.35, 200.0 / max(raw_p95, 1.0))
else:
ratio = min(4.0, max(0.25, 48.0 / max(raw_p50, 0.5)))
if raw_p95 > 120.0 and raw_p95 * ratio > 235.0:
ratio = max(0.25, min(ratio, 235.0 / raw_p95))
ratio = min(ratio, max(0.25, 200.0 / max(raw_p95, 1.0)))
elif raw_p95 > 120.0 and raw_p95 * ratio > 235.0:
ratio = max(0.25, min(ratio, 235.0 / raw_p95))
new_ev = max(self.e_min, min(self.ev_total * (ratio ** 0.75), self.e_max))
if abs(new_ev - self.ev_total) <= self.ev_total * 0.05:
return False
+142
View File
@@ -398,6 +398,148 @@ class OpenCVTests(unittest.TestCase):
env.close()
try:
import cv2 as _cv2 # noqa
import numpy as _np # noqa
HAS_CV2_NUMPY = True
except ImportError:
HAS_CV2_NUMPY = False
def _make_quad_raw(w, h, p8_func):
"""Build an unpacked->CSI2P Quad-Bayer raw buffer from per-block pixel helper `p8_func(x, y, phase)`."""
stride = (w // 4) * 5
arr = _np.zeros((h, stride), dtype=_np.uint8)
for y in range(h):
for xb in range(w // 4):
c0, c1 = p8_func(xb, y)
base = xb * 5
arr[y, base + 0] = c0
arr[y, base + 1] = c0
arr[y, base + 2] = c1
arr[y, base + 3] = c1
return arr.tobytes(), stride
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
class QuadDebayerTests(unittest.TestCase):
def test_quad_debayer_color_lowlight_and_covered_dark(self):
# 1. Normal color response: red top half, blue bottom half
def rb(xb, y):
if (y % 4) < 2:
return (200, 24) if y < 4 else (24, 24)
return (24, 24) if y < 4 else (24, 200)
raw, stride = _make_quad_raw(8, 8, rb)
out = cf.debayer_quad_csi2p(raw, 8, 8, stride, rot=0, target_size=None, cv2_mod=_cv2, np_mod=_np)
self.assertEqual(out.shape, (4, 4, 3))
self.assertGreater(float(out[0, :, 2].mean()), float(out[0, :, 0].mean()) + 30.0)
self.assertGreater(float(out[3, :, 0].mean()), float(out[3, :, 2].mean()) + 30.0)
# 2. Low-light scene: optical black=15 (sum=60), signal +2..+3 per p8 (8..12 above black in 2x2 sum)
w, h = 80, 80
raw_dim, stride_dim = _make_quad_raw(w, h, lambda xb, y: (17 if xb < w // 8 else 18, 17 if y < h // 2 else 18))
out_dim, stats_dim = cf.debayer_quad_csi2p(
raw_dim, w, h, stride_dim, rot=0, target_size=(80, 80), return_stats=True, cv2_mod=_cv2, np_mod=_np
)
self.assertEqual(len(stats_dim), 2)
self.assertGreater(float(out_dim.mean()), 65.0)
# 3. Covered lens dark frame: constant black=15 (zero signal above black 60) -> must stay dark
raw_dark, stride_dark = _make_quad_raw(w, h, lambda xb, y: (15, 15))
out_dark, stats_dark = cf.debayer_quad_csi2p(
raw_dark, w, h, stride_dark, rot=0, target_size=(80, 80), return_stats=True, cv2_mod=_cv2, np_mod=_np
)
self.assertLess(float(out_dark.mean()), 12.0)
self.assertLess(stats_dark[1], 0.8)
class _MockSubdevIO:
def __init__(self):
self.meta = {
cf.V4L2_CID_VBLANK: (56, 62029, 1, 56),
cf.V4L2_CID_EXPOSURE: (4, 3542, 1, 1000),
cf.V4L2_CID_ANALOGUE_GAIN: (0, 480, 1, 0),
cf.V4L2_CID_DIGITAL_GAIN: (0, 4096, 1, 1024),
}
self.vals = {
cf.V4L2_CID_VBLANK: 56,
cf.V4L2_CID_EXPOSURE: 1000,
cf.V4L2_CID_ANALOGUE_GAIN: 0,
cf.V4L2_CID_DIGITAL_GAIN: 1024,
}
self.log = []
self.closed = False
def query(self, cid):
if cid == cf.V4L2_CID_EXPOSURE:
extra = max(0, self.vals[cf.V4L2_CID_VBLANK] - 56)
return (4, 3542 + extra, 1, 1000)
return self.meta[cid]
def get(self, cid):
return self.vals[cid]
def set(self, cid, val):
if cid == cf.V4L2_CID_EXPOSURE:
max_exp = 3542 + max(0, self.vals[cf.V4L2_CID_VBLANK] - 56)
if val > max_exp:
raise AssertionError("EXPOSURE %d set before VBLANK raised (max %d)" % (val, max_exp))
elif cid == cf.V4L2_CID_VBLANK:
max_exp_after = 3542 + max(0, val - 56)
if self.vals[cf.V4L2_CID_EXPOSURE] > max_exp_after:
raise AssertionError("VBLANK %d lowered while EXPOSURE still %d" % (val, self.vals[cf.V4L2_CID_EXPOSURE]))
self.vals[cid] = val
self.log.append((cid, val))
return val
def close(self):
self.closed = True
class QuadSensorAETests(unittest.TestCase):
def test_lowlight_ramp_cooldown_and_restore(self):
io = _MockSubdevIO()
ae = cf.QuadSensorAE("/dev/null", io=io)
ae.apply_initial()
self.assertEqual(ae.cooldown, 2)
# Cooldown skips 2 frames
self.assertFalse(ae.update(4.0, 8.0))
self.assertEqual(ae.cooldown, 1)
self.assertFalse(ae.update(4.0, 8.0))
self.assertEqual(ae.cooldown, 0)
# Ramp in pitch dimness through vblank/exposure, analogue gain, then digital gain
for _ in range(10):
ae.cooldown = 0
ae.update(3.0, 6.0)
self.assertGreater(io.vals[cf.V4L2_CID_VBLANK], 56)
self.assertGreater(io.vals[cf.V4L2_CID_EXPOSURE], 3542)
self.assertEqual(io.vals[cf.V4L2_CID_ANALOGUE_GAIN], 480)
self.assertGreater(io.vals[cf.V4L2_CID_DIGITAL_GAIN], 1024)
# Bright highlight overload backs off exposure safely before reducing vblank
io.log.clear()
for _ in range(12):
ae.cooldown = 0
ae.update(210.0, 250.0)
self.assertEqual(io.vals[cf.V4L2_CID_VBLANK], 56)
self.assertLess(io.vals[cf.V4L2_CID_EXPOSURE], 3542)
self.assertEqual(io.vals[cf.V4L2_CID_DIGITAL_GAIN], 1024)
# Raise again then close() to test clean restore order
ae.cooldown = 0
for _ in range(8):
ae.cooldown = 0
ae.update(3.0, 6.0)
ae.close()
self.assertTrue(io.closed)
self.assertEqual(io.vals[cf.V4L2_CID_VBLANK], 56)
self.assertEqual(io.vals[cf.V4L2_CID_EXPOSURE], 1000)
self.assertEqual(io.vals[cf.V4L2_CID_ANALOGUE_GAIN], 0)
self.assertEqual(io.vals[cf.V4L2_CID_DIGITAL_GAIN], 1024)
@unittest.skipUnless("--pam" in sys.argv, "pass --pam to test the PAM module")
class PamTests(unittest.TestCase):
"""pam_chandra_face.so through pamtester, with a throwaway service in /etc/pam.d."""