Skip to main content

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

Project description

cv2_enumerate_cameras

PyPI - Version Python Version from PEP 621 TOML PyPI - Downloads

Retrieve camera's name, VID, PID, and the corresponding OpenCV index.

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

We have provided a simple script that prints out information for all cameras. Open a shell and simply run:

python -m cv2_enumerate_cameras

Supported Backends

Since OpenCV supports many different backends, not all of them provide support for camera enumeration. The following code demonstrates how to retrieve the names of the supported backends.

from cv2.videoio_registry import getBackendName
from cv2_enumerate_cameras import supported_backends

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

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)

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}')

The enumerate_cameras(apiPreference: int = CAP_ANY) function comes with the default parameter CAP_ANY, and you will receive output similar to the following:

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.

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)

When using CAP_ANY as an option for the enumerate_cameras function, the backend parameter can be omitted. However, it is highly recommended to pass it along when creating a VideoCapture.

Camera Info

The cv2_enumerate_cameras.enumerate_cameras() function will return a list of CameraInfo objects, each containing information about a specific camera.

def enumerate_cameras(apiPreference: int = CAP_ANY) -> list[CameraInfo]:
    ...
  • CameraInfo.index: Camera index for creating cv2.VideoCaptureach containing all the information about a camera;
  • 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

This example demonstrates how to automatically select a camera based on its VID and PID and create a VideoCapture object.

import cv2
from cv2_enumerate_cameras import enumerate_cameras

# define a function to search for a camera
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

# find the camera with VID 0x04F2 and PID 0xB711
cap = find_camera(0x04F2, 0xB711)

# read and display the camera frame
while True:
    result, frame = cap.read()
    if not result:
        break
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break

TODO

Currently, support for MacOS is in the planning stage, and pull requests are very welcome.

  • 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.18.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distributions

cv2_enumerate_cameras-1.1.18.2-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

cv2_enumerate_cameras-1.1.18.2-cp32-abi3-win_amd64.whl (19.9 kB view details)

Uploaded CPython 3.2+ Windows x86-64

File details

Details for the file cv2_enumerate_cameras-1.1.18.2.tar.gz.

File metadata

File hashes

Hashes for cv2_enumerate_cameras-1.1.18.2.tar.gz
Algorithm Hash digest
SHA256 91ba2970d9c2a80c5671a18408cc5d9403883c2013380eee06a2bc5a705f4614
MD5 6ec6b01b2967c66334fdd84031b2a753
BLAKE2b-256 3702c86273478f9dffbcedceedbdb7add459f07c58950d627ec1a6c62839be1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for cv2_enumerate_cameras-1.1.18.2.tar.gz:

Publisher: publish-to-pypi.yml on chinaheyu/cv2_enumerate_cameras

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cv2_enumerate_cameras-1.1.18.2-py3-none-any.whl.

File metadata

File hashes

Hashes for cv2_enumerate_cameras-1.1.18.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e87f778ad59d20211a5002453557df2e98b8046a89991920d0dd0809df791cfe
MD5 b7187a0f03135c31b991a92ad0b5ec82
BLAKE2b-256 027786696e0617d38d247d9384c5ec8979efb4b3de9715a1ae722335400e49a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for cv2_enumerate_cameras-1.1.18.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on chinaheyu/cv2_enumerate_cameras

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cv2_enumerate_cameras-1.1.18.2-cp32-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for cv2_enumerate_cameras-1.1.18.2-cp32-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b002fee99f684b796a3e609de700fc8235657b778d86fd1c55feb1c67a253c85
MD5 489d10a40cec179769a77f00c77bc432
BLAKE2b-256 099f645f58630aa5fad0598395fd2293dbd6a46512310a0cb591047ab10988e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for cv2_enumerate_cameras-1.1.18.2-cp32-abi3-win_amd64.whl:

Publisher: publish-to-pypi.yml on chinaheyu/cv2_enumerate_cameras

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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