Files
chandra/camera/test_camera.py
T
nandi bfc60ee0e5
CI / test (push) Failing after 50s
Add chandra-remote: VNC and web control over Tailscale
- remote/: shared-memory framebuffer mirror served as RFB 3.8 (VNC, port 5900) and an HTTP/WebSocket PWA (port 6080), bound to the Tailscale interface and loopback by default
- Compositor: remote pointer, button, scroll, modifier, key, key-tap, text and clipboard input, routed past OLED blanking
- chandra-ctl: add status, refresh and osk commands; add input-* and clipboard-set for remote control
- Camera: debayer 10-bit CSI-2 Quad-Bayer frames for imx371 and high-quality still capture
- Camera: rotate frames by the libcamera mounting angle (CCW for 90), fix the expected test values, and restyle the UI for a dark theme
2026-11-28 17:57:55 -08:00

130 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""Host-side unit tests for chandra-camera frame/camera helpers (no hardware required)."""
import importlib.machinery
import importlib.util
import os
import sys
import unittest
try:
import cv2
import numpy as np
HAS_CV2_NUMPY = True
except ImportError:
cv2 = None
np = None
HAS_CV2_NUMPY = False
CAM_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "chandra-camera")
_loader = importlib.machinery.SourceFileLoader("chandra_camera_mod", CAM_PATH)
_spec = importlib.util.spec_from_loader("chandra_camera_mod", _loader)
cam_mod = importlib.util.module_from_spec(_spec)
sys.modules["chandra_camera_mod"] = cam_mod
_loader.exec_module(cam_mod)
class TestCameraHelpers(unittest.TestCase):
def test_sort_and_label_cameras(self):
raw = [
("/base/front", "libcamera", "imx371", 0, 90, 4656 * 3496),
("/base/back2", "libcamera", "imx376", 1, 270, 2592 * 1944),
("/base/back1", "libcamera", "imx519", 1, 270, 4656 * 3496),
]
cams = cam_mod.sort_and_label_cameras(raw)
self.assertEqual([c.model for c in cams], ["imx519", "imx371", "imx376"])
self.assertEqual([c.label for c in cams], ["Back · imx519", "Front · imx371", "Back 2 · imx376"])
self.assertEqual(cam_mod.select_initial_camera(cams, ""), 0)
self.assertEqual(cam_mod.select_initial_camera(cams, "front"), 1)
self.assertEqual(cam_mod.select_initial_camera(cams, "back"), 0)
self.assertEqual(cam_mod.select_initial_camera(cams, "imx376"), 2)
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
def test_convert_abgr8888_with_stride_and_rotations(self):
# 2x3 image with stride padding (row = 2*4 + 4 padding bytes = 12)
h, w, stride = 3, 2, 12
buf = bytearray(h * stride)
# Pixel (x=0, y=0): ABGR8888 in LE memory is [R, G, B, A] = [10, 20, 200, 255] -> BGR [200, 20, 10]
buf[0:4] = bytes([10, 20, 200, 255])
# Pixel (x=1, y=0): R=40, G=50, B=60 -> BGR [60, 50, 40]
buf[4:8] = bytes([40, 50, 60, 255])
bgr0 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=0, cv2_mod=cv2, np_mod=np)
self.assertEqual(bgr0.shape, (3, 2, 3))
self.assertEqual(tuple(bgr0[0, 0]), (200, 20, 10))
self.assertEqual(tuple(bgr0[0, 1]), (60, 50, 40))
bgr90 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=90, cv2_mod=cv2, np_mod=np)
self.assertEqual(bgr90.shape, (2, 3, 3))
# Libcamera Rotation=90 (CCW mounting) rotates CCW: (row=0, col=0) -> (row=w-1=1, col=0)
self.assertEqual(tuple(bgr90[1, 0]), (200, 20, 10))
bgr270 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=270, cv2_mod=cv2, np_mod=np)
self.assertEqual(bgr270.shape, (2, 3, 3))
# Libcamera Rotation=270 rotates CW: (row=0, col=0) -> (row=0, col=h-1=2)
self.assertEqual(tuple(bgr270[0, 2]), (200, 20, 10))
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
def test_convert_argb_rgb_and_cairo_bgra(self):
# ARGB8888 in LE memory is [B, G, R, A]
buf = bytes([210, 110, 30, 255, 10, 20, 30, 255])
bgr = cam_mod.convert_frame(buf, 2, 1, 8, "ARGB8888", rot=0, cv2_mod=cv2, np_mod=np)
self.assertEqual(tuple(bgr[0, 0]), (210, 110, 30))
rgb_buf = bytes([11, 22, 33, 44, 55, 66])
bgr2 = cam_mod.convert_frame(rgb_buf, 2, 1, 6, "RGB888", rot=180, cv2_mod=cv2, np_mod=np)
# Inverted horizontally+vertically and channel-flipped -> [66, 55, 44] then [33, 22, 11]
self.assertEqual(tuple(bgr2[0, 0]), (66, 55, 44))
self.assertEqual(tuple(bgr2[0, 1]), (33, 22, 11))
argb32 = cam_mod.bgr_to_cairo_argb32(bgr, np_mod=np)
self.assertEqual(argb32.shape, (1, 2, 4))
self.assertEqual(tuple(argb32[0, 0]), (210, 110, 30, 255))
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
def test_ensure_even_frame(self):
odd = np.zeros((101, 203, 3), dtype=np.uint8)
even = cam_mod.ensure_even_frame(odd)
self.assertEqual(even.shape, (100, 202, 3))
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
def test_debayer_quad_csi2p(self):
# Construct an 8x8 CSI-2 10-bit packed 4x4 Quad-Bayer SRGGB image:
# Top half (rows 0..3): strong red (R=200, G=24, B=24)
# Bottom half (rows 4..7): strong blue (R=24, G=24, B=200)
w, h, stride = 8, 8, 10
arr = np.zeros((h, stride), dtype=np.uint8)
for y in range(h):
top = y < 4
for x_blk in range(2): # 4 pixels per block, 5 bytes per block
# Row phase inside 4x4 Quad-Bayer: y%4 in {0,1} -> R,R,G,G; {2,3} -> G,G,B,B
if (y % 4) < 2:
c0 = 200 if top else 24 # R
c1 = 24 # G
else:
c0 = 24 # G
c1 = 24 if top else 200 # B
base = x_blk * 5
arr[y, base + 0] = c0
arr[y, base + 1] = c0
arr[y, base + 2] = c1
arr[y, base + 3] = c1
arr[y, base + 4] = 0
raw = arr.tobytes()
out_live = cam_mod.debayer_quad_csi2p(
raw, w, h, stride, rot=90, target_size=None, high_quality=False, cv2_mod=cv2, np_mod=np
)
out_hq = cam_mod.debayer_quad_csi2p(
raw, w, h, stride, rot=0, target_size=(6, 4), high_quality=True, cv2_mod=cv2, np_mod=np
)
self.assertEqual(out_live.shape, (4, 4, 3)) # 8x8 -> 4x4, rot=90 keeps 4x4
self.assertEqual(out_hq.shape, (4, 6, 3)) # resized to WxH = 6x4
# Top row in rot=0 should have R > B; bottom row should have B > R.
self.assertGreater(float(out_hq[0, :, 2].mean()), float(out_hq[0, :, 0].mean()) + 30.0)
self.assertGreater(float(out_hq[3, :, 0].mean()), float(out_hq[3, :, 2].mean()) + 30.0)
if __name__ == "__main__":
unittest.main()