Detect color with a Viam Rover
In this tutorial you will learn how to configure a color detector with the vision service and leverage that detector with a transform camera to detect the color red.
The vision service enables a robot to use its cameras to see and interpret the world around it. The service also allows you to create different types of detectors with which the robot can recognize objects, scan QR codes, perform optical quality inspections, sort different colored objects, take measurements, and more.
You can follow this tutorial with a rented Viam Rover or with your own Viam Rover.
Tip
If you are renting your rover, we recommend that you skim through this page before renting your rover.
Be aware that if you are running out of time during your rental, you can extend your rover rental as long as there are no other reservations.
Enable the cameras
Before configuring color detection, enable the rover’s camera to get a better sense of what it perceives.
If you are running this tutorial with a rented Viam Rover, enable both provided cameras: the front-facing camera and the overhead cam. In the
viam_base
component panel under the Control tab, enable both thecam
for the front-facing camera and theoverhead-cam:overheadcam
for an overhead view of your rover.You can also view and control the camera streams from the individual camera component panels.
If you are running this tutorial on your own Viam Rover, enable the front facing camera. If you are using the
ViamRover
fragment with your rover, the front facing camera is namedcam
and can be enabled in theviam_base
component panel under the Control tab.
Add the vision service to detect a color
This tutorial uses the color #7a4f5c
or rgb(122, 79, 92)
(a reddish color).
Hex color #7a4f5c:
Navigate to your robot’s Config tab on the Viam app and configure your vision service color detector:
Click the Services subtab and click Create service in the lower-left corner.
Select the
Vision
type, then select theColor Detector
model.Enter
my_color_detector
as the name for your detector and click Create.In the resulting vision service panel, click the color picker box to set the color to be detected. For this tutorial, set the color to
rgb(122, 79, 92)
or use hex code#7a4f5c
.Then, set Hue Tolerance to
0.06
and Segment Size px to100
.
Your configuration should look like the following:
Add the vision service object to the services array in your rover’s raw JSON configuration:
"services": [
{
"name": "my_color_detector",
"type": "vision",
"model": "color_detector",
"attributes": {
"segment_size_px": 100,
"detect_color": "#7a4f5c",
"hue_tolerance_pct": 0.06
}
},
... // Other services
]
The color_detector
is a heuristic-based detector that draws boxes around objects according to their hue.
Click Save config and head to the Components tab.
You cannot interact directly with the vision service. To be able to interact with the vision service you must configure a camera component.
Tip
If you want to detect other colors, change the color parameter detect_color
.
Object colors can vary dramatically based on the light source.
We recommend you verify the desired color detection value under actual lighting conditions.
To determine the color value from the actual cam component image, you can use a pixel color tool, like Color Picker for Chrome.
If the color is not reliably detected, drag the Hue Tolerance slider to the right or navigate to the Raw JSON tab to increase the hue_tolerance_pct
.
Note that the detector does not detect black, perfect greys (greys where the red, green, and blue color component values are equal), or white.
Configure a transform camera to use the color detector
Viam camera components can be physical like the one already configured on the rover, or virtual. A virtual transform camera transforms the output from a physical camera.
To view output from the color detector overlaid on images from a physical camera, configure a transform camera:
- Navigate to the Config tab in the Viam app and click the Components subtab.
- Click Create component in the lower-left corner of the page.
- Select
camera
as the type. - Select
transform
as the model. - Enter a name, for example
detectionCam
, and click Create.

Viam generates an Attributes section for the detection camera’s component panel:
Copy the following JSON configuration into the Attributes section:
{
"source": "cam",
"pipeline": [
{
"attributes": {
"detector_name": "my_color_detector",
"confidence_threshold": 0.3
},
"type": "detections"
}
]
}
Explanations of each attribute are as follows:
source
: The name of the physical camera on the rover, which provides the visual feed to get detections from.pipeline
: Contains the transformation objects to apply to the camera.attributes
: The attributes of this transform camera.detector_name
: The name of the detector.confidence_threshold
: The percentage of confidence needed by the detection service to identify a color. Since we set it to0.3
, this means that detections with less than 30% confidence won’t be recognized.type
: The type of transform camera.
The filled-in transform camera configuration panel will look like this:
After adding the component and its attributes, click Save config.
Test your transform camera in the Control tab
In the Control tab, click on your base component and enable the detection camera in the Live Feeds section.
Next, enable the keyboard and move your rover around until your camera detects the specified color.
Each time the camera detects the color, you will see a red rectangle around the color labeled with the detection confidence level.
Scroll down in the Control tab and select the dedicated section for detectionCam to access its live stream.
Next Steps
If you’re ready for more, try making your rover detect other colors. You could also write some code with a Viam SDK to make your Viam Rover move in a square.
You can also ask questions in the Community Discord and we will be happy to help.
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!