Detect color with a Viam Rover
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.
In this tutorial you will learn how to configure a color detector with the Vision Service and how to leverage that detector with a transform camera to detect the color red.
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.
Add the Vision Service to detect a color
This tutorial uses the hex color #7a4f5c (a reddish color).
Hex color #7a4f5c:
Navigate to the robot page on the Viam app. Click on the robot you wish to add the Vision Service to. Select the Config tab, and click on Services.
Scroll to the Create Service section. To create a Vision Service:
- Select
vision
as the Type. - Enter
my_color_detector
as the Name. - Select
color_detector
as the Model. - Click Create Service.
In your Vision Service’s panel, fill in the Attributes field.
{
"segment_size_px": 100,
"detect_color": "#7a4f5c",
"hue_tolerance_pct": 0.06
}
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 configuration adds a model
of type
color_detector
with the color as a parameter.
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, 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 camera transforms the output from a physical camera.
To view output from the color detector overlaid on images from a physical camera, you need to configure a transform camera.
Navigate to the Components tab in the Viam app and scroll to the Create Component section. To create a transform camera:
- Enter a name for Name, for example
detectionCam
. - Select
camera
as the Type. - Select
transform
as the Model. - Click Create Component.
Viam generates an empty Attributes section for the detection camera’s component panel. The panel’s Attribute Guide section displays the available attributes for the selected component.
Tip
Attribute Guides always prefix required attributes with an asterisk.
Copy the following JSON configuration into the Attributes section:
{
"source": "cam",
"pipeline": [
{
"attributes": {
"detector_name": "my_color_detector",
"confidence_threshold": 0.3
},
"type": "detections"
}
]
}
Field | Default value | Description |
---|---|---|
source | cam | The name of the physical camera on the rover, which provides the visual feed to get detections from. |
attributes | The attributes of this detectionCam. | |
attributes.detector_name | my_color_detector | The name of this detectionCam. |
attributes.confidence_threshold | 0.3 (30%) | The percentage of confidence in the color being present the detection service needs to detect a color. |
type | detections | The type of the component. |
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 add the detection camera from the Select Cameras drop down.
Next, enable the keyboard and move your rover around until your camera detects the configured color. Each time the camera detects the color, you will see a red rectangle around the color labeled with the detection confidence level.
If you scroll down in the Control tab, you can also click on the detectionCam’s own section to view its stream there.
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.
Have questions, or want to meet other people working on robots? Join our Community Discord.
Was this page helpful?
Glad to hear it! If there is anything we could be doing better, please create an issue.
We're sorry about that. If you'd like to talk to us for help, please join the Community Discord. To ensure we know what's wrong with this page, you can also open an issue.