Skip to main content

Get device list with Windows DirectShow

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

windows-capture-device-list-1.1.0.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distributions

windows_capture_device_list-1.1.0-cp310-cp310-win_amd64.whl (14.2 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

windows_capture_device_list-1.1.0-cp39-cp39-win_amd64.whl (14.2 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

windows_capture_device_list-1.1.0-cp38-cp38-win_amd64.whl (14.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

windows_capture_device_list-1.1.0-cp37-cp37m-win_amd64.whl (14.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

windows_capture_device_list-1.1.0-cp36-cp36m-win_amd64.whl (14.2 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page