Camera: cut viewfinder CPU use
CI / test (push) Failing after 49s

- Decode half-size frames at 24 fps (15 on the raw front camera) for the viewfinder; full frames only for photos, video, focusing and QR scans
- Swap channels with OpenCV instead of numpy; 2 OpenCV threads for live frames; OpenMP threads sleep when idle

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-29 01:14:29 -08:00
1 parent f5a252e063
commit 019c9ada6c
3 files changed
+138 -25

No files matched your search

+46
View File
@@ -332,6 +332,52 @@ class TestCameraHelpers(unittest.TestCase):
self.assertAlmostEqual(pts_ms[4], 145.0, delta=5.0)
class PreviewCostTests(unittest.TestCase):
"""The viewfinder decodes small, paced frames; whoever asks gets full ones."""
def engine(self):
eng = cam_mod.CameraEngine([])
eng._frames_since_open = cam_mod.PREVIEW_SETTLE_FRAMES
return eng
@unittest.skipUnless(HAS_CV2_NUMPY, "needs OpenCV")
def test_half_frame_is_the_full_one_scaled(self):
rng = np.random.default_rng(3)
small = rng.integers(0, 256, (6, 8, 4), dtype=np.uint8)
big = small.repeat(2, axis=0).repeat(2, axis=1)
stride = 16 * 4 + 12
rows = np.zeros((12, stride), np.uint8)
rows[:, :64] = big.reshape(12, 64)
for rot in (0, 90, 270):
for fmt in ("ABGR8888", "ARGB8888"):
kw = dict(rot=rot, cv2_mod=cv2, np_mod=np)
full = cam_mod.convert_frame(rows.tobytes(), 16, 12, stride, fmt, **kw)
half = cam_mod.convert_frame(rows.tobytes(), 16, 12, stride, fmt, half=True, **kw)
self.assertTrue(np.array_equal(half, full[::2, ::2]))
@unittest.skipUnless(HAS_CV2_NUMPY, "needs OpenCV")
def test_preview_is_paced(self):
eng = self.engine()
due = sum(eng._preview_frame_due(100.0 + i / 34.0) for i in range(34 * 4))
self.assertTrue(92 <= due <= 98, due)
eng._frames_since_open = 0
self.assertTrue(eng._preview_frame_due(100.0) and eng._preview_frame_due(100.0))
@unittest.skipUnless(HAS_CV2_NUMPY, "needs OpenCV")
def test_full_frames_are_for_whoever_asks(self):
eng = self.engine()
self.assertFalse(eng._needs_full_frame())
self.assertIsNone(eng.grab_full_frame(timeout=0.01))
self.assertTrue(eng._needs_full_frame())
eng._full_wanted = False
class AF:
state = "coarse"
eng._af = AF()
self.assertTrue(eng._needs_full_frame())
class QRTests(unittest.TestCase):
def test_reporter_reports_once_until_out_of_view(self):
r = cam_mod.QRReporter()