Skip to main content

Enumerate / List / Find / Detect / Search index for opencv VideoCapture.

Project description

cv2_enumerate_cameras

Installation

Install from PyPI

pip install cv2_enumerate_cameras

Install from Source

pip install git+https://github.com/chinaheyu/cv2_enumerate_cameras.git

Example

Run as Script

python -m cv2_enumerate_cameras

Supported Backends

from cv2.videoio_registry import getBackendName
from cv2_enumerate_cameras import supported_backends

for backend in supported_backends:
    print(getBackendName(backend))

Enumerate Cameras

This is an example showing how to enumerate cameras.

from cv2_enumerate_cameras import enumerate_cameras

for camera_info in enumerate_cameras():
    print(f'{camera_info.index}: {camera_info.name}')

Output:

1400: HD Webcam
...
700: HD Webcam
701: OBS Virtual Camera
...

These indices may seem strange, since opencv defaults to using the high digits of index to represent the backend. For example, 701 indicates the second camera on the DSHOW backend (700).

You can also select a supported backend for enumerating camera devices.

Currently supported backends on windows:

  • Microsoft Media Foundation (CAP_MSMF)
  • DirectShow (CAP_DSHOW)

Currently supported backends on linux:

  • GStreamer (CAP_GSTREAMER)
  • V4L/V4L2 (CAP_V4L2)

Here's an example of enumerating camera devices via the CAP_MSMF backend on windows.

import cv2
from cv2_enumerate_cameras import enumerate_cameras

for camera_info in enumerate_cameras(cv2.CAP_MSMF):
    print(f'{camera_info.index}: {camera_info.name}')

Output:

0: HD Webcam
...

Once you have found the target camera, you can create a cv2.VideoCapture by its index and backend properties.

cap = cv2.VideoCapture(camera_info.index, camera_info.backend)

Camera Info

The cv2_enumerate_cameras.enumerate_cameras() function will return a list of CameraInfo objects.

  • CameraInfo.index: Camera index for creating cv2.VideoCapture
  • CameraInfo.name: Camera name
  • CameraInfo.path: Camera device path
  • CameraInfo.vid: Vendor identifier
  • CameraInfo.pid: Product identifier
  • CameraInfo.backend: Camera backend

Find Camera by Vendor and Product Identifier

import cv2
from cv2_enumerate_cameras import enumerate_cameras

def find_camera(vid, pid, apiPreference=cv2.CAP_ANY):
    for i in enumerate_cameras(apiPreference):
        if i.vid == vid and i.pid == pid:
            return cv2.VideoCapture(i.index, i.backend)
    return None

cap = find_camera(0x04F2, 0xB711)
while True:
    result, frame = cap.read()
    if not result:
        break
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break

TODO

  • Windows Support
  • Linux Support
  • MacOS Support

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

cv2_enumerate_cameras-1.1.10.tar.gz (42.4 kB view hashes)

Uploaded Source

Built Distributions

cv2_enumerate_cameras-1.1.10-py3-none-any.whl (30.8 kB view hashes)

Uploaded Python 3

cv2_enumerate_cameras-1.1.10-pp310-pypy310_pp73-win_amd64.whl (40.6 kB view hashes)

Uploaded PyPy Windows x86-64

cv2_enumerate_cameras-1.1.10-pp39-pypy39_pp73-win_amd64.whl (40.6 kB view hashes)

Uploaded PyPy Windows x86-64

cv2_enumerate_cameras-1.1.10-pp38-pypy38_pp73-win_amd64.whl (40.6 kB view hashes)

Uploaded PyPy Windows x86-64

cv2_enumerate_cameras-1.1.10-pp37-pypy37_pp73-win_amd64.whl (40.6 kB view hashes)

Uploaded PyPy Windows x86-64

cv2_enumerate_cameras-1.1.10-cp312-cp312-win_amd64.whl (40.5 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

cv2_enumerate_cameras-1.1.10-cp312-cp312-win32.whl (39.8 kB view hashes)

Uploaded CPython 3.12 Windows x86

cv2_enumerate_cameras-1.1.10-cp311-cp311-win_amd64.whl (40.5 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

cv2_enumerate_cameras-1.1.10-cp311-cp311-win32.whl (39.8 kB view hashes)

Uploaded CPython 3.11 Windows x86

cv2_enumerate_cameras-1.1.10-cp310-cp310-win_amd64.whl (40.5 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

cv2_enumerate_cameras-1.1.10-cp310-cp310-win32.whl (39.8 kB view hashes)

Uploaded CPython 3.10 Windows x86

cv2_enumerate_cameras-1.1.10-cp39-cp39-win_amd64.whl (40.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

cv2_enumerate_cameras-1.1.10-cp39-cp39-win32.whl (39.8 kB view hashes)

Uploaded CPython 3.9 Windows x86

cv2_enumerate_cameras-1.1.10-cp38-cp38-win_amd64.whl (40.5 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

cv2_enumerate_cameras-1.1.10-cp38-cp38-win32.whl (39.8 kB view hashes)

Uploaded CPython 3.8 Windows x86

cv2_enumerate_cameras-1.1.10-cp37-cp37m-win_amd64.whl (40.5 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

cv2_enumerate_cameras-1.1.10-cp37-cp37m-win32.whl (39.8 kB view hashes)

Uploaded CPython 3.7m Windows x86

cv2_enumerate_cameras-1.1.10-cp36-cp36m-win_amd64.whl (26.8 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

cv2_enumerate_cameras-1.1.10-cp36-cp36m-win32.whl (26.1 kB view hashes)

Uploaded CPython 3.6m Windows x86

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