Previous
Button
Add a camera to your machine’s configuration so you can capture images and video from the Viam app and from code.
The camera API gives you GetImages (capture frames), GetPointCloud (depth data), and stream access regardless of the underlying hardware. Common models include:
Browse all available camera models in the Viam registry.
Go to app.viam.com and navigate to your machine.
Confirm it shows as Live in the upper left.
If it shows as offline, verify that viam-server is running on your machine.
my-camera) and click Create.After creating the component, you’ll see its configuration panel.
For a USB webcam (webcam model):
Most USB webcams work with no additional configuration. If you have multiple cameras connected, specify which one to use:
{
"video_path": "video0"
}
To find available video devices on Linux:
ls /dev/video*
You can also set resolution and frame rate:
{
"width_px": 640,
"height_px": 480,
"frame_rate": 30
}
Click Save in the upper right of the configuration panel.
When you save, viam-server automatically reloads the configuration and initializes the new component.
You do not need to restart anything.
Every component in Viam has a built-in test panel in the Configure tab. The test panel uses the exact same APIs your code will use, so if the camera works here, it will work in your programs.
You should see a live feed from the camera.
Capture an image from your camera programmatically.
To get the credentials for the code below, go to your machine’s page in the Viam app, click the CONNECT tab, and select SDK code sample. Toggle Include API key on. Copy the machine address, API key, and API key ID from the code sample.
When you run the code below, it saves an image file to your current directory. Check that the image shows what the camera sees.
Install the SDK if you haven’t already:
pip install viam-sdk
Save this as camera_test.py:
import asyncio
from io import BytesIO
from PIL import Image as PILImage
from viam.robot.client import RobotClient
from viam.components.camera import Camera
async def main():
opts = RobotClient.Options.with_api_key(
api_key="YOUR-API-KEY",
api_key_id="YOUR-API-KEY-ID"
)
robot = await RobotClient.at_address("YOUR-MACHINE-ADDRESS", opts)
camera = Camera.from_robot(robot, "my-camera")
images, metadata = await camera.get_images()
image = PILImage.open(BytesIO(images[0].data))
image.save("test-capture.png")
print(f"Captured {image.size[0]}x{image.size[1]} image")
await robot.close()
if __name__ == "__main__":
asyncio.run(main())
Run it:
python camera_test.py
You should see output like:
Captured 640x480 image
And a file called test-capture.png in your current directory.
Initialize a Go module and install the SDK if you haven’t already:
mkdir camera-test && cd camera-test
go mod init camera-test
go get go.viam.com/rdk
Save this as main.go:
package main
import (
"context"
"fmt"
"image/png"
"os"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/robot/client"
"go.viam.com/rdk/utils"
)
func main() {
ctx := context.Background()
logger := logging.NewLogger("camera-test")
robot, err := client.New(ctx, "YOUR-MACHINE-ADDRESS", logger,
client.WithCredentials(utils.Credentials{
Type: utils.CredentialsTypeAPIKey,
Payload: "YOUR-API-KEY",
}),
client.WithAPIKeyID("YOUR-API-KEY-ID"),
)
if err != nil {
logger.Fatal(err)
}
defer robot.Close(ctx)
cam, err := camera.FromProvider(robot, "my-camera")
if err != nil {
logger.Fatal(err)
}
img, _, err := cam.Images(ctx, nil, nil)
if err != nil {
logger.Fatal(err)
}
f, err := os.Create("test-capture.png")
if err != nil {
logger.Fatal(err)
}
defer f.Close()
image, err := img[0].Image(ctx)
if err != nil {
logger.Fatal(err)
}
if err := png.Encode(f, image); err != nil {
logger.Fatal(err)
}
fmt.Printf("Captured image and saved to test-capture.png\n")
}
Run it:
go run main.go
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!