3

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)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
jxw
  • 586
  • 2
  • 6
  • 36
  • I think there is no concrete way of achieving what you are asking in your question, you could iterate among the devices connected to your PC and try to identify the appropriate camera for that I recommend reading this post https://stackoverflow.com/questions/8044539/Listing-Available-Devices-in-Python-Opencv – user11717481 Oct 08 '22 at 23:08
  • if you want this, you need to lobby for an extension of the OpenCV API on their issue tracker (github). I'm surprised it hasn't happened yet. people keep needing this. – Christoph Rackwitz Oct 09 '22 at 13:41
  • Yes it is also second time I find myself in need for this feature. For now in the project I am working on we ended up implementing a QR code that is read on the specific camera thereby allow it to identify itself. This is really not an elegant way – jxw Oct 10 '22 at 05:31

1 Answers1

1

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)
zachm0
  • 41
  • 4