Files
Lumi/vision/camview.py
T
2026-11-18 18:37:10 +09:00

446 lines
18 KiB
Python

"""Camera service: the ONLY program that opens the Orbbec cameras; everyone else reads its results.
Two programs opening Orbbec cameras at the same time make the SDK reset the devices: the head camera then
disconnects every ~2.5 s (seen 2026-10-12). So one service owns both cameras:
python vision/camview.py --serve # started by api/server.py at boot (and by lumi.py if it is not running)
python vision/camview.py --stop # free the cameras, e.g. for camera_fps.py / camera_test.py; it stays off
# (nothing restarts it) until `python vision/camview.py --serve` or --start
For each camera it runs person detection (person.py) and writes to /dev/shm:
- lumi_cam_<name>.jpg / .json picture with boxes (PUBLISH_HZ) + people, for the dashboard (api/server.py /view)
- lumi_det_<name>.json every detection frame: people with boxes, frame size, head yaw (RemoteDetector);
for the torso also objects (cups, bottles...) measured by objects.py
- lumi_raw_<name>.jpg the camera's own full-size JPEG of every frame (live video, calib.py)
- lumi_intr_<name>.json colour intrinsics of the running mode (calib.py, objects)
Programs that need people (lumi.py, api/server.py) use RemoteDetector, which has the same read() as
person.PersonDetector. They tell the service whom to draw red (focus) through lumi_focus_<name>.json.
Nobody reading for IDLE_S (no viewer, no lumi.py): detection drops to 1 frame a second.
"""
import io
import json
import os
import signal
import subprocess
import sys
import threading
import time
from pathlib import Path
import cv2
import numpy as np
ROOT = Path(__file__).resolve().parents[1] # the project folder
VENV_PY = ROOT / ".venv" / "bin" / "python"
SHM = Path("/dev/shm")
PID_FILE = SHM / "lumi_camd.pid"
STATUS_FILE = SHM / "lumi_camd.json"
OFF_FILE = SHM / "lumi_camd.off" # written by --stop: do not restart the service automatically
SETTINGS = SHM / "lumi_view_settings.json"
CAMERAS = ("head", "torso")
PUBLISH_HZ = 5.0
VIEW_WIDTH = 640 # px, pictures are scaled down to this width
FRESH_S = 1.5 # s; a newer picture counts as live
IDLE_S = 30.0 # s without any reader -> slow detection (1 per second)
DEFAULTS = {"box_color": "#22c55e", "thickness": 2, "boxes": True, "labels": True}
FOCUS_COLOR = (68, 68, 239) # BGR red: the person Lumi talks to
DEPTH_MIN_MBPS = 5000 # head depth only at USB 3 speed: at USB 2, depth + the speaker playing (same hub) = disconnects
FOCUS_FRESH_S = 3.0 # s; an older focus file (lumi.py gone) is ignored
DEPTH_EVERY_S = 0.5 # s between torso depth frames written for the skills (is the table there?)
MATCH_DEG = 8.0 # a torso-camera person within this angle gives the head person's distance
_last = {}
_settings = {"time": 0.0, "value": dict(DEFAULTS)}
def frame_path(name):
return SHM / f"lumi_cam_{name}.jpg"
def info_path(name):
return SHM / f"lumi_cam_{name}.json"
def raw_path(name):
"""The camera's own JPEG of the newest frame (no boxes): the dashboard's live video."""
return SHM / f"lumi_raw_{name}.jpg"
def intrinsics_path(name):
"""Colour camera matrix and distortion of the running mode, written when the camera opens."""
return SHM / f"lumi_intr_{name}.json"
def intrinsics(name):
"""{width, height, fx, fy, cx, cy, dist} of a camera opened by the service, or None."""
try:
return json.loads(intrinsics_path(name).read_text())
except (FileNotFoundError, ValueError):
return None
def usb_mbps(serial):
"""Link speed (Mb/s) of the USB device with this serial number, 0 if not found (Linux sysfs)."""
for d in Path("/sys/bus/usb/devices").glob("*"):
try:
if (d / "serial").read_text().strip() == serial:
return int((d / "speed").read_text())
except (OSError, ValueError):
continue
return 0
def det_path(name):
return SHM / f"lumi_det_{name}.json"
def depth_path(name):
"""Depth (mm, uint16, aligned to the full-size colour frame) of a recent frame, .npy; torso only."""
return SHM / f"lumi_depth_{name}.npy"
def depth(name, max_age=FRESH_S):
"""The newest depth frame written by the service, or None when there is none this fresh."""
try:
if time.time() - depth_path(name).stat().st_mtime > max_age:
return None
return np.load(depth_path(name))
except (FileNotFoundError, ValueError, OSError):
return None
def focus_path(name):
return SHM / f"lumi_focus_{name}.json"
def want_path(name):
return SHM / f"lumi_cam_{name}.want"
def _write(path, data):
tmp = path.with_suffix(path.suffix + f".{os.getpid()}.tmp")
tmp.write_bytes(data)
os.replace(tmp, path) # atomic: a reader never sees half a file
def want(name):
"""A reader (dashboard, lumi.py) is interested in this camera: keep full speed."""
try:
want_path(name).touch()
except OSError:
pass
# ---------- drawing settings (dashboard) ----------
def settings():
now = time.time()
if now - _settings["time"] > 1.0:
_settings["time"] = now
try:
_settings["value"] = {**DEFAULTS, **json.loads(SETTINGS.read_text())}
except (FileNotFoundError, ValueError):
_settings["value"] = dict(DEFAULTS)
return _settings["value"]
def save_settings(new):
_settings["time"] = 0.0
value = {**settings(), **{k: v for k, v in new.items() if k in DEFAULTS and v is not None}}
_write(SETTINGS, json.dumps(value).encode())
_settings["time"] = 0.0
return value
def _bgr(hex_color):
h = hex_color.lstrip("#")
try:
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
except ValueError:
r, g, b = 34, 197, 94
return b, g, r
def _draw(img, people, scale, k=1.0):
"""Person boxes and their distance labels on `img` (boxes are in camera pixels, x `scale` to img); `k` makes
lines and words bigger on a bigger picture (1 = the 640 px dashboard picture)."""
s = settings()
box_color, thick = _bgr(s["box_color"]), max(1, min(6, int(s["thickness"])))
line, font, pad = max(1, round(thick * k)), 0.5 * k, max(1, round(k))
for p in people:
x1, y1, x2, y2 = (int(v * scale) for v in p["box"])
col = FOCUS_COLOR if p.get("focus") or p.get("near") else box_color
if s["boxes"]:
cv2.rectangle(img, (x1, y1), (x2, y2), col, line + (pad if col == FOCUS_COLOR else 0))
if s["labels"]:
text = ("TALKING " if p.get("focus") else "") + f"{p['distance_m']} m {p['angle_deg']:+.0f} deg"
(tw, th), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, font, pad)
ty = max(th + 6 * pad, y1)
cv2.rectangle(img, (x1, ty - th - 6 * pad), (x1 + tw + 6 * pad, ty), col, -1)
cv2.putText(img, text, (x1 + 3 * pad, ty - 4 * pad), cv2.FONT_HERSHEY_SIMPLEX, font, (20, 20, 20), pad,
cv2.LINE_AA)
def picture_hd(name, quality=85):
"""The camera's newest frame at its own size (1280 x 720) with the person boxes, for a big picture (the Lumi
app's Drive): the dashboard picture is only 640 px. Without people in view the camera's own JPEG as it is."""
jpeg = raw_path(name).read_bytes()
try:
d = json.loads(det_path(name).read_text())
people = d["people"] if time.time() - d["time"] < 1.0 else []
width = d["width"]
except (FileNotFoundError, ValueError, KeyError):
people, width = [], 0
if not people or not width:
return jpeg
img = cv2.imdecode(np.frombuffer(jpeg, np.uint8), cv2.IMREAD_COLOR)
if img is None:
return jpeg
_draw(img, people, img.shape[1] / width, img.shape[1] / VIEW_WIDTH)
ok, out = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, quality])
return out.tobytes() if ok else jpeg
def publish(name, color, people=(), source="camview.py"):
"""Picture with person boxes for the dashboard (called by PersonDetector.read for every frame)."""
now = time.time()
if now - _last.get(name, 0) < 1 / PUBLISH_HZ:
return
_last[name] = now
scale = VIEW_WIDTH / color.shape[1]
img = cv2.resize(color, (VIEW_WIDTH, int(color.shape[0] * scale)))
_draw(img, people, scale)
ok, jpg = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, 75])
if not ok:
return
_write(frame_path(name), jpg.tobytes())
_write(info_path(name), json.dumps({"time": now, "source": source, "people": [
{**{k: p[k] for k in ("distance_m", "angle_deg", "score")}, "focus": bool(p.get("focus")),
"near": bool(p.get("near"))}
for p in people]}).encode())
def picture_age(name):
try:
return time.time() - frame_path(name).stat().st_mtime
except FileNotFoundError:
return None
def status():
try:
return json.loads(STATUS_FILE.read_text())
except (FileNotFoundError, ValueError):
return {}
# ---------- the service ----------
def service_pid():
try:
pid = int(PID_FILE.read_text())
os.kill(pid, 0)
if "camview.py" in Path(f"/proc/{pid}/cmdline").read_bytes().decode(errors="replace"):
return pid
except (FileNotFoundError, ValueError, ProcessLookupError, PermissionError):
pass
return None
_start_lock = threading.Lock()
def ensure_running():
"""Start the camera service if it is not running (safe to call often)."""
with _start_lock:
if service_pid() or OFF_FILE.exists():
return
subprocess.Popen([str(VENV_PY), str(ROOT / "vision" / "camview.py"), "--serve"], cwd=ROOT, stdin=subprocess.DEVNULL,
stdout=open(ROOT / "logs" / "camview.log", "a"), stderr=subprocess.STDOUT, start_new_session=True)
for _ in range(30): # wait until it has written its pid
time.sleep(0.1)
if service_pid():
return
def stop_service():
OFF_FILE.touch()
pid = service_pid()
if pid:
try:
os.kill(pid, signal.SIGTERM)
time.sleep(1.5)
if service_pid():
os.kill(pid, signal.SIGKILL)
except ProcessLookupError:
pass
return pid
def _serve():
OFF_FILE.unlink(missing_ok=True)
if service_pid():
sys.exit("camera service already running")
PID_FILE.write_text(str(os.getpid()))
signal.signal(signal.SIGTERM, lambda *_: os._exit(0)) # exit at once: pipeline.stop() can hang
cv2.setNumThreads(8) # two YOLOs share these (the Thor has 14 cores)
from vision.person import PersonDetector, CAMERAS as SERIALS
from vision import head_cam, objects
state = {name: "starting" for name in CAMERAS}
def write_status():
_write(STATUS_FILE, json.dumps({"pid": os.getpid(), "cameras": state, "time": time.time()}).encode())
latest = {} # name -> (time, people): the torso's depth helps the head
def write_raw(name, jpeg):
_write(raw_path(name), jpeg)
def torso_distance(angle):
"""Distance of the torso camera's person at about this angle (both angles are waist-frame)."""
t, people = latest.get("torso", (0, []))
if time.time() - t > 0.5:
return None
near = [p for p in people if p["distance_m"] and abs(p["angle_deg"] - angle) < MATCH_DEG]
return min(near, key=lambda p: abs(p["angle_deg"] - angle))["distance_m"] if near else None
def run(name):
det = None
last_focus_read, focus_mtime, last_depth = 0.0, None, 0.0
while True:
if det is None:
try:
depth = name != "head" or usb_mbps(SERIALS[name]) >= DEPTH_MIN_MBPS
det = PersonDetector(name, head_sign=1.0, depth=depth)
det.on_raw = write_raw # every camera frame, for the live video
if not depth: # head at USB 2: colour only, distance from the torso
print(f"{name} camera at USB 2 speed: colour only", flush=True)
det.distance_hint = torso_distance
try:
_write(intrinsics_path(name), json.dumps(det.intrinsics()).encode())
except Exception as e:
print(f"{name}: no intrinsics: {e}", flush=True)
state[name] = "ok"
except Exception as e:
state[name] = f"cannot open: {e}"
write_status()
time.sleep(5)
continue
write_status()
now = time.time()
if now - last_focus_read > 0.2: # whom lumi.py talks to (drawn red)
last_focus_read = now
try:
st = focus_path(name).stat()
if now - st.st_mtime > FOCUS_FRESH_S: # lumi.py stopped (or crashed): nobody to draw red
det.focus_angle = None
elif st.st_mtime != focus_mtime:
focus_mtime = st.st_mtime
det.focus_angle = json.loads(focus_path(name).read_text()).get("angle")
except (FileNotFoundError, ValueError):
det.focus_angle = None
try:
color, people = det.read()
except Exception as e:
state[name] = f"error: {e}"
write_status()
time.sleep(1)
continue
if color is not None:
latest[name] = (time.time(), people)
found = []
if det.objects:
try:
if name == "torso":
found = objects.measure_all(color, det.depth, det.objects)
elif det.head_pose is not None: # the head camera's pose follows the head
T = head_cam.base_from_head(*det.head_pose[2:])
if T is not None:
found = objects.measure_all(color, det.depth, det.objects, intrinsics(name), T)
except Exception as e:
print(f"objects: {e}", flush=True)
_write(det_path(name), json.dumps({
"time": time.time(), "serial": det.serial, "height": color.shape[0], "width": color.shape[1],
"head_yaw": det.head_yaw, "head_pose": det.head_pose, "bad_jpeg": det.bad_jpeg, "reopens": det.reopens, "reboots": det.reboots,
"people": people, "objects": found}).encode())
if det.depth is not None and time.time() - last_depth >= DEPTH_EVERY_S:
buf = io.BytesIO()
np.save(buf, det.depth.astype(np.uint16))
_write(depth_path(name), buf.getvalue())
last_depth = time.time()
try:
idle = time.time() - want_path(name).stat().st_mtime > IDLE_S
except FileNotFoundError:
idle = True
if idle:
time.sleep(1.0)
for name in CAMERAS:
threading.Thread(target=run, args=(name,), daemon=True).start()
time.sleep(2.0) # open the cameras one after the other
while True:
time.sleep(5)
write_status()
class RemoteDetector:
"""Reads the camera service's detections; same use as person.PersonDetector (read(), name, head_yaw...)."""
def __init__(self, camera="head", head_sign=1.0):
if camera not in CAMERAS:
raise ValueError(f"camera must be one of {CAMERAS} (the camera service owns them)")
self.name, self.head_sign = camera, head_sign
self.serial, self.head_yaw, self.bad_jpeg, self.reopens = "?", 0.0, 0, 0
self._last_time, self._last_want = 0.0, 0.0
self._focus, self._focus_written = None, 0.0
ensure_running()
@property
def focus_angle(self):
return self._focus
@focus_angle.setter
def focus_angle(self, angle):
angle = None if angle is None else round(float(angle), 1)
if angle != self._focus or time.time() - self._focus_written > 1.0: # heartbeat: see FOCUS_FRESH_S
self._focus, self._focus_written = angle, time.time()
for name in CAMERAS: # waist-frame angle: the same person in both cameras
_write(focus_path(name), json.dumps({"angle": angle}).encode())
def read(self, timeout=0.3):
"""(frame stand-in with .shape, people) for a new detection frame, or (None, []) if none came."""
now = time.time()
if now - self._last_want > 1.0:
self._last_want = now
want(self.name)
if not service_pid():
ensure_running()
end = now + timeout
while time.time() < end:
try:
if det_path(self.name).stat().st_mtime > self._last_time:
d = json.loads(det_path(self.name).read_text())
if d["time"] > self._last_time:
self._last_time = d["time"]
self.serial, self.head_yaw = d["serial"], d["head_yaw"]
self.bad_jpeg, self.reopens = d["bad_jpeg"], d["reopens"]
return np.empty((d["height"], 0), np.uint8), d["people"]
except (FileNotFoundError, ValueError, KeyError):
pass
time.sleep(0.01)
return None, []
def close(self):
self.focus_angle = None
if __name__ == "__main__":
if "--serve" in sys.argv:
_serve()
elif "--stop" in sys.argv:
print(("stopped" if stop_service() else "not running") + "; it stays off until: python vision/camview.py --start")
elif "--start" in sys.argv:
OFF_FILE.unlink(missing_ok=True)
ensure_running()
print({"pid": service_pid()})
else:
print({"pid": service_pid(), **status()})