How to capture camera video with OpenCV

Camera capture in OpenCV starts by opening a VideoCapture stream and confirming that frames arrive from the expected device. A small smoke test is enough to check a webcam, USB camera, or virtual camera before the feed becomes part of a larger vision pipeline.

The VideoCapture source can be a camera index, and index 0 is commonly the default camera on a workstation. The smoke-test script opens the selected index, reads a few warm-up frames, writes one still image, and prints the backend plus frame size so the camera path is visible without needing a display window.

A Linux or macOS-style shell can run the same Python 3 file once OpenCV is installed. Camera permissions, privacy prompts, and backend names differ by operating system, so a failed open can mean the camera is busy, blocked by privacy settings, or exposed under a different index.

Steps to capture camera video with OpenCV:

  1. Create the OpenCV camera capture smoke-test script.
    capture_camera_frame.py
    #!/usr/bin/env python3
    import argparse
    import sys
     
    import cv2 as cv
     
     
    parser = argparse.ArgumentParser(description="Capture one frame from an OpenCV camera.")
    parser.add_argument("--camera", type=int, default=0, help="camera index, usually 0 for the default camera")
    parser.add_argument("--output", default="capture.jpg", help="image file to write")
    parser.add_argument("--warmup-frames", type=int, default=5, help="frames to read before saving the output frame")
    args = parser.parse_args()
     
    cap = cv.VideoCapture(args.camera)
    if not cap.isOpened():
        sys.exit(f"Cannot open camera {args.camera}")
     
    try:
        backend = cap.getBackendName()
        frame = None
     
        for _ in range(max(args.warmup_frames, 0) + 1):
            ok, frame = cap.read()
            if not ok or frame is None:
                sys.exit("Cannot read a frame from the camera")
     
        height, width = frame.shape[:2]
     
        if not cv.imwrite(args.output, frame):
            sys.exit(f"Cannot write {args.output}")
     
        print(f"Opened camera {args.camera} with backend {backend}")
        print(f"Frame size: {width}x{height}")
        print(f"Saved frame: {args.output}")
    finally:
        cap.release()

    The warm-up reads let the driver settle exposure and focus before the saved frame is written.

  2. Run the script against the default camera.
    $ python3 capture_camera_frame.py --camera 0 --output capture.jpg
    Opened camera 0 with backend V4L2
    Frame size: 1280x720
    Saved frame: capture.jpg

    If the script prints Cannot open camera 0, close other camera applications and check the operating system's camera privacy setting before trying another index.

  3. Verify that OpenCV can read the captured frame file.
    $ python3 -c "import cv2 as cv; frame = cv.imread('capture.jpg'); print(frame.shape if frame is not None else 'not readable')"
    (720, 1280, 3)

    The shape is printed as height, width, and color channels. A not readable result means the frame file was not written as a valid image.

  4. Run the same script with another camera index when the default camera is not the target device.
    $ python3 capture_camera_frame.py --camera 1 --output capture-camera-1.jpg
    Opened camera 1 with backend V4L2
    Frame size: 1920x1080
    Saved frame: capture-camera-1.jpg

    Use the index whose saved frame shows the intended device. Backend names such as V4L2, MSMF, or AVFOUNDATION depend on the platform.

  5. Remove the smoke-test files when the camera check is complete.
    $ rm -f capture_camera_frame.py capture.jpg capture-camera-1.jpg

    Keep the script instead when it will become the starting point for a larger capture or analysis tool.