Camera API

The camera API allows you to give commands to your camera components for getting images or point clouds.

The API for camera components allows you to:

  • Request single images in 2D color, or display z-depth.
  • Request a point cloud. Each 3D point cloud image consists of a set of coordinates (x,y,z) representing depth in mm.

The camera component supports the following methods:

Method NameDescription
GetImagesGet simultaneous images from different imagers, along with associated metadata.
GetPointCloudGet a point cloud from the camera as bytes with a MIME type describing the structure of the data.
GetPropertiesGet the camera intrinsic parameters and camera distortion, as well as whether the camera supports returning point clouds.
DoCommandExecute model-specific commands that are not otherwise defined by the component API.
GetGeometriesGet all the geometries associated with the camera in its current configuration, in the frame of the camera.
GetResourceNameGet the ResourceName for this camera.
CloseSafely shut down the resource and prevent further use.

API

GetImages

GetImages is used for getting simultaneous images from different imagers from 3D cameras along with associated metadata, and single images from non-3D cameras, for example webcams, RTSP cameras, etc. in the image list in the response. Multiple images returned from GetImages() do not represent a time series of images.

Parameters:

  • filter_source_names (Sequence[str]) (optional): The filter_source_names parameter can be used to filter only the images from the specified source names. When unspecified, all images are returned.
  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Tuple[Sequence[video.NamedImage], common.ResponseMetadata]): A tuple containing two values; the first [0] a list of images returned from the camera system, and the second [1] the metadata associated with this response.

Example:

my_camera = Camera.from_robot(robot=machine, name="my_camera")

images, metadata = await my_camera.get_images()
first_image = images[0]
timestamp = metadata.captured_at

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • filterSourceNames ([]string)
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • ([]NamedImage): The list of images returned from the camera system, with the name of the imager associated with the image.
  • (resource.ResponseMetadata): The metadata, which holds the timestamp of when the data was captured.
  • (error): An error, if one occurred.

Example:

myCamera, err := camera.FromProvider(machine, "my_camera")

images, metadata, err := myCamera.Images(context.Background(), nil, nil)

For more information, see the Go SDK Docs.

Parameters:

  • filterSourceNames (string) (optional): A list of source names to filter the images by. If empty or undefined, all images will be returned.
  • extra (None) (optional): Extra parameters to pass to the camera.
  • callOptions (CallOptions) (optional)

Returns:

  • (Promise< { images: { image: Uint8Array; mimeType: string; sourceName: string }[]; metadata: ResponseMetadata; },>)

Example:

const camera = new VIAM.CameraClient(machine, 'my_camera');
const images = await camera.getImages();

For more information, see the TypeScript SDK Docs.

Parameters:

Returns:

Example:

const camera = new VIAM.CameraClient(machine, 'my_camera');
const images = await camera.getImages();

For more information, see the Flutter SDK Docs.

GetPointCloud

Get a point cloud from the camera as bytes with a MIME type describing the structure of the data. The consumer of this call should decode the bytes into the format suggested by the MIME type.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Tuple[bytes, str]): A tuple containing two values; the first [0] the pointcloud data, and the second [1] the mimetype of the pointcloud (for example, PCD).

Example:

import numpy as np
import open3d as o3d

my_camera = Camera.from_robot(robot=machine, name="my_camera")

data, _ = await my_camera.get_point_cloud()

# write the point cloud into a temporary file
with open("/tmp/pointcloud_data.pcd", "wb") as f:
    f.write(data)
pcd = o3d.io.read_point_cloud("/tmp/pointcloud_data.pcd")
points = np.asarray(pcd.points)

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (pointcloud.PointCloud): A general purpose container of points. It does not dictate whether or not the cloud is sparse or dense.
  • (error): An error, if one occurred.

Example:

myCamera, err := camera.FromProvider(machine, "my_camera")

// gets the next point cloud from a camera
pointCloud, err := myCamera.NextPointCloud(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

  • extra (None) (optional)
  • callOptions (CallOptions) (optional)

Returns:

  • (Promise)

Example:

const camera = new VIAM.CameraClient(machine, 'my_camera');
const pointCloud = await camera.getPointCloud();

For more information, see the TypeScript SDK Docs.

Parameters:

Returns:

Example:

var nextPointCloud = await myCamera.pointCloud();

For more information, see the Flutter SDK Docs.

GetProperties

Get the camera intrinsic parameters and camera distortion, as well as whether the camera supports returning point clouds.

Parameters:

  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (viam.components.camera.Camera.Properties): The properties of the camera.

Example:

my_camera = Camera.from_robot(robot=machine, name="my_camera")

properties = await my_camera.get_properties()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.

Returns:

  • (Properties): Properties of the particular implementation of a camera.
  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

Parameters:

  • callOptions (CallOptions) (optional)

Returns:

Example:

const camera = new VIAM.CameraClient(machine, 'my_camera');
const properties = await camera.getProperties();

For more information, see the TypeScript SDK Docs.

Parameters:

  • None.

Returns:

Example:

var cameraProperties = await myCamera.properties();

For more information, see the Flutter SDK Docs.

DoCommand

Execute model-specific commands that are not otherwise defined by the component API. Most models do not implement DoCommand. Any available model-specific commands should be covered in the model’s documentation. If you are implementing your own camera and want to add features that have no corresponding built-in API method, you can implement them with DoCommand.

Parameters:

  • command (Mapping[str, ValueTypes]) (required): The command to execute.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Mapping[str, viam.utils.ValueTypes]): Result of the executed command.

Raises:

  • (NotImplementedError): Raised if the Resource does not support arbitrary commands.

Example:

my_camera = Camera.from_robot(robot=machine, name="my_camera")
command = {"cmd": "test", "data1": 500}
result = await my_camera.do_command(command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

myCamera, err := camera.FromProvider(machine, "my_camera")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myCamera.DoCommand(context.Background(), command)

For more information, see the Go SDK Docs.

Parameters:

  • command (Struct) (required): The command to execute.
  • callOptions (CallOptions) (optional)

Returns:

Example:

import { Struct } from '@viamrobotics/sdk';

const result = await resource.doCommand(
  Struct.fromJson({
    myCommand: { key: 'value' },
  })
);

For more information, see the TypeScript SDK Docs.

Parameters:

Returns:

Example:

// Example using doCommand with an arm component
const command = {'cmd': 'test', 'data1': 500};
var result = myArm.doCommand(command);

For more information, see the Flutter SDK Docs.

GetGeometries

Get all the geometries associated with the camera in its current configuration, in the frame of the camera. The motion and navigation services use the relative position of inherent geometries to configured geometries representing obstacles for collision detection and obstacle avoidance while motion planning.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

my_camera = Camera.from_robot(robot=machine, name="my_camera")
geometries = await my_camera.get_geometries()

if geometries:
    # Get the center of the first geometry
    print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

Example:

// This example shows using Geometries with an camera component.
myCamera, err := camera.FromProvider(machine, "my_camera")

geometries, err := myCamera.Geometries(context.Background(), nil)

if len(geometries) > 0 {
   // Get the center of the first geometry
   elem := geometries[0]
   fmt.Println("Pose of the first geometry's center point:", elem.Pose())
}

For more information, see the Go SDK Docs.

Parameters:

  • extra (None) (optional)
  • callOptions (CallOptions) (optional)

Returns:

For more information, see the TypeScript SDK Docs.

GetResourceName

Get the ResourceName for this camera.

Parameters:

  • name (str) (required): The name of the Resource.

Returns:

Example:

my_camera_name = Camera.get_resource_name("my_camera")

For more information, see the Python SDK Docs.

Parameters:

  • None.

Returns:

Example:

myCamera, err := camera.FromProvider(machine, "my_camera")

err = myCamera.Name()

For more information, see the Go SDK Docs.

Parameters:

  • None.

Returns:

  • (string): The name of the resource.

Example:

camera.name

For more information, see the TypeScript SDK Docs.

Parameters:

Returns:

Example:

final myCameraResourceName = myCamera.getResourceName("my_camera");

For more information, see the Flutter SDK Docs.

Close

Safely shut down the resource and prevent further use.

Parameters:

  • None.

Returns:

  • None.

Example:

my_camera = Camera.from_robot(robot=machine, name="my_camera")
await my_camera.close()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.

Returns:

  • (error): An error, if one occurred.

Example:

myCamera, err := camera.FromProvider(machine, "my_camera")

err = myCamera.Close(context.Background())

For more information, see the Go SDK Docs.