- 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
This commit is contained in:
1 parent
de39b74282
commit
bfc60ee0e5
24 files changed
+5310
-79
No files matched your search
+162
-32
@@ -24,6 +24,8 @@ FLASH_LED_PATH = "/sys/class/leds/white:flash/brightness"
|
||||
IMAGE_EXTS = (".jpg", ".jpeg", ".png", ".webp")
|
||||
VIDEO_EXTS = (".mp4", ".mkv", ".webm")
|
||||
|
||||
QUAD_BAYER_MODELS = {"imx371"}
|
||||
|
||||
# Fallback resolutions when a camera reports no enumerated StillCapture sizes.
|
||||
DEFAULT_VIEWFINDER_SIZE = (1280, 960)
|
||||
PREFERRED_STILL_SIZES = [
|
||||
@@ -212,6 +214,18 @@ def select_initial_camera(cameras, spec):
|
||||
return 0
|
||||
|
||||
|
||||
def rotate_bgr(bgr, rot, cv2_mod):
|
||||
"""Rotate BGR frame according to libcamera.properties.Rotation (CCW mounting angle)."""
|
||||
rot_i = int(rot) % 360
|
||||
if rot_i == 90:
|
||||
return cv2_mod.rotate(bgr, cv2_mod.ROTATE_90_COUNTERCLOCKWISE)
|
||||
if rot_i == 180:
|
||||
return cv2_mod.rotate(bgr, cv2_mod.ROTATE_180)
|
||||
if rot_i == 270:
|
||||
return cv2_mod.rotate(bgr, cv2_mod.ROTATE_90_CLOCKWISE)
|
||||
return bgr
|
||||
|
||||
|
||||
def convert_frame(raw, width, height, stride, fmt, rot=0, cv2_mod=None, np_mod=None):
|
||||
"""Convert raw camera buffer bytes into a contiguous upright uint8 BGR image."""
|
||||
if np_mod is None:
|
||||
@@ -238,14 +252,64 @@ def convert_frame(raw, width, height, stride, fmt, rot=0, cv2_mod=None, np_mod=N
|
||||
else:
|
||||
raise RuntimeError("unsupported pixel format %s" % fmt)
|
||||
|
||||
rot_i = int(rot) % 360
|
||||
if rot_i == 90:
|
||||
return np_mod.ascontiguousarray(cv2_mod.rotate(bgr, cv2_mod.ROTATE_90_CLOCKWISE))
|
||||
if rot_i == 180:
|
||||
return np_mod.ascontiguousarray(cv2_mod.rotate(bgr, cv2_mod.ROTATE_180))
|
||||
if rot_i == 270:
|
||||
return np_mod.ascontiguousarray(cv2_mod.rotate(bgr, cv2_mod.ROTATE_90_COUNTERCLOCKWISE))
|
||||
return bgr
|
||||
return np_mod.ascontiguousarray(rotate_bgr(bgr, rot, cv2_mod))
|
||||
|
||||
|
||||
def debayer_quad_csi2p(
|
||||
raw_buf,
|
||||
width,
|
||||
height,
|
||||
stride,
|
||||
rot=0,
|
||||
offset=0,
|
||||
target_size=(960, 720),
|
||||
high_quality=False,
|
||||
gamma_lut=None,
|
||||
cv2_mod=None,
|
||||
np_mod=None,
|
||||
):
|
||||
"""Debayer 10-bit CSI-2 packed 4x4 Quad-Bayer SRGGB into upright BGR uint8."""
|
||||
if np_mod is None:
|
||||
import numpy as np_mod
|
||||
if cv2_mod is None:
|
||||
import cv2 as cv2_mod
|
||||
|
||||
arr = np_mod.frombuffer(raw_buf, dtype=np_mod.uint8, count=height * stride, offset=offset).reshape((height, stride))
|
||||
row_bytes = (width // 4) * 5
|
||||
even_h = (height // 2) * 2
|
||||
even_w = (width // 4) * 4
|
||||
if high_quality:
|
||||
p8 = arr[:even_h, :row_bytes].reshape((even_h, even_w // 4, 5))[:, :, :4].reshape((even_h, even_w))
|
||||
bayer = (
|
||||
(
|
||||
p8[0::2, 0::2].astype(np_mod.uint16)
|
||||
+ p8[0::2, 1::2]
|
||||
+ p8[1::2, 0::2]
|
||||
+ p8[1::2, 1::2]
|
||||
)
|
||||
>> 2
|
||||
).astype(np_mod.uint8)
|
||||
else:
|
||||
b5 = arr[:even_h:2, :row_bytes].reshape((even_h // 2, even_w // 4, 5))
|
||||
bayer = np_mod.ascontiguousarray(b5[:, :, [0, 2]].reshape((even_h // 2, even_w // 2)))
|
||||
|
||||
bgr = cv2_mod.cvtColor(bayer, cv2_mod.COLOR_BayerBG2BGR)
|
||||
if target_size is not None:
|
||||
bgr = cv2_mod.resize(bgr, target_size, interpolation=cv2_mod.INTER_AREA)
|
||||
|
||||
f = np_mod.maximum(bgr.astype(np_mod.float32) - 16.0, 0.0)
|
||||
means = f.mean(axis=(0, 1))
|
||||
avg = float(means.mean())
|
||||
if avg > 1.0:
|
||||
gains = np_mod.clip(avg / np_mod.maximum(means, 1.0), 0.65, 2.2)
|
||||
boost = float(np_mod.clip(75.0 / avg, 1.0, 3.8))
|
||||
u8 = np_mod.clip(f * (gains * boost), 0, 255).astype(np_mod.uint8)
|
||||
else:
|
||||
u8 = np_mod.clip(f, 0, 255).astype(np_mod.uint8)
|
||||
if gamma_lut is None:
|
||||
gamma_lut = np_mod.clip((np_mod.linspace(0.0, 1.0, 256) ** (1.0 / 2.2)) * 255.0, 0, 255).astype(np_mod.uint8)
|
||||
out = cv2_mod.LUT(u8, gamma_lut)
|
||||
return np_mod.ascontiguousarray(rotate_bgr(out, rot, cv2_mod))
|
||||
|
||||
|
||||
def bgr_to_cairo_argb32(bgr, np_mod=None):
|
||||
@@ -355,11 +419,13 @@ class CameraEngine:
|
||||
|
||||
self._cam = None
|
||||
self._sc = None
|
||||
self._raw_sc = None
|
||||
self._alloc = None
|
||||
self._requests = []
|
||||
self._v4l2 = None
|
||||
self._started = False
|
||||
self._reuse_arg = None
|
||||
self._gamma_lut = np.clip((np.linspace(0.0, 1.0, 256) ** (1.0 / 2.2)) * 255.0, 0, 255).astype(np.uint8)
|
||||
|
||||
self._latest_bgr = None
|
||||
self._latest_seq = 0
|
||||
@@ -463,34 +529,61 @@ class CameraEngine:
|
||||
cam.acquire()
|
||||
self._cam = cam
|
||||
try:
|
||||
role = libcamera.StreamRole.StillCapture if role_name == "still" else libcamera.StreamRole.Viewfinder
|
||||
sizes = target_sizes or ([DEFAULT_VIEWFINDER_SIZE] if role_name == "viewfinder" else PREFERRED_STILL_SIZES)
|
||||
chosen_exc = None
|
||||
self._raw_sc = None
|
||||
configured = False
|
||||
for w, h in sizes:
|
||||
if role_name == "viewfinder" and info.model.lower() in QUAD_BAYER_MODELS:
|
||||
try:
|
||||
cfg = cam.generate_configuration([role])
|
||||
sc = cfg.at(0)
|
||||
sc.size = libcamera.Size(w, h)
|
||||
cfg.validate()
|
||||
cam.configure(cfg)
|
||||
self._sc = sc
|
||||
configured = True
|
||||
break
|
||||
except Exception as exc:
|
||||
chosen_exc = exc
|
||||
cfg = cam.generate_configuration([
|
||||
libcamera.StreamRole.Viewfinder,
|
||||
libcamera.StreamRole.Raw,
|
||||
])
|
||||
if cfg is not None and len(cfg) >= 2:
|
||||
cfg.at(0).size = libcamera.Size(640, 480)
|
||||
cfg.validate()
|
||||
cam.configure(cfg)
|
||||
self._sc = cfg.at(0)
|
||||
self._raw_sc = cfg.at(1)
|
||||
configured = True
|
||||
except Exception:
|
||||
self._raw_sc = None
|
||||
|
||||
if not configured:
|
||||
raise RuntimeError("failed to configure %s: %s" % (info.label, chosen_exc))
|
||||
role = libcamera.StreamRole.StillCapture if role_name == "still" else libcamera.StreamRole.Viewfinder
|
||||
sizes = target_sizes or ([DEFAULT_VIEWFINDER_SIZE] if role_name == "viewfinder" else PREFERRED_STILL_SIZES)
|
||||
chosen_exc = None
|
||||
for w, h in sizes:
|
||||
try:
|
||||
cfg = cam.generate_configuration([role])
|
||||
sc = cfg.at(0)
|
||||
sc.size = libcamera.Size(w, h)
|
||||
cfg.validate()
|
||||
cam.configure(cfg)
|
||||
self._sc = sc
|
||||
configured = True
|
||||
break
|
||||
except Exception as exc:
|
||||
chosen_exc = exc
|
||||
if not configured:
|
||||
raise RuntimeError("failed to configure %s: %s" % (info.label, chosen_exc))
|
||||
|
||||
alloc = libcamera.FrameBufferAllocator(cam)
|
||||
if alloc.allocate(self._sc.stream) <= 0:
|
||||
raise RuntimeError("failed to allocate camera buffers for %s" % info.label)
|
||||
self._alloc = alloc
|
||||
reqs = []
|
||||
for buf in alloc.buffers(self._sc.stream):
|
||||
r = cam.create_request()
|
||||
r.add_buffer(self._sc.stream, buf)
|
||||
reqs.append(r)
|
||||
if self._raw_sc is not None:
|
||||
if alloc.allocate(self._raw_sc.stream) <= 0:
|
||||
raise RuntimeError("failed to allocate raw buffers for %s" % info.label)
|
||||
for b0, b1 in zip(alloc.buffers(self._sc.stream), alloc.buffers(self._raw_sc.stream)):
|
||||
r = cam.create_request()
|
||||
r.add_buffer(self._sc.stream, b0)
|
||||
r.add_buffer(self._raw_sc.stream, b1)
|
||||
reqs.append(r)
|
||||
else:
|
||||
for buf in alloc.buffers(self._sc.stream):
|
||||
r = cam.create_request()
|
||||
r.add_buffer(self._sc.stream, buf)
|
||||
reqs.append(r)
|
||||
self._requests = reqs
|
||||
# Drain any stale completed requests before starting the newly acquired sensor.
|
||||
self.cm.get_ready_requests()
|
||||
@@ -525,20 +618,43 @@ class CameraEngine:
|
||||
self._requests = []
|
||||
self._alloc = None
|
||||
self._sc = None
|
||||
self._raw_sc = None
|
||||
try:
|
||||
self._cam.release()
|
||||
except Exception:
|
||||
pass
|
||||
self._cam = None
|
||||
|
||||
def _read_libcamera_frame(self, cam_info):
|
||||
def _read_libcamera_frame(self, cam_info, high_quality=False, target_size=(960, 720)):
|
||||
ready = self.cm.get_ready_requests()
|
||||
if not ready:
|
||||
return None
|
||||
frame = None
|
||||
for req in ready:
|
||||
if self._sc is not None and self._sc.stream in req.buffers:
|
||||
if req.status == self.libcamera.Request.Status.Complete:
|
||||
if req.status == self.libcamera.Request.Status.Complete:
|
||||
if self._raw_sc is not None and self._raw_sc.stream in req.buffers:
|
||||
buf = req.buffers[self._raw_sc.stream]
|
||||
plane = buf.planes[0]
|
||||
w = self._raw_sc.size.width
|
||||
h = self._raw_sc.size.height
|
||||
stride = self._raw_sc.stride
|
||||
fmt = str(self._raw_sc.pixel_format)
|
||||
if "CSI2P" in fmt.upper():
|
||||
with mmap.mmap(plane.fd, plane.offset + plane.length, mmap.MAP_SHARED, mmap.PROT_READ) as mm:
|
||||
frame = debayer_quad_csi2p(
|
||||
mm,
|
||||
w,
|
||||
h,
|
||||
stride,
|
||||
rot=cam_info.rotation,
|
||||
offset=plane.offset,
|
||||
target_size=target_size,
|
||||
high_quality=high_quality,
|
||||
gamma_lut=self._gamma_lut,
|
||||
cv2_mod=self.cv2,
|
||||
np_mod=self.np,
|
||||
)
|
||||
elif self._sc is not None and self._sc.stream in req.buffers:
|
||||
buf = req.buffers[self._sc.stream]
|
||||
plane = buf.planes[0]
|
||||
w = self._sc.size.width
|
||||
@@ -557,7 +673,7 @@ class CameraEngine:
|
||||
cv2_mod=self.cv2,
|
||||
np_mod=self.np,
|
||||
)
|
||||
self._requeue(req)
|
||||
self._requeue(req)
|
||||
return frame
|
||||
|
||||
def _run_loop(self):
|
||||
@@ -630,7 +746,21 @@ class CameraEngine:
|
||||
def _perform_still_capture(self, dst_path):
|
||||
info = self.active_camera
|
||||
try:
|
||||
if info.backend == "libcamera":
|
||||
if info.backend == "libcamera" and self._raw_sc is not None:
|
||||
captured = None
|
||||
deadline = time.monotonic() + 4.0
|
||||
while time.monotonic() < deadline and not self._stop_event.is_set():
|
||||
captured = self._read_libcamera_frame(info, high_quality=True, target_size=None)
|
||||
if captured is not None:
|
||||
break
|
||||
time.sleep(0.01)
|
||||
if captured is None:
|
||||
raise RuntimeError("still capture timed out")
|
||||
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
||||
if not self.cv2.imwrite(dst_path, captured, [self.cv2.IMWRITE_JPEG_QUALITY, 95]):
|
||||
raise RuntimeError("failed to write %s" % dst_path)
|
||||
self._still_result = (dst_path, None)
|
||||
elif info.backend == "libcamera":
|
||||
self._close_camera()
|
||||
self._open_camera(info, role_name="still", target_sizes=PREFERRED_STILL_SIZES)
|
||||
captured = None
|
||||
|
||||
+42
-23
@@ -1105,43 +1105,53 @@ static gboolean start_worker_process(CameraApp *app) {
|
||||
}
|
||||
|
||||
static void build_ui(CameraApp *app) {
|
||||
GtkSettings *settings = gtk_settings_get_default();
|
||||
if (settings) {
|
||||
g_object_set(settings, "gtk-application-prefer-dark-theme", TRUE, NULL);
|
||||
}
|
||||
|
||||
const char *css =
|
||||
"window, .cam-root { background-color: #0b0d14; color: #f4f6fc; }\n"
|
||||
".cam-pill { background-color: rgba(14, 18, 30, 0.74); color: #f4f6fc;\n"
|
||||
" border: 1px solid rgba(255, 255, 255, 0.16); border-radius: 18px;\n"
|
||||
" padding: 5px 12px; font-size: 13px; font-weight: 600; min-height: 24px; }\n"
|
||||
".cam-pill:hover { background-color: rgba(30, 38, 60, 0.88); }\n"
|
||||
".cam-pill-active { background-color: rgba(110, 140, 255, 0.88);\n"
|
||||
" color: #090d16; border-color: rgba(180, 200, 255, 0.90); }\n"
|
||||
".cam-pill-danger { background-color: rgba(239, 68, 68, 0.88);\n"
|
||||
" color: #ffffff; border-color: rgba(255, 180, 180, 0.80); }\n"
|
||||
".cam-dock { background-color: rgba(10, 12, 20, 0.86);\n"
|
||||
" border-top: 1px solid rgba(255, 255, 255, 0.10); padding: 10px 16px 14px 16px; }\n"
|
||||
".cam-mode-bar { background-color: rgba(255, 255, 255, 0.08);\n"
|
||||
"window, .cam-root { background-color: #0b0d14; background-image: none; color: #f4f6fc; }\n"
|
||||
"button.cam-pill { background-color: rgba(14, 18, 30, 0.82); background-image: none; color: #f4f6fc;\n"
|
||||
" border: 1px solid rgba(255, 255, 255, 0.22); border-radius: 18px;\n"
|
||||
" box-shadow: none; text-shadow: none; -gtk-icon-shadow: none;\n"
|
||||
" padding: 4px 10px; font-size: 12px; font-weight: 600; min-height: 24px; }\n"
|
||||
"button.cam-pill label { color: inherit; }\n"
|
||||
"button.cam-pill:hover { background-color: rgba(34, 43, 68, 0.92); background-image: none; }\n"
|
||||
"button.cam-pill.cam-pill-active { background-color: #6e8cff; background-image: none;\n"
|
||||
" color: #090d16; border-color: rgba(180, 200, 255, 0.95); }\n"
|
||||
"button.cam-pill.cam-pill-danger { background-color: #ef4444; background-image: none;\n"
|
||||
" color: #ffffff; border-color: rgba(255, 180, 180, 0.85); }\n"
|
||||
".cam-dock { background-color: rgba(10, 12, 20, 0.88); background-image: none;\n"
|
||||
" border-top: 1px solid rgba(255, 255, 255, 0.12); padding: 10px 14px 14px 14px; }\n"
|
||||
".cam-mode-bar { background-color: rgba(255, 255, 255, 0.10); background-image: none;\n"
|
||||
" border-radius: 18px; padding: 3px; }\n"
|
||||
".cam-mode-btn { background-color: transparent; color: #c2cadc; border: none;\n"
|
||||
"button.cam-mode-btn { background-color: transparent; background-image: none; color: #c2cadc;\n"
|
||||
" border: none; box-shadow: none; text-shadow: none; -gtk-icon-shadow: none;\n"
|
||||
" border-radius: 15px; padding: 4px 18px; font-size: 13px; font-weight: 600; min-height: 22px; }\n"
|
||||
".cam-mode-selected { background-color: #6e8cff; color: #0b0d14; }\n"
|
||||
".cam-mode-selected-video { background-color: #ef4444; color: #ffffff; }\n"
|
||||
".cam-rec-badge { background-color: rgba(239, 68, 68, 0.90); color: #ffffff;\n"
|
||||
"button.cam-mode-btn label { color: inherit; }\n"
|
||||
"button.cam-mode-btn.cam-mode-selected { background-color: #6e8cff; background-image: none; color: #0b0d14; }\n"
|
||||
"button.cam-mode-btn.cam-mode-selected-video { background-color: #ef4444; background-image: none; color: #ffffff; }\n"
|
||||
".cam-rec-badge { background-color: rgba(239, 68, 68, 0.92); color: #ffffff;\n"
|
||||
" border-radius: 12px; padding: 2px 10px; font-size: 12px; font-weight: 700; }\n"
|
||||
".cam-icon-btn { background-color: transparent; border: none; padding: 0; box-shadow: none; }\n"
|
||||
".cam-gallery-bar { background-color: #111523;\n"
|
||||
" border-bottom: 1px solid rgba(255, 255, 255, 0.10); padding: 8px 12px; }\n"
|
||||
".cam-gallery-foot { background-color: #111523;\n"
|
||||
" border-top: 1px solid rgba(255, 255, 255, 0.10); padding: 8px 14px; }\n"
|
||||
"button.cam-icon-btn, button.cam-icon-btn:hover { background-color: transparent; background-image: none;\n"
|
||||
" border: none; padding: 0; box-shadow: none; outline: none; }\n"
|
||||
".cam-gallery-bar { background-color: #111523; background-image: none;\n"
|
||||
" border-bottom: 1px solid rgba(255, 255, 255, 0.12); padding: 8px 10px; }\n"
|
||||
".cam-gallery-foot { background-color: #111523; background-image: none;\n"
|
||||
" border-top: 1px solid rgba(255, 255, 255, 0.12); padding: 8px 12px; }\n"
|
||||
".cam-title { font-size: 14px; font-weight: 700; color: #f4f6fc; }\n"
|
||||
".cam-sub { font-size: 12px; color: #9ba6c2; }\n";
|
||||
|
||||
GtkCssProvider *prov = gtk_css_provider_new();
|
||||
gtk_css_provider_load_from_data(prov, css, -1, NULL);
|
||||
gtk_style_context_add_provider_for_screen(
|
||||
gdk_screen_get_default(), GTK_STYLE_PROVIDER(prov), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
gdk_screen_get_default(), GTK_STYLE_PROVIDER(prov), GTK_STYLE_PROVIDER_PRIORITY_USER);
|
||||
g_object_unref(prov);
|
||||
|
||||
app->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||
gtk_window_set_title(GTK_WINDOW(app->window), "Camera");
|
||||
gtk_window_set_default_size(GTK_WINDOW(app->window), 520, 960);
|
||||
gtk_window_set_default_size(GTK_WINDOW(app->window), 480, 860);
|
||||
gtk_widget_set_size_request(app->window, 340, 480);
|
||||
gtk_style_context_add_class(gtk_widget_get_style_context(app->window), "cam-root");
|
||||
|
||||
@@ -1372,6 +1382,15 @@ int main(int argc, char **argv) {
|
||||
sync_ui_labels(&app);
|
||||
|
||||
gtk_widget_show_all(app.window);
|
||||
GdkDisplay *dpy = gdk_display_get_default();
|
||||
GdkMonitor *mon = dpy ? gdk_display_get_monitor(dpy, 0) : NULL;
|
||||
if (mon) {
|
||||
GdkRectangle geo;
|
||||
gdk_monitor_get_geometry(mon, &geo);
|
||||
if (geo.width <= 900 || geo.height > geo.width) {
|
||||
gtk_window_maximize(GTK_WINDOW(app.window));
|
||||
}
|
||||
}
|
||||
if (app.open_gallery_start) {
|
||||
open_gallery_view(&app);
|
||||
} else {
|
||||
|
||||
+41
-4
@@ -56,13 +56,13 @@ class TestCameraHelpers(unittest.TestCase):
|
||||
|
||||
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))
|
||||
# Clockwise 90 deg puts original (row=0, col=0) at (row=0, col=h-1=2)
|
||||
self.assertEqual(tuple(bgr90[0, 2]), (200, 20, 10))
|
||||
# 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))
|
||||
# Counter-clockwise 90 deg puts original (row=0, col=0) at (row=w-1=1, col=0)
|
||||
self.assertEqual(tuple(bgr270[1, 0]), (200, 20, 10))
|
||||
# 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):
|
||||
@@ -87,6 +87,43 @@ class TestCameraHelpers(unittest.TestCase):
|
||||
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()
|
||||
Reference in new issue
Block a user