Add typed visual CultCache pipeline
This commit is contained in:
parent
7f811d5e5e
commit
48cbd2fc9a
14 changed files with 634 additions and 27 deletions
103
scripts/live_sensor_fusion.py
Normal file
103
scripts/live_sensor_fusion.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from localcast.sensor_fusion import (
|
||||
CameraModel,
|
||||
FusionConfig,
|
||||
Observation2D,
|
||||
RenderBridgeConfig,
|
||||
SensorRig,
|
||||
put_live_render_frame,
|
||||
lower_points_to_render_frame,
|
||||
)
|
||||
|
||||
|
||||
def camera(sensor_id: str, x: float) -> CameraModel:
|
||||
return CameraModel(
|
||||
sensor_id=sensor_id,
|
||||
camera_matrix=np.array([[300.0, 0.0, 160.0], [0.0, 300.0, 120.0], [0.0, 0.0, 1.0]]),
|
||||
dist_coeffs=np.zeros(5),
|
||||
world_from_sensor=np.array(
|
||||
[
|
||||
[1.0, 0.0, 0.0, x],
|
||||
[0.0, 1.0, 0.0, 0.0],
|
||||
[0.0, 0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 0.0, 1.0],
|
||||
]
|
||||
),
|
||||
width=320,
|
||||
height=240,
|
||||
)
|
||||
|
||||
|
||||
def synthetic_observations(rig: SensorRig, now_s: float, count: int) -> list[Observation2D]:
|
||||
left = rig.cameras["ps3eye_left"]
|
||||
right = rig.cameras["ps3eye_right"]
|
||||
timestamp = time.monotonic_ns()
|
||||
observations: list[Observation2D] = []
|
||||
for index in range(count):
|
||||
u = index / max(1, count)
|
||||
theta = index * 2.399963229728653 + now_s * 0.65
|
||||
radius = 0.12 + 1.05 * np.sqrt(u)
|
||||
point = np.array(
|
||||
[
|
||||
radius * np.cos(theta),
|
||||
radius * np.sin(theta),
|
||||
1.25 + 0.45 * np.sin(index * 0.041 + now_s),
|
||||
],
|
||||
dtype=np.float64,
|
||||
)
|
||||
marker = f"synthetic-fused-{index}"
|
||||
observations.append(Observation2D("ps3eye_left", marker, timestamp, left.project_world(point), 0.95))
|
||||
observations.append(Observation2D("ps3eye_right", marker, timestamp, right.project_world(point), 0.92))
|
||||
return observations
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Write live sensor-fusion render frames into typed CultCache state.")
|
||||
parser.add_argument("--cache", default=str(ROOT / "calibration" / "runs" / "visual-state.msgpack"))
|
||||
parser.add_argument("--fps", type=float, default=30.0)
|
||||
parser.add_argument("--points", type=int, default=256)
|
||||
parser.add_argument("--duration", type=float)
|
||||
args = parser.parse_args()
|
||||
|
||||
left = camera("ps3eye_left", -0.25)
|
||||
right = camera("ps3eye_right", 0.25)
|
||||
rig = SensorRig(
|
||||
cameras={left.sensor_id: left, right.sensor_id: right},
|
||||
config=FusionConfig(max_pair_dt_ns=30_000_000, max_reprojection_error_px=0.01, cache_ttl_ns=500_000_000),
|
||||
)
|
||||
render_config = RenderBridgeConfig(default_point_radius_m=0.035)
|
||||
interval = 1.0 / max(1.0, args.fps)
|
||||
start = time.monotonic()
|
||||
frame_id = 0
|
||||
cache_path = Path(args.cache)
|
||||
while True:
|
||||
now = time.monotonic()
|
||||
if args.duration is not None and now - start >= args.duration:
|
||||
break
|
||||
observations = synthetic_observations(rig, now, args.points)
|
||||
result = rig.fuse(observations)
|
||||
frame = lower_points_to_render_frame(
|
||||
result.points,
|
||||
render_config,
|
||||
frame_id=frame_id,
|
||||
created_monotonic_ns=time.monotonic_ns(),
|
||||
)
|
||||
put_live_render_frame(cache_path, frame)
|
||||
frame_id += 1
|
||||
sleep_for = interval - (time.monotonic() - now)
|
||||
if sleep_for > 0:
|
||||
time.sleep(sleep_for)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -8,9 +8,11 @@ if str(ROOT) not in sys.path:
|
|||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
from localcast.sensor_fusion.spout_output import (
|
||||
CultCacheRenderFrameSource,
|
||||
OpenGLSpoutPointRenderer,
|
||||
RenderFrameFileSource,
|
||||
SpoutOutputConfig,
|
||||
write_cult_status,
|
||||
write_status,
|
||||
)
|
||||
|
||||
|
|
@ -18,6 +20,7 @@ from localcast.sensor_fusion.spout_output import (
|
|||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Publish render-frame packets as a named Spout texture for OBS.")
|
||||
parser.add_argument("--frame-json", default=str(ROOT / "calibration" / "runs" / "synthetic-render-frame.json"))
|
||||
parser.add_argument("--frame-cache", default=str(ROOT / "calibration" / "runs" / "visual-state.msgpack"))
|
||||
parser.add_argument("--sender-name", default="LocalCastBridge Point Cloud")
|
||||
parser.add_argument("--width", type=int, default=1920)
|
||||
parser.add_argument("--height", type=int, default=1080)
|
||||
|
|
@ -26,11 +29,15 @@ def main() -> None:
|
|||
parser.add_argument("--demo-if-missing", action="store_true")
|
||||
parser.add_argument("--demo-point-count", type=int, default=4096)
|
||||
parser.add_argument("--status", default=str(ROOT / "calibration" / "runs" / "stream-spout-status.json"))
|
||||
parser.add_argument("--status-cache", default=str(ROOT / "calibration" / "runs" / "visual-stream-status.msgpack"))
|
||||
parser.add_argument("--source", choices=["cultcache", "json"], default="cultcache")
|
||||
parser.add_argument("--duration", type=float, help="Optional run duration in seconds for smoke tests.")
|
||||
args = parser.parse_args()
|
||||
|
||||
frame_path = Path(args.frame_json)
|
||||
frame_cache_path = Path(args.frame_cache)
|
||||
status_path = Path(args.status)
|
||||
status_cache_path = Path(args.status_cache)
|
||||
config = SpoutOutputConfig(
|
||||
sender_name=args.sender_name,
|
||||
width=args.width,
|
||||
|
|
@ -39,7 +46,12 @@ def main() -> None:
|
|||
point_scale=args.point_scale,
|
||||
demo_point_count=args.demo_point_count,
|
||||
)
|
||||
source = RenderFrameFileSource(frame_path, demo_if_missing=args.demo_if_missing, demo_point_count=args.demo_point_count)
|
||||
if args.source == "cultcache":
|
||||
source = CultCacheRenderFrameSource(frame_cache_path, demo_if_missing=args.demo_if_missing, demo_point_count=args.demo_point_count)
|
||||
frame_source_name = str(frame_cache_path)
|
||||
else:
|
||||
source = RenderFrameFileSource(frame_path, demo_if_missing=args.demo_if_missing, demo_point_count=args.demo_point_count)
|
||||
frame_source_name = str(frame_path)
|
||||
frame_interval = 1.0 / max(1.0, args.fps)
|
||||
start = time.monotonic()
|
||||
last_status = 0.0
|
||||
|
|
@ -57,7 +69,15 @@ def main() -> None:
|
|||
sender_name=config.sender_name,
|
||||
frames_sent=renderer.frames_sent,
|
||||
point_count=len(frame.points),
|
||||
frame_path=frame_path,
|
||||
frame_path=Path(frame_source_name),
|
||||
last_error=None if ok else "SpoutGL sendTexture returned false",
|
||||
)
|
||||
write_cult_status(
|
||||
status_cache_path,
|
||||
sender_name=config.sender_name,
|
||||
frames_sent=renderer.frames_sent,
|
||||
point_count=len(frame.points),
|
||||
frame_source=frame_source_name,
|
||||
last_error=None if ok else "SpoutGL sendTexture returned false",
|
||||
)
|
||||
last_status = now
|
||||
|
|
@ -69,7 +89,15 @@ def main() -> None:
|
|||
sender_name=config.sender_name,
|
||||
frames_sent=renderer.frames_sent,
|
||||
point_count=0,
|
||||
frame_path=frame_path,
|
||||
frame_path=Path(frame_source_name),
|
||||
last_error="stopped",
|
||||
)
|
||||
write_cult_status(
|
||||
status_cache_path,
|
||||
sender_name=config.sender_name,
|
||||
frames_sent=renderer.frames_sent,
|
||||
point_count=0,
|
||||
frame_source=frame_source_name,
|
||||
last_error="stopped",
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue