cv2-enumerate-cameras


Namecv2-enumerate-cameras JSON
Version 1.3.0 PyPI version JSON
download
home_pageNone
SummaryEnumerate / List / Find / Detect / Search index for opencv VideoCapture.
upload_time2025-09-12 04:42:33
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseMIT License Copyright (c) 2024 Yu He Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords opencv cv2 enumerate camera video capture
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">

# cv2_enumerate_cameras

![PyPI - Version](https://img.shields.io/pypi/v/cv2-enumerate-cameras)
![Python Version from PEP 621 TOML](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2Fchinaheyu%2Fcv2_enumerate_cameras%2Fmain%2Fpyproject.toml)
![PyPI - Downloads](https://img.shields.io/pypi/dm/cv2-enumerate-cameras)

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

![](https://raw.githubusercontent.com/chinaheyu/cv2_enumerate_cameras/main/assets/script.png)

</div>

## Installation

### Install from PyPI

```commandline
pip install cv2-enumerate-cameras
```

## Example

### Run as Script

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

```commandline
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.

```python
from cv2.videoio_registry import getBackendName
from cv2_enumerate_cameras import supported_backends

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

Supported backends on windows:

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

Supported backends on linux:

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

Supported backends on macOS:

- AVFoundation (CAP_AVFOUNDATION)

### Enumerate Cameras

This is an example showing how to enumerate cameras.

```python
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](https://github.com/opencv/opencv/blob/43112409ef0b711b18c2dc12433ad5e2403aea71/modules/videoio/src/cap.cpp#L384). 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.

```python
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.

```pycon
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.

```pycon
def enumerate_cameras(apiPreference: int = CAP_ANY) -> list[CameraInfo]:
    ...
```

- `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

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

```python
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
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "cv2-enumerate-cameras",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": "opencv, cv2, enumerate, camera, video capture",
    "author": null,
    "author_email": "Yu He <chinaheyu@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/71/cf/a6eb5fa54d7dfc87c93a6161aef57527c6ef4d8239be7cef10d13fb9efd6/cv2_enumerate_cameras-1.3.0.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\r\n\r\n# cv2_enumerate_cameras\r\n\r\n![PyPI - Version](https://img.shields.io/pypi/v/cv2-enumerate-cameras)\r\n![Python Version from PEP 621 TOML](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2Fchinaheyu%2Fcv2_enumerate_cameras%2Fmain%2Fpyproject.toml)\r\n![PyPI - Downloads](https://img.shields.io/pypi/dm/cv2-enumerate-cameras)\r\n\r\nRetrieve camera's name, VID, PID, and the corresponding OpenCV index.\r\n\r\n![](https://raw.githubusercontent.com/chinaheyu/cv2_enumerate_cameras/main/assets/script.png)\r\n\r\n</div>\r\n\r\n## Installation\r\n\r\n### Install from PyPI\r\n\r\n```commandline\r\npip install cv2-enumerate-cameras\r\n```\r\n\r\n## Example\r\n\r\n### Run as Script\r\n\r\nWe have provided a simple script that prints out information for all cameras. Open a shell and simply run:\r\n\r\n```commandline\r\npython -m cv2_enumerate_cameras\r\n```\r\n\r\n### Supported Backends\r\n\r\nSince 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.\r\n\r\n```python\r\nfrom cv2.videoio_registry import getBackendName\r\nfrom cv2_enumerate_cameras import supported_backends\r\n\r\nfor backend in supported_backends:\r\n    print(getBackendName(backend))\r\n```\r\n\r\nSupported backends on windows:\r\n\r\n- Microsoft Media Foundation (CAP_MSMF)\r\n- DirectShow (CAP_DSHOW)\r\n\r\nSupported backends on linux:\r\n\r\n- GStreamer (CAP_GSTREAMER)\r\n-  V4L/V4L2 (CAP_V4L2)\r\n\r\nSupported backends on macOS:\r\n\r\n- AVFoundation (CAP_AVFOUNDATION)\r\n\r\n### Enumerate Cameras\r\n\r\nThis is an example showing how to enumerate cameras.\r\n\r\n```python\r\nfrom cv2_enumerate_cameras import enumerate_cameras\r\n\r\nfor camera_info in enumerate_cameras():\r\n    print(f'{camera_info.index}: {camera_info.name}')\r\n```\r\n\r\nThe `enumerate_cameras(apiPreference: int = CAP_ANY)` function comes with the default parameter `CAP_ANY`, and you will receive output similar to the following:\r\n\r\n```\r\n1400: HD Webcam\r\n...\r\n700: HD Webcam\r\n701: OBS Virtual Camera\r\n...\r\n```\r\n\r\nThese indices may seem strange, since [opencv defaults to using the high digits of index to represent the backend](https://github.com/opencv/opencv/blob/43112409ef0b711b18c2dc12433ad5e2403aea71/modules/videoio/src/cap.cpp#L384). For example, 701 indicates the second camera on the DSHOW backend (700).\r\n\r\nYou can also select a supported backend for enumerating camera devices.\r\n\r\nHere's an example of enumerating camera devices via the `CAP_MSMF` backend on windows.\r\n\r\n```python\r\nimport cv2\r\nfrom cv2_enumerate_cameras import enumerate_cameras\r\n\r\nfor camera_info in enumerate_cameras(cv2.CAP_MSMF):\r\n    print(f'{camera_info.index}: {camera_info.name}')\r\n```\r\n\r\nOutput:\r\n\r\n```\r\n0: HD Webcam\r\n...\r\n```\r\n\r\nOnce you have found the target camera, you can create a `cv2.VideoCapture` by its **index** and **backend** properties.\r\n\r\n```pycon\r\ncap = cv2.VideoCapture(camera_info.index, camera_info.backend)\r\n```\r\n\r\nWhen 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`.\r\n\r\n### Camera Info\r\n\r\nThe `cv2_enumerate_cameras.enumerate_cameras()` function will return a list of `CameraInfo` objects, each containing information about a specific camera.\r\n\r\n```pycon\r\ndef enumerate_cameras(apiPreference: int = CAP_ANY) -> list[CameraInfo]:\r\n    ...\r\n```\r\n\r\n- `CameraInfo.index`: Camera index for creating `cv2.VideoCapture`;\r\n- `CameraInfo.name`: Camera name;\r\n- `CameraInfo.path`:  Camera device path;\r\n- `CameraInfo.vid`:  Vendor identifier;\r\n- `CameraInfo.pid`:  Product identifier;\r\n- `CameraInfo.backend`: Camera backend.\r\n\r\n### Find Camera by Vendor and Product Identifier\r\n\r\nThis example demonstrates how to automatically select a camera based on its VID and PID and create a `VideoCapture` object.\r\n\r\n```python\r\nimport cv2\r\nfrom cv2_enumerate_cameras import enumerate_cameras\r\n\r\n# define a function to search for a camera\r\ndef find_camera(vid, pid, apiPreference=cv2.CAP_ANY):\r\n    for i in enumerate_cameras(apiPreference):\r\n        if i.vid == vid and i.pid == pid:\r\n            return cv2.VideoCapture(i.index, i.backend)\r\n    return None\r\n\r\n# find the camera with VID 0x04F2 and PID 0xB711\r\ncap = find_camera(0x04F2, 0xB711)\r\n\r\n# read and display the camera frame\r\nwhile True:\r\n    result, frame = cap.read()\r\n    if not result:\r\n        break\r\n    cv2.imshow('frame', frame)\r\n    if cv2.waitKey(1) == ord('q'):\r\n        break\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT License\r\n        \r\n        Copyright (c) 2024 Yu He\r\n        \r\n        Permission is hereby granted, free of charge, to any person obtaining a copy\r\n        of this software and associated documentation files (the \"Software\"), to deal\r\n        in the Software without restriction, including without limitation the rights\r\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\n        copies of the Software, and to permit persons to whom the Software is\r\n        furnished to do so, subject to the following conditions:\r\n        \r\n        The above copyright notice and this permission notice shall be included in all\r\n        copies or substantial portions of the Software.\r\n        \r\n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n        SOFTWARE.\r\n        ",
    "summary": "Enumerate / List / Find / Detect / Search index for opencv VideoCapture.",
    "version": "1.3.0",
    "project_urls": {
        "Homepage": "https://github.com/chinaheyu/cv2_enumerate_cameras",
        "Issues": "https://github.com/chinaheyu/cv2_enumerate_cameras/issues"
    },
    "split_keywords": [
        "opencv",
        " cv2",
        " enumerate",
        " camera",
        " video capture"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "3c38cc570b8579a2be52d0a3f6dba7afad536bc6d41cfc44108727ef7d8b185a",
                "md5": "7b8772722233e686cb760bf6b7a10f54",
                "sha256": "bd24be3c2df42b88da59e9f6029b97484fc8468678a2be3900aa717835c40f6a"
            },
            "downloads": -1,
            "filename": "cv2_enumerate_cameras-1.3.0-cp32-abi3-win32.whl",
            "has_sig": false,
            "md5_digest": "7b8772722233e686cb760bf6b7a10f54",
            "packagetype": "bdist_wheel",
            "python_version": "cp32",
            "requires_python": ">=3.6",
            "size": 20406,
            "upload_time": "2025-09-12T04:42:28",
            "upload_time_iso_8601": "2025-09-12T04:42:28.463717Z",
            "url": "https://files.pythonhosted.org/packages/3c/38/cc570b8579a2be52d0a3f6dba7afad536bc6d41cfc44108727ef7d8b185a/cv2_enumerate_cameras-1.3.0-cp32-abi3-win32.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "9def0a6e095462e90d12358ef68653c7ea4d86dd6f5dd8f433279ecb5745e9ca",
                "md5": "739bb60e214de83790caf40c7e9b2bb5",
                "sha256": "ef892cc7d92dec3ad0f696fa592a9d5461e593698048f84e877353cffcfc486f"
            },
            "downloads": -1,
            "filename": "cv2_enumerate_cameras-1.3.0-cp32-abi3-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "739bb60e214de83790caf40c7e9b2bb5",
            "packagetype": "bdist_wheel",
            "python_version": "cp32",
            "requires_python": ">=3.6",
            "size": 21296,
            "upload_time": "2025-09-12T04:42:30",
            "upload_time_iso_8601": "2025-09-12T04:42:30.870718Z",
            "url": "https://files.pythonhosted.org/packages/9d/ef/0a6e095462e90d12358ef68653c7ea4d86dd6f5dd8f433279ecb5745e9ca/cv2_enumerate_cameras-1.3.0-cp32-abi3-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "7ed56a6b74c92b6901d7077eb67284010354d90762702ed3451ea5d6297ef692",
                "md5": "05223b3c2febe4c8de6a8ba934098ce1",
                "sha256": "7a67d8cc98fd112c3df51ff699c6953884e96088ced2ee05191bdeaaa2e1e847"
            },
            "downloads": -1,
            "filename": "cv2_enumerate_cameras-1.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "05223b3c2febe4c8de6a8ba934098ce1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 11571,
            "upload_time": "2025-09-12T04:42:32",
            "upload_time_iso_8601": "2025-09-12T04:42:32.272483Z",
            "url": "https://files.pythonhosted.org/packages/7e/d5/6a6b74c92b6901d7077eb67284010354d90762702ed3451ea5d6297ef692/cv2_enumerate_cameras-1.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "71cfa6eb5fa54d7dfc87c93a6161aef57527c6ef4d8239be7cef10d13fb9efd6",
                "md5": "bdacd4d5e7d96a9cc130cb5c272492dd",
                "sha256": "795d8005b0e92ca117e1b8c4849c752edb39b7e2a1818b2d7ac4463aa26caac1"
            },
            "downloads": -1,
            "filename": "cv2_enumerate_cameras-1.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "bdacd4d5e7d96a9cc130cb5c272492dd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 12455,
            "upload_time": "2025-09-12T04:42:33",
            "upload_time_iso_8601": "2025-09-12T04:42:33.549651Z",
            "url": "https://files.pythonhosted.org/packages/71/cf/a6eb5fa54d7dfc87c93a6161aef57527c6ef4d8239be7cef10d13fb9efd6/cv2_enumerate_cameras-1.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-12 04:42:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "chinaheyu",
    "github_project": "cv2_enumerate_cameras",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "cv2-enumerate-cameras"
}
        
Elapsed time: 3.67678s