webcam
webcam is the general camera model.
If the camera drivers are among those in this mediadevices repository, the camera will work with the webcam model.
The webcam model works on Linux, macOS, and Windows.
On Windows, the built-in webcam model replaces the modular-webcam module, so you no longer need to add that module.
Windows support requires viam-server v1.15.0 or later.
If a webcam is unplugged and plugged back in, it reappears without a restart.
A webcam whose driver fails during discovery is skipped, and the other webcams still work.
Configuration
{
"name": "<your-camera-name>",
"model": "webcam",
"api": "rdk:component:camera",
"attributes": {
"intrinsic_parameters": {
"width_px": <int>,
"height_px": <int>,
"fx": <float>,
"fy": <float>,
"ppx": <float>,
"ppy": <float>
},
"distortion_parameters": {
"rk1": <float>,
"rk2": <float>,
"rk3": <float>,
"tp1": <float>,
"tp2": <float>
},
"format": <string>,
"video_path": "<your-video-path>",
"width_px": <int>,
"height_px": <int>,
"frame_rate": <float>,
"idle_timeout_ms": <int>
}
}
{
"name": "my_cam",
"model": "webcam",
"api": "rdk:component:camera",
"attributes": {
"video_path": "video0"
}
}
Attributes
| Name | Type | Required? | Description |
|---|---|---|---|
video_path | string | Optional | The ID of or the path to the webcam. If you don’t provide a video_path, it defaults to the first valid video path it finds. Using the ID of a webcam is more consistent than the path. See Using video_path. |
format | string | Optional | The camera image format, used with video_path to find the camera. See Using format. |
width_px | int | Optional | The camera image width in pixels, used with video_path to find a camera with this resolution. Negative values are rejected with a configuration error.Default: Closest possible value to 640 |
height_px | int | Optional | The camera image height in pixels, used with video_path to find a camera with this resolution. Negative values are rejected with a configuration error.Default: Closest possible value to 480 |
frame_rate | float | Optional | The camera capture frequency as frames per second, used with video_path to find a camera with this throughput.Default: Closest possible value to 30.0 |
idle_timeout_ms | int | Optional | How long in milliseconds to wait without an Images request before the camera pauses frame reads. The camera driver stays open so exposure settings are preserved, and the next Images call resumes reads automatically (with a wake timeout of 15 seconds). Pausing reads reduces CPU use. Set to 0 to disable idle pausing. Must be non-negative.Default: 0 (disabled) |
intrinsic_parameters | object | Optional | The intrinsic parameters of the camera used to do 2D <-> 3D projections:
|
distortion_parameters | object | Optional | Modified Brown-Conrady parameters used to correct for distortions caused by the shape of the camera lens:
|
Using video_path
Find a video path using a discovery service
The viam:find-webcams:webcam-discovery service helps you identify path options.
To add and use the service:
- Navigate to the CONFIGURE tab of your machine’s page.
- Click the + icon next to your machine part in the left-hand menu and select Blocks.
- Search for
find-webcamsand select thediscovery / find-webcams:webcam-discoveryservice. - Click Add module.
- Enter a name or use the suggested name for your camera and click Add to machine.
- Save your configuration, and wait a moment for the service to start.
- Click Test to see the available
video_paths. - Click the Copy attributes button for the camera you want to use.
- Paste the copied attributes into the camera card’s attributes editor.
- Click Save.
- You can now delete the discovery service and the module that provides it from your machine.
The discovery service gives identical cameras different names, so you can tell two of the same model apart.
On Windows, it sets up the built-in webcam model and offers MJPEG configurations, which you can copy the same way.
These behaviors require find-webcams v0.3.2 or later and viam-server v1.15.0 or later.
Find a video path using the command line
To list available video_paths use the following command:
ls /dev/v4l/by-id/
To find the paths of all connected video devices, run the following command:
v4l2-ctl --list-devices
The id listed by ls /dev/v4l/by-id/ is a more consistent way to refer to the webcam.
system_profiler SPCameraDataType
The Unique ID displayed for each camera is the video_path.
The Unique ID changes when you move the camera to a different USB port.
If that happens while viam-server is running, viam-server reconnects to the camera by its device name and logs camera reconnected under a new path.
viam-server doesn’t reconnect by name if another connected camera has the same device name, because it can’t tell the two apart; it logs another device shares this camera's hardware name; reconnecting by name is disabled instead.
In either case, update video_path to the camera’s new Unique ID so that viam-server finds the camera the next time it starts.
If you are using macOS version 15.x.x Sequoia or later, you need to give viam-server permissions to access webcams.
When you run viam-server for the first time, a pop-up message will ask for camera access.
Click Allow.
To confirm settings, you can go to System Settings > Privacy & Security > Camera and check that the toggle next to viam-server is set to enable access.
Using format
Viam supports the following pixel formats:
- I420
- MJPEG / MJPG
- NV12
- NV21
- UYVY / Y422
- YUY2 / YUYV / V422
- Z16
On Linux, you can determine which pixel formats your camera supports by running the following command on the machine your camera is connected to.
Replace /dev/video0 with the video path you determined for your video device above, if different:
v4l2-ctl --list-formats-ext --device /dev/video0
Windows
On Windows, webcams stream MJPEG.
With MJPEG, the built-in webcam model works at 1920x1080 and you can run two webcams at the same time.
Reads from an unplugged webcam time out instead of hanging.
Use the find-webcams discovery service to get a working MJPEG configuration.
Troubleshooting
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!