Skip to main content

Getting Camera List and Resolution in Python on Windows

OpenCV does not have an API for enumerating capture devices. The sample shows how to create a Python extension to invoke DirectShow C++ APIs for enumerating capture devices and corresponding resolutions.

Environment

How to Build the CPython Extension

  • Create a source distribution:

    python setup.py sdist
    
  • distutils:

    python .\setup_distutils.py build
    
  • scikit-build:

    pip wheel . --verbose
    

Test

import device
import cv2

def select_camera(last_index):
    number = 0
    hint = "Select a camera (0 to " + str(last_index) + "): "
    try:
        number = int(input(hint))
        # select = int(select)
    except Exception:
        print("It's not a number!")
        return select_camera(last_index)

    if number > last_index:
        print("Invalid number! Retry!")
        return select_camera(last_index)

    return number


def open_camera(index):
    cap = cv2.VideoCapture(index)
    return cap

def main():
    # print OpenCV version
    print("OpenCV version: " + cv2.__version__)

    # Get camera list
    device_list = device.getDeviceList()
    index = 0

    for camera in device_list:
        print(str(index) + ': ' + camera[0] + ' ' + str(camera[1]))
        index += 1

    last_index = index - 1

    if last_index < 0:
        print("No device is connected")
        return

    # Select a camera
    camera_number = select_camera(last_index)
    
    # Open camera
    cap = open_camera(camera_number)

    if cap.isOpened():
        width = cap.get(3) # Frame Width
        height = cap.get(4) # Frame Height
        print('Default width: ' + str(width) + ', height: ' + str(height))

        while True:
            
            ret, frame = cap.read()
            cv2.imshow("frame", frame)

            # key: 'ESC'
            key = cv2.waitKey(20)
            if key == 27:
                break

        cap.release() 
        cv2.destroyAllWindows() 

if __name__ == "__main__":
    main()
python test.py

camera list in Python

Blog

Listing Multiple Cameras for OpenCV-Python on Windows

Metadata

Release files for windows-capture-device-list 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for windows-capture-device-list 1.1.0
File Size Uploaded Provenance Actions
windows-capture-device-list-1.1.0.tar.gz 3.5 kB Without provenance Details for windows-capture-device-list-1.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for windows-capture-device-list 1.1.0
File Actions
windows_capture_device_list-1.1.0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details for windows_capture_device_list-1.1.0-cp310-cp310-win_amd64.whl
windows_capture_device_list-1.1.0-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details for windows_capture_device_list-1.1.0-cp39-cp39-win_amd64.whl
windows_capture_device_list-1.1.0-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details for windows_capture_device_list-1.1.0-cp38-cp38-win_amd64.whl
windows_capture_device_list-1.1.0-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details for windows_capture_device_list-1.1.0-cp37-cp37m-win_amd64.whl
windows_capture_device_list-1.1.0-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details for windows_capture_device_list-1.1.0-cp36-cp36m-win_amd64.whl

Total release size: 74.3 kB

Release files / windows-capture-device-list-1.1.0.tar.gz

Download URL windows-capture-device-list-1.1.0.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cc49dd04b830e813347524e2043a860f90ea37508cac60db658753e8fcc02e43
BLAKE2b-256 checksum
How to use checksums
2bf31baf7456fe49f95a1957de070d4ae5d87338765e062b4cf5867a1e5c92c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / windows_capture_device_list-1.1.0-cp310-cp310-win_amd64.whl

Download URL windows_capture_device_list-1.1.0-cp310-cp310-win_amd64.whl
Size 14.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
c94f19d5f000947aa2eddadafc27a6b260fc3054023396772d51255f108d007f
BLAKE2b-256 checksum
How to use checksums
87372bfd89b026331e4a77706fc29a2e84bcd9a22b322b9fa3f53946338e32ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / windows_capture_device_list-1.1.0-cp39-cp39-win_amd64.whl

Download URL windows_capture_device_list-1.1.0-cp39-cp39-win_amd64.whl
Size 14.2 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
5b38d59d0d997cd45154e5e9f0c6205c2eb3a68deccbdf9412f761bbc2d63207
BLAKE2b-256 checksum
How to use checksums
a5b042ce1b7c61a934999e898a3bb96a7c975a15d8ab4cbddf477481cdad5c60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / windows_capture_device_list-1.1.0-cp38-cp38-win_amd64.whl

Download URL windows_capture_device_list-1.1.0-cp38-cp38-win_amd64.whl
Size 14.2 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
a6358c6e8cddde4fc4bc3910595397a6662d2335297edccebc0e87f6d3b330b0
BLAKE2b-256 checksum
How to use checksums
efb798d585bdb86f9118a10ca7e6e6be65f9f903e9d1a78fbd32058ef69256d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / windows_capture_device_list-1.1.0-cp37-cp37m-win_amd64.whl

Download URL windows_capture_device_list-1.1.0-cp37-cp37m-win_amd64.whl
Size 14.2 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
94d4c3781400c52d81df5fb06091af72dc4d414a4b02ba6ab4bf772bfc79e09b
BLAKE2b-256 checksum
How to use checksums
945043a6f9748364e7589bfb9b51a8567481911912fc239148a577a5877d2829
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / windows_capture_device_list-1.1.0-cp36-cp36m-win_amd64.whl

Download URL windows_capture_device_list-1.1.0-cp36-cp36m-win_amd64.whl
Size 14.2 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
f6f5fad2debaa858271bb9d26d86f69e7f6da7e103b237f0d9346330fd55f2d4
BLAKE2b-256 checksum
How to use checksums
d19b6efe7e22a23f84bd4c2c0454db7a71ed36d1ca4e8b95d511d9a73e39c0f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for windows-capture-device-list 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page