- 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
+136
-18
@@ -509,6 +509,63 @@ def camera_problem(device):
|
||||
return ""
|
||||
|
||||
|
||||
QUAD_BAYER_MODELS = {"imx371"}
|
||||
|
||||
|
||||
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 debayer_quad_csi2p(
|
||||
raw_buf,
|
||||
width,
|
||||
height,
|
||||
stride,
|
||||
rot=0,
|
||||
offset=0,
|
||||
target_size=(640, 480),
|
||||
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
|
||||
b5 = arr[:even_h:2, :row_bytes].reshape((even_h // 2, width // 4, 5))
|
||||
bayer = np_mod.ascontiguousarray(b5[:, :, [0, 2]].reshape((even_h // 2, width // 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))
|
||||
|
||||
|
||||
class LibcameraCamera:
|
||||
"""Capture BGR frames via python-libcamera (MIPI/SoftISP phone cameras)."""
|
||||
|
||||
@@ -524,31 +581,67 @@ class LibcameraCamera:
|
||||
self.cv2, self.libcamera, self.mmap, self.np = cv2, libcamera, mmap, np
|
||||
reuse_enum = getattr(libcamera.Request, "Reuse", None) or getattr(libcamera.Request, "ReuseFlag", None)
|
||||
self._reuse_arg = getattr(reuse_enum, "ReuseBuffers", None) if reuse_enum is not None else 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.sc = None
|
||||
self.raw_sc = None
|
||||
self.alloc = None
|
||||
self.requests = []
|
||||
self._started = False
|
||||
|
||||
cid = device[len("libcamera:"):]
|
||||
self.cm = libcamera.CameraManager.singleton()
|
||||
self.cam = self.cm.get(cid)
|
||||
if self.cam is None:
|
||||
raise Fail("camera %s disappeared" % device, 2)
|
||||
self.rot = int(self.cam.properties.get(libcamera.properties.Rotation) or 0)
|
||||
self.model = str(self.cam.properties.get(libcamera.properties.Model) or "").lower()
|
||||
try:
|
||||
self.cam.acquire()
|
||||
except Exception as e:
|
||||
raise Fail("couldn't acquire camera %s: %s" % (device, e), 2)
|
||||
try:
|
||||
cfg = self.cam.generate_configuration([libcamera.StreamRole.Viewfinder])
|
||||
self.sc = cfg.at(0)
|
||||
self.sc.size = libcamera.Size(640, 480)
|
||||
cfg.validate()
|
||||
self.cam.configure(cfg)
|
||||
if self.model in QUAD_BAYER_MODELS:
|
||||
try:
|
||||
cfg = self.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()
|
||||
self.cam.configure(cfg)
|
||||
self.sc = cfg.at(0)
|
||||
self.raw_sc = cfg.at(1)
|
||||
except Exception:
|
||||
self.raw_sc = None
|
||||
|
||||
if self.raw_sc is None:
|
||||
cfg = self.cam.generate_configuration([libcamera.StreamRole.Viewfinder])
|
||||
self.sc = cfg.at(0)
|
||||
self.sc.size = libcamera.Size(640, 480)
|
||||
cfg.validate()
|
||||
self.cam.configure(cfg)
|
||||
|
||||
self.alloc = libcamera.FrameBufferAllocator(self.cam)
|
||||
if self.alloc.allocate(self.sc.stream) <= 0:
|
||||
raise Fail("couldn't allocate camera buffers for %s" % device, 2)
|
||||
self.requests = []
|
||||
for buf in self.alloc.buffers(self.sc.stream):
|
||||
r = self.cam.create_request()
|
||||
r.add_buffer(self.sc.stream, buf)
|
||||
self.requests.append(r)
|
||||
if self.raw_sc is not None:
|
||||
if self.alloc.allocate(self.raw_sc.stream) <= 0:
|
||||
raise Fail("couldn't allocate raw buffers for %s" % device, 2)
|
||||
for b0, b1 in zip(self.alloc.buffers(self.sc.stream), self.alloc.buffers(self.raw_sc.stream)):
|
||||
r = self.cam.create_request()
|
||||
r.add_buffer(self.sc.stream, b0)
|
||||
r.add_buffer(self.raw_sc.stream, b1)
|
||||
self.requests.append(r)
|
||||
else:
|
||||
for buf in self.alloc.buffers(self.sc.stream):
|
||||
r = self.cam.create_request()
|
||||
r.add_buffer(self.sc.stream, buf)
|
||||
self.requests.append(r)
|
||||
self.cm.get_ready_requests()
|
||||
self.cam.start()
|
||||
self._started = True
|
||||
for r in self.requests:
|
||||
self.cam.queue_request(r)
|
||||
except Exception as e:
|
||||
@@ -573,13 +666,27 @@ class LibcameraCamera:
|
||||
bgr = self.np.ascontiguousarray(img[:, :, ::-1]) if fmt == "RGB888" else self.np.ascontiguousarray(img)
|
||||
else:
|
||||
raise Fail("unsupported libcamera pixel format %s" % fmt, 2)
|
||||
if self.rot == 90:
|
||||
return self.cv2.rotate(bgr, self.cv2.ROTATE_90_CLOCKWISE)
|
||||
if self.rot == 180:
|
||||
return self.cv2.rotate(bgr, self.cv2.ROTATE_180)
|
||||
if self.rot == 270:
|
||||
return self.cv2.rotate(bgr, self.cv2.ROTATE_90_COUNTERCLOCKWISE)
|
||||
return bgr
|
||||
return self.np.ascontiguousarray(rotate_bgr(bgr, self.rot, self.cv2))
|
||||
|
||||
def _raw_to_bgr(self, buf):
|
||||
p = buf.planes[0]
|
||||
w, h, stride = self.raw_sc.size.width, self.raw_sc.size.height, self.raw_sc.stride
|
||||
fmt = str(self.raw_sc.pixel_format)
|
||||
if "CSI2P" not in fmt.upper():
|
||||
raise Fail("unsupported quad raw format %s" % fmt, 2)
|
||||
with self.mmap.mmap(p.fd, p.offset + p.length, self.mmap.MAP_SHARED, self.mmap.PROT_READ) as mm:
|
||||
return debayer_quad_csi2p(
|
||||
mm,
|
||||
w,
|
||||
h,
|
||||
stride,
|
||||
rot=self.rot,
|
||||
offset=p.offset,
|
||||
target_size=(640, 480),
|
||||
gamma_lut=self._gamma_lut,
|
||||
cv2_mod=self.cv2,
|
||||
np_mod=self.np,
|
||||
)
|
||||
|
||||
def _requeue(self, req):
|
||||
if self._reuse_arg is not None:
|
||||
@@ -605,7 +712,10 @@ class LibcameraCamera:
|
||||
frame = None
|
||||
try:
|
||||
if req.status == self.libcamera.Request.Status.Complete:
|
||||
frame = self._to_bgr(req.buffers[self.sc.stream])
|
||||
if self.raw_sc is not None and self.raw_sc.stream in req.buffers:
|
||||
frame = self._raw_to_bgr(req.buffers[self.raw_sc.stream])
|
||||
elif self.sc is not None and self.sc.stream in req.buffers:
|
||||
frame = self._to_bgr(req.buffers[self.sc.stream])
|
||||
finally:
|
||||
self._requeue(req)
|
||||
if frame is not None:
|
||||
@@ -615,11 +725,19 @@ class LibcameraCamera:
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
self.cam.stop()
|
||||
if getattr(self, "_started", False):
|
||||
self.cam.stop()
|
||||
except Exception:
|
||||
pass
|
||||
self._started = False
|
||||
try:
|
||||
self.cm.get_ready_requests()
|
||||
except Exception:
|
||||
pass
|
||||
self.requests = []
|
||||
self.alloc = None
|
||||
self.sc = None
|
||||
self.raw_sc = None
|
||||
try:
|
||||
self.cam.release()
|
||||
except Exception:
|
||||
|
||||
Reference in new issue
Block a user