Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
cv2-enumerate-cameras
Advanced tools
Enumerate / List / Find / Detect / Search index for opencv VideoCapture.
Retrieve camera's name, VID, PID, and the corresponding OpenCV index.
pip install cv2_enumerate_cameras
pip install git+https://github.com/chinaheyu/cv2_enumerate_cameras.git
We have provided a simple script that prints out information for all cameras. Open a shell and simply run:
python -m cv2_enumerate_cameras
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:
Currently supported backends on linux:
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
.
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.VideoCapture
ach 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.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
Currently, support for MacOS is in the planning stage, and pull requests are very welcome.
FAQs
Enumerate / List / Find / Detect / Search index for opencv VideoCapture.
We found that cv2-enumerate-cameras demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.