Skip to content

LiveViewClient

class LiveViewClient

Importable as dream.LiveViewClient.

Shows one robot on the platform’s Live View page: its cameras, status and arms, and the buttons viewers press.

The key resolves as DreamClient’s does: the argument, else your credentials. Where the platform is comes from your credentials and cannot be changed from code. The constructor checks the encoder by opening one; nothing is dialed until the first stream(), and close(), or leaving the with block, ends the link. Callbacks run on the client’s thread, so keep them short; a callback that has seconds of work (moving arms) returns it as an awaitable, which the client waits for before it answers the page.

The client runs on a thread of its own, which reads your newest stream() _POLL_HZ times a second and does all of the work from there, so it never runs on, or waits for, your control loop. A camera is encoded only while a viewer watches it, and faces are blurred on the robot before any frame leaves it, unless face_blur=False. Live View shows the robot; recording a run is the dream client’s job, done from DreamClient.set_observation().

Given the loop’s DreamClient, the client also reports the model’s round trips while the robot is running, as the station does: the page waits for the first one before it shows the run as answered (“Dream-1 is getting ready” until then), and draws its latency from them.

See also: EventChange, stream(), register_callback()

A context manager: use it in a with block. Entering: Returns the client; leaving the block calls close(). Leaving: Calls close().

LiveViewClient(
    robot_id: str,
    *,
    api_key: str | None = None,
    dream_client: Any = None,
    face_blur: bool = True,
    model_fixed: bool = True,
    cameras: Sequence[str] | None = None,
)

Resolves the key and the link and opens one encoder to prove it can; nothing runs until the first stream().

Parameters

  • robot_id (str): The robot as the platform knows it, rbt_ and 16 hex digits: the robot_id under live: in the station file (~/.dreamlabs/station.yaml) that the setup wizard’s Save writes when it registers the robot. The Live View page lists robots by name, never by id.
  • api_key (str | None, default None, keyword-only): Your API key; the credentials’ (DREAM_API_KEY) when None.
  • dream_client (Any, default None, keyword-only): The loop’s dream.DreamClient, whose last_timing stream() reads to report each new round trip to the page, whose checkpoint the page shows as the model and whose session_id names the run. Without it the page never sees an answer and shows the run as getting ready. A loop that opens a new DreamClient (one per run, as the station does) sets the dream_client attribute to it.
  • face_blur (bool, default True, keyword-only): Blur every face in every encoded frame before it leaves the robot, with a detector per camera (shared.faceblur), and say so to the platform. On by default; off sends the frames as captured.
  • model_fixed (bool, default True, keyword-only): True (the default) when your code chooses the model: the page shows the one you run and offers no model or action choice. False lets a viewer pick them: a Start then carries the checkpoint and the action space, which get_model() returns, and your loop runs them.
  • cameras (Sequence[str] | None, default None, keyword-only): The cameras a viewer may watch, in this order, each named as it is in your observation’s keys (video.<name>) and shown under that name, not renamed; stream() may then carry any of them, a camera with no frame yet left out, and a frame of another name is not shown. None: the cameras of the first stream(), the model’s roles renamed as the page names them (overhead is top).

Raises

  • DreamConfigError: No key is set anywhere, robot_id is empty, the platform is http:// on a host other than this one (the key would cross the network in the clear), PyAV, OpenCV or websockets is not installed, the encoder does not open in this PyAV build, face_blur is on and the face detector is not installed, or cameras names a camera twice, an empty name or pose.
LiveViewClient.register_callback(
    callback_func: Callable[[EventChange], object],
    *,
    home: bool = False,
    reset: bool = False,
    prompt: bool = False,
) -> None

Adds a callback for Start, Stop and a new task from the page, and for the presses it opts in to.

The callback runs on the client’s thread with the EventChange. It may return nothing (the page hears the press applied), the robot’s state after it ("running"), a mapping with state and, when the press failed, error (its message), or an awaitable (or a concurrent.futures.Future, such as asyncio.run_coroutine_threadsafe() returns) that resolves to one of those. The client waits for an awaitable up to the page’s deadline (10 s), then answers the page: an exception, or an error, is a failure with its message; past the deadline the page hears timeout. A Start, a Stop, a Home or a Reset is never cancelled halfway, deadline or not: the work goes on and the next status says where it ended. Only a new task’s awaitable is cancelled at the deadline.

Parameters

  • callback_func (Callable[[EventChange], object]): Called with EventChange.start on Start, EventChange.end on Stop, and on a new task EventChange.prompt with prompt=True, else EventChange.end. Keep it short: record the event for your loop to act on, as the example on the Live View page does, or return the work as an awaitable.
  • home (bool, default False, keyword-only): Also call it with EventChange.home when a viewer presses Home. While no callback takes Home, the page hears “not supported”.
  • reset (bool, default False, keyword-only): Also call it with EventChange.reset when a viewer presses Reset arms; the same holds.
  • prompt (bool, default False, keyword-only): Call it with EventChange.prompt, rather than EventChange.end, when a viewer sends a task.
LiveViewClient.get_prompt() -> str | None

The task a viewer last sent from the page; after a Start without one, the prompt you last streamed.

Returns

str | None: The task’s text, or None until a viewer sends one.

LiveViewClient.get_model() -> tuple[str | None, str | None]

The model a viewer chose with the last Start, for a client built with model_fixed=False.

Returns

tuple[str | None, str | None]: (checkpoint, action_space): the checkpoint slug and "eef" or "joint", each None when the Start named none (keep the one you run); (None, None) before the first Start and with model_fixed=True.

LiveViewClient.get_steps() -> int | None

How many steps the last Start asked for, which the page offers while your status carries steps.

Returns

int | None: The step count, at least 1, or None when the Start named none (keep your count).

LiveViewClient.stats() -> dict[str, Any]

The client’s own counters, for your loop’s logs or status page; safe from any thread.

Returns

dict[str, Any]: linked, node (the live node holding the link), viewers (None: the link carries camera subscriptions, not viewer counts), streams (the watched cameras), pose (whether a viewer holds the arms’ stream), dropped_frames per camera, reconnects, errors (encoder faults and closes with a code), yields (times the tick guard paused the video) and live_paused.

LiveViewClient.stream(
    obs: Mapping[str, Any],
    status: str,
    *,
    arms: Mapping[str, Any] | None = None,
    error: str | tuple[str, str | None] | None = None,
    prompt: str | None = None,
    run: int | None = None,
    model: tuple[str | None, str | None] | None = None,
    steps: int | None = None,
) -> None

Hands the client the newest observation and the robot’s status; returns at once.

Call it whenever you have something new, from any thread and at any rate: the client reads the newest call on its own thread _POLL_HZ times a second, so your loop’s pace never reaches the video. The first call opens the link; without cameras in the constructor it also names the cameras, and every later call carries the same ones (with cameras, a camera with no frame yet may be left out). A frame is encoded once: hand the client a new array or JPEG for each new camera frame, and the same one again while the camera has delivered nothing new. Every keyword is optional; each one the page shows is shown only once you pass it.

Parameters

  • obs (Mapping[str, Any]): The observation, DreamClient.set_observation()’s frames and state in one mapping: each video.<role> (or image_window.<role>) key holds one frame and is a camera on the page: overhead the main picture (top), wrist_l and wrist_r the side panels (left, right), any other role under its own name. A frame is a base64 JPEG, or an RGB uint8 array [height, width, 3] (the client keeps a reference, so hand it a new array per frame and the same one while it has not changed). Every other key is ignored here.
  • status (str): What the page shows as the robot’s state: idle, running, error, or any short word. Report running from a viewer’s Start until the episode ends: the page shows the task and its Stop button only while the robot is running, and offers Start only while it is idle or error.
  • arms (Mapping[str, Any] | None, default None, keyword-only): Each arm’s joints as they are now, by name ({"left": (q, gripper), "right": (q, gripper)}): q the joint angles in radians, gripper the opening from 0 closed to 1 open, or None for an arm that cannot be read. The page draws the robot from them while a viewer looks (the pose stream, sent only when the first call carries arms), and shows an arm whose value is None as not answering.
  • error (str | tuple[str, str | None] | None, default None, keyword-only): Why the arms are not moving, for the page’s banner: a message, or (message, code) with a short code for it (pool_warming, ArmError); None while nothing is wrong.
  • prompt (str | None, default None, keyword-only): The task your robot runs or will run, when your code sets it: the status shows it rather than the one a viewer last sent, and a Start without a task runs it (get_prompt() then returns it).
  • run (int | None, default None, keyword-only): Your run number, which the status carries; the client counts runs itself, one per stretch of running, when left out.
  • model (tuple[str | None, str | None] | None, default None, keyword-only): (checkpoint, action_space) your loop runs, which the page shows (action_space "eef" or "joint", or None); the dream_client’s checkpoint when left out.
  • steps (int | None, default None, keyword-only): How many steps your next run takes, 0 for no limit; the page then offers a step count, which a Start carries (get_steps()).

Raises

  • DreamObservationError: obs is not a mapping, a frame is neither a base64 JPEG nor an RGB array, without cameras, obs carries no frame or not the cameras of the first call; status is empty; or a keyword is not of the shape above.
LiveViewClient.close() -> None

Ends the link, releases the encoders and joins the client’s thread; idempotent.