9

I have 3 identical cameras running in my system, the only difference between them is the USB port where are plugged. I need to load a specific Python script using OpenCV for each camera, and my code is as simple as:

capture = cv2.VideoCapture(index)

where index equals to 0, 1, 2 respectively in each script. The problem comes when I restart my PC and run the same code, sometimes runs as expected and sometimes 1 or 2 cameras had switched their index (not changing any code or unplugging from USB) so the result is loading the script without the corresponding camera.

For example, if in script2.py the camera index defined was 2, then I restart the PC, and now when running script2.py loads the camera with index 0.

Any solutions for this? How can I specify the physical port to VideoCapture() to be always the same? I'm running Win10

Thanks in advance

Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58
  • 1
    Related: [Create openCV VideoCapture from interface name instead of camera numbers](https://stackoverflow.com/questions/35821763/create-opencv-videocapture-from-interface-name-instead-of-camera-numbers) – Burak Nov 06 '20 at 16:04
  • This was pretty usefull, but im running my code on Windows so i need to reach the same usb location on my machine. Thanks a lot. – Jose Luis Bru Quiles Nov 07 '20 at 00:40
  • [How to check available webcams from the command line?](https://askubuntu.com/questions/348838/how-to-check-available-webcams-from-the-command-line#answer-1124192) suggests a Python library. Worth a try. – Burak Nov 07 '20 at 01:37
  • Thanks again for the reply, but even with that library i can't get an unique identifier for each camera, just the name which in my case i have 3 cameras named "H264 USB Camera", so my systems keeps loading them wrong sometimes. – Jose Luis Bru Quiles Nov 19 '20 at 11:22
  • Did you look at [OpenCV with multiple webcams - how to tell which camera is which in code](https://stackoverflow.com/questions/58962748/opencv-with-multiple-webcams-how-to-tell-which-camera-is-which-in-code) – Ahmet Nov 20 '20 at 07:06
  • for C++ and windows OS you could have a look at https://github.com/studiosi/OpenCVDeviceEnumerator (maybe you can write a Python Wrapper?). For Linux you can probably find some video 4 linux tools. BUT depending on your camera type, it might be possible that there are no serial numbers or similar availbale. However, it is probably somehow possible to find out to which hardware port a device is connected?!? – Micka Nov 20 '20 at 07:56
  • 1
    @Ahx The answer is for Ubuntu in your link. But there is mentioned [OpenCV VideoCapture device index / device number](https://stackoverflow.com/questions/41298588/opencv-videocapture-device-index-device-number/41299177) which seems to have the solutions for Windows in [CV-camera-finder](https://github.com/pvys/CV-camera-finder). – Burak Nov 20 '20 at 07:59
  • @Micka I found the physical USB port where each camera is connected, the problem is that i cannot use that value in VideoCapture – Jose Luis Bru Quiles Nov 20 '20 at 16:36
  • opencv uses directShow in windows, so you'll need a way to find directShow device numbers and usn information combined. – Micka Nov 21 '20 at 09:16
  • did you have a look at: https://github.com/studiosi/OpenCVDeviceEnumerator ? there is a devicePath variable for each device, and the id. Maybe this is what you need. Not Python unfortunately, but after testing that this devicePath is what you need you might be able to search for identical Python functions. – Micka Nov 21 '20 at 09:23
  • I think this can be achieved using https://github.com/andreaschiavinato/python_grabber , look at the [first example](https://github.com/andreaschiavinato/python_grabber/blob/master/examples/example_1.py). You would need to know the monikers of each camera though. – Mateo Torres Nov 22 '20 at 03:58
  • Are the three cameras running with default configs and are they different? If so, you can check by getting their attributes like exposure etc using cap.get(). something like this: https://stackoverflow.com/questions/63256300/how-do-i-get-usb-webcam-property-ids-for-opencv – SajanGohil Nov 26 '20 at 08:21
  • Hi guys, at the time of today i haven't resolved this issue, let me explain my iterations and results: 1 - Just selecting the index for each camera -> After every restart the index may have been changed or maybe not 2 - I used DevCon to extract the hardware ID of the cameras for disabling all of them with the purpose of enabling again 1 by 1 and expecting the new index come in the right order -> The index still the same 3 - I made a custom PCB which has 3 relay for switing on/off the power of the cameras to again, powering them in order before use in script -> Still not work Im desperated. – Jose Luis Bru Quiles Dec 18 '20 at 12:56
  • Did you try all the mentioned GitHub projects? It would be nice if you could list the results of each of them. I suggest creating an answer and listing the important steps so that you can get more help and find solutions to specific cases. – Burak Dec 18 '20 at 15:31
  • @Burak Yep i checked them all again just for refreshing and i got some interesting aproach but not completly fine for me. In your previous answer you posted a link to the Camera Finder, and my results were those: It works great, however in my case i got 3 cameras with the same name and i cant recognise them programatically cause all 3 are "HD USB Camera". I tried to change its "friendlyName" property from the windows register to "CAM 1" but cameraFinder always return HD USB Camera. Any help would be extremly apreciated :) – Jose Luis Bru Quiles Dec 18 '20 at 16:32
  • This question attracted the most viewers I've ever seen for an unsolved good question. If none of the proposed methods works, the best you can do is to find a workaround. For example, put different colored small papers in front of each camera and distinguish them by computer vision before the rest of the script. Or an easier solution, switch to Ubuntu :) Good luck! – Burak Dec 18 '20 at 18:24
  • 2
    Well guys, i haven't reached my goal to resolve this issue in a nice and plain way as it supposed to be but i've done a workaround using image analysis detecting the environment for each camera. Despite the fact it works well it seems ridiculous to me not having any opencv function to solve this issue, because this shouldn't be a problem, its just selecting a camera. Thanks a lot for your help guys. – Jose Luis Bru Quiles Jan 18 '21 at 12:56

1 Answers1

-1

I used multiple of the same Huddly IQ USB cameras and directShow backend so I could capture images using the serial number from each camera. The find_video_devices.exe file lists the cameras using directShow, then I used that information to map the serial number from the registry using Python and use OpenCV to capture using the correct index. I made a small repository here. https://github.com/gabrielsenstig/multicameracapture

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34921297) – Ben A. Sep 01 '23 at 22:32
  • Even `find_video_devices.exe` is closed source. I do not see this answer helpful unless directShow code to identify cameras is provided. – Burak Sep 02 '23 at 18:03