jxw
jxw

Reputation: 711

Get usb camera id with open cv2 on windows 10

I have 4 usb cameras interfaced to my computer via usb. Currently I am using opencv-python==4.5.5.64 to connect to the cameras. The problem is, I can't seem to read a unique id of each camera. Currently my code snippet look like below where cameraIndex is an integer. The question is, what should I do after obtaining the cap to read a unique id from the camera that I connected? Even better, is it possible to connect to a camera just by using its unique id? I am running the cameras on a Windows 10 PC.

import cv2
cap = cv2.VideoCapture(cameraIndex, cv2.CAP_MSMF)

Upvotes: 5

Views: 5452

Answers (2)

John
John

Reputation: 405

Here is a lightweight method that uses directshow under the hood.

https://github.com/JohnHardy/pyusbcameraindex

Upvotes: 0

zachm0
zachm0

Reputation: 51

I had the same problem recently, I was able to find an answer here that uses pywin32 and another here that uses a custom .pyd library. The first answer ended up being easier to implement, here is a snippet:

import asyncio
import winrt.windows.devices.enumeration as windows_devices


CAMERA_NAME = "Dino-Lite Premier"

async def get_camera_info():
    return await windows_devices.DeviceInformation.find_all_async(4)

connected_cameras = asyncio.run(get_camera_info())
names = [camera.name for camera in connected_cameras]

if CAMERA_NAME not in names:
    print("Camera not found")
else:
    camera_index = names.index(CAMERA_NAME)
    print(camera_index)

Upvotes: 2

Related Questions