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

NameTypeRequired?Description
video_pathstringOptionalThe 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.
formatstringOptionalThe camera image format, used with video_path to find the camera. See Using format.
width_pxintOptionalThe 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_pxintOptionalThe 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_ratefloatOptionalThe 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_msintOptionalHow 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_parametersobjectOptionalThe intrinsic parameters of the camera used to do 2D <-> 3D projections:
  • width_px: The expected width of the aligned image in pixels.
  • height_px: The expected height of the aligned image in pixels.
  • fx: The image center x point.
  • fy: The image center y point.
  • ppx: The image focal x.
  • ppy: The image focal y.
distortion_parametersobjectOptionalModified Brown-Conrady parameters used to correct for distortions caused by the shape of the camera lens:
  • rk1: The radial distortion x.
  • rk2: The radial distortion y.
  • rk3: The radial distortion z.
  • tp1: The tangential distortion x.
  • tp2: The tangential distortion y.

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:

  1. Navigate to the CONFIGURE tab of your machine’s page.
  2. Click the + icon next to your machine part in the left-hand menu and select Blocks.
  3. Search for find-webcams and select the discovery / find-webcams:webcam-discovery service.
  4. Click Add module.
  5. Enter a name or use the suggested name for your camera and click Add to machine.
  6. Save your configuration, and wait a moment for the service to start.
  7. Click Test to see the available video_paths.
  8. Click the Copy attributes button for the camera you want to use.
  9. Paste the copied attributes into the camera card’s attributes editor.
  10. Click Save.
  11. 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

Changing the config fails with a device busy error (Linux)
Before viam-server v1.14.0, changing a webcam’s configuration on Linux could fail with a device busy error. Update viam-server to v1.14.0 or later. If you see Device or resource busy when running v4l2-ctl yourself, stop viam-server first.
Cannot open webcam or found no webcams

When working with a webcam camera on the Linux platform, your Linux OS must be able to access the camera properly, and the camera must be configured to use a pixel format that Viam supports.

On your Linux system, verify each of the following:

  • Ensure that your Linux OS is able to access your camera:

    1. Run the following command to list compatible camera devices on your system:

      v4l2-ctl --list-devices
      

      In the list of camera devices returned, find the entry for your camera. For example, the webcam on a rover may appear as follows:

      GENERAL WEBCAM: GENERAL WEBCAM (usb-0000:01:00.0-1.4):
              /dev/video0
              /dev/video1
              /dev/media4
      

      The video path for your camera device is the first path listed under that camera, in this case /dev/video0.

    2. Then, stop viam-server, and verify that your Linux OS is able to access that video device properly:

      v4l2-ctl --stream-count 1 --device /dev/video0
      

      Replace /dev/video0 in the above command with the video path you determined for your video device above, if different.

      The command returns successfully (with no output) if Linux is able to successfully communicate with the camera, or errors with Cannot open device if there was a problem communicating. If this command errors, you should consult the documentation for your camera and Linux distribution to troubleshoot. If you receive the error Device or resource busy instead, be sure you have stopped viam-server first, then re-run the command above.

  • Ensure that your camera uses a supported pixel format:

    1. First, determine your video path, like /dev/video0, following the instructions above.

    2. Then, run the following command:

      v4l2-ctl --list-formats-ext --device /dev/video0
      

      Replace /dev/video0 in the above command with the video path you determined for your video device above, if different.

      The command will return a list of pixel formats your camera supports, such as MJPG (also notated as MJPEG) or YUYV (also notated as YUY2). In order to use a camera device with Viam, it must support at least one of the pixel formats listed in Using format. If your camera does not support any of these formats, it cannot be used with Viam.

If you are still having issues with your camera component on the Linux platform, and would like to file an issue, include your machine’s camera debug file contained in the /root/.viam/debug/components/camera directory. If you are running viam-server as a different user, find the .viam/debug/components/camera directory in that user’s home directory instead. This file contains basic diagnostic and configuration information about your camera that helps to quickly troubleshoot issues.

macOS camera permissions issues

On macOS, if you’re having trouble accessing your webcam, you may need to grant camera permissions for the application that you use to run viam-server.

Resetting camera permissions:

If viam-server is not prompting for camera permissions or you need to re-grant permissions, you can reset the camera permissions for all applications using the following command:

tccutil reset Camera

If you know the macOS bundle ID of the application that you use to run viam-server, you can pass the bundle ID to limit the camera permissions reset to only that application. For instance, you can reset camera permissions for the built-in Terminal app with tccutil reset Camera com.apple.Terminal. If the command was successful, you should see output similar to the following:

Successfully reset Camera approval status for com.apple.Terminal

After running this command, restart viam-server and it should prompt for camera permissions again.

Checking camera permissions:

To verify camera permissions, go to System Settings > Privacy & Security > Camera. Find the application viam-server is running in (such as Terminal or VS Code). Ensure that your terminal application has camera access enabled.