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 details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.10 Windows x86-64

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

Uploaded CPython 3.9 Windows x86-64

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

Uploaded CPython 3.8 Windows x86-64

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

Uploaded CPython 3.7m Windows x86-64

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

Uploaded CPython 3.6m Windows x86-64

File details

Details for the file windows-capture-device-list-1.1.0.tar.gz.

File metadata

File hashes

Hashes for windows-capture-device-list-1.1.0.tar.gz
Algorithm Hash digest
SHA256 cc49dd04b830e813347524e2043a860f90ea37508cac60db658753e8fcc02e43
MD5 65fc0badfcff087b8b142c9722613337
BLAKE2b-256 2bf31baf7456fe49f95a1957de070d4ae5d87338765e062b4cf5867a1e5c92c5

See more details on using hashes here.

File details

Details for the file windows_capture_device_list-1.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for windows_capture_device_list-1.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c94f19d5f000947aa2eddadafc27a6b260fc3054023396772d51255f108d007f
MD5 95ce3bdf4bee3be0c2075580891d08fc
BLAKE2b-256 87372bfd89b026331e4a77706fc29a2e84bcd9a22b322b9fa3f53946338e32ca

See more details on using hashes here.

File details

Details for the file windows_capture_device_list-1.1.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for windows_capture_device_list-1.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5b38d59d0d997cd45154e5e9f0c6205c2eb3a68deccbdf9412f761bbc2d63207
MD5 67b34f1f4c42be9d0b1c20b4307cf8d0
BLAKE2b-256 a5b042ce1b7c61a934999e898a3bb96a7c975a15d8ab4cbddf477481cdad5c60

See more details on using hashes here.

File details

Details for the file windows_capture_device_list-1.1.0-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for windows_capture_device_list-1.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a6358c6e8cddde4fc4bc3910595397a6662d2335297edccebc0e87f6d3b330b0
MD5 cc99f4357a15c42b76a5f4efc1b3f506
BLAKE2b-256 efb798d585bdb86f9118a10ca7e6e6be65f9f903e9d1a78fbd32058ef69256d6

See more details on using hashes here.

File details

Details for the file windows_capture_device_list-1.1.0-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for windows_capture_device_list-1.1.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 94d4c3781400c52d81df5fb06091af72dc4d414a4b02ba6ab4bf772bfc79e09b
MD5 50aedd7555ff2df26f5e23bb84ee8891
BLAKE2b-256 945043a6f9748364e7589bfb9b51a8567481911912fc239148a577a5877d2829

See more details on using hashes here.

File details

Details for the file windows_capture_device_list-1.1.0-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for windows_capture_device_list-1.1.0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 f6f5fad2debaa858271bb9d26d86f69e7f6da7e103b237f0d9346330fd55f2d4
MD5 6dbabbbc28822f7b6fac309957f006c4
BLAKE2b-256 d19b6efe7e22a23f84bd4c2c0454db7a71ed36d1ca4e8b95d511d9a73e39c0f9

See more details on using hashes here.

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