Previous
Board
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:
The camera component supports the following methods:
Method Name | Description |
---|---|
GetImage | Return an image from the camera. |
GetImages | Get simultaneous images from different imagers, along with associated metadata. |
GetPointCloud | Get a point cloud from the camera as bytes with a MIME type describing the structure of the data. |
GetProperties | Get the camera intrinsic parameters and camera distortion, as well as whether the camera supports returning point clouds. |
DoCommand | Execute model-specific commands that are not otherwise defined by the component API. |
GetGeometries | Get all the geometries associated with the camera in its current configuration, in the frame of the camera. |
GetResourceName | Get the ResourceName for this camera with the given name. |
Close | Safely shut down the resource and prevent further use. |
To get started using Viam’s SDKs to connect to and control your camera and the rest of your machine, go to your machine’s page on the Viam app, Navigate to the CONNECT tab’s Code sample page, select your preferred programming language, and copy the sample code.
To show your machine’s API key in the sample code, toggle Include API key.
We strongly recommend that you add your API key and machine address as an environment variable. Anyone with these secrets can access your machine, and the computer running your machine.
When executed, this sample code creates a connection to your machine as a client.
The following examples assume you have a camera called "my_camera"
configured as a component of your machine.
If your camera has a different name, change the name
in the code.
Import the camera package for the SDK you are using:
Return an image from the camera. You can request a specific MIME type but the returned MIME type is not guaranteed. If the server does not know how to return the specified MIME type, the server returns the image in another format instead.
Parameters:
mime_type
(str) (required): The desired mime type of the image. This does not guarantee output type.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:
If the mime_type
of your image is image/vnd.viam.dep
, pass the returned image data to the Viam Python SDK’s ViamImage.bytes_to_depth_array()
method to decode the raw image data to a standard 2D image representation.
In addition, the Python SDK provides the helper functions viam_to_pil_image
and pil_to_viam_image
to decode the ViamImage
into a PIL Image
and vice versa.
For example:
Be sure to close the image when finished.
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.mimeType
(string): The desired MIME type of the image. This does not guarantee output type.extra
(map[string]interface{}): Extra options to pass to the underlying RPC call.Returns:
Example:
You can also try to directly decode as an Image.Image
with the camera’s DecodeImageFromCamera
function:
To use either method, be sure to import "go.viam.com/rdk/utils"
at the beginning of your file.
For more information, see the Go SDK Docs.
Intended specifically for use with cameras that support simultaneous depth and color image streams, like the Intel RealSense or the Luxonis OAK-D. If your camera does not have multiple imagers, this method will work without capturing multiple images simultaneously.
You can use the rgb-d-overlay
module to view and compare the camera streams returned by this method.
See the module readme for further instructions.
Get simultaneous images from different imagers, along with associated metadata.
The multiple images returned from GetImages()
do not represent a time series of images.
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:
Example:
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:
Example:
For more information, see the Go SDK Docs.
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:
Example:
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:
Example:
For more information, see the Go SDK Docs.
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:
Example:
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:
For more information, see the Go SDK Docs.
Execute model-specific commands that are not otherwise defined by the component API.
For native models, model-specific commands are covered with each model’s documentation.
If you are implementing your own camera and adding features that have no native API method, you can access 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:
Raises:
Example:
For more information, see the Python SDK Docs.
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:
For more information, see the Python SDK Docs.
Get the ResourceName
for this camera with the given name.
Parameters:
name
(str) (required): The name of the Resource.Returns:
Example:
For more information, see the Python SDK Docs.
Safely shut down the resource and prevent further use.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.Returns:
Example:
For more information, see the Go SDK Docs.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!