23

I want to get the number of available cameras.

I tried to count cameras like this:

for(int device = 0; device<10; device++) 
{
    VideoCapture cap(device);
    if (!cap.isOpened())
        return device;          
}

If I have a camera connected, it never failed to open. So I tried to preview different devices but I get always the image of my camera.

If I connect a second camera, device 0 is camera 1 and device 1-10 are camera 2.

I think there is a problem with DirectShow devices.

How to solve this problem? Or is there a function like in OpenCV1 cvcamGetCamerasCount()?

I am using Windows 7 and USB cameras.

nobody
  • 19,814
  • 17
  • 56
  • 77
Leon
  • 2,386
  • 2
  • 20
  • 17

6 Answers6

11

OpenCV still has no API to enumerate the cameras or get the number of available devices. See this ticket on OpenCV bug tracker for details.

Behavior of VideoCapture is undefined for device numbers greater then number of devices connected and depends from API used to communicate with your camera. See OpenCV 2.3 (C++,QtGui), Problem Initializing some specific USB Devices and Setups for the list of APIs used in OpenCV.

Community
  • 1
  • 1
Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
10

Even if it's an old post here a solution for OpenCV 2/C++

/**
 * Get the number of camera available
 */
int countCameras()
{
   cv::VideoCapture temp_camera;
   int maxTested = 10;
   for (int i = 0; i < maxTested; i++){
     cv::VideoCapture temp_camera(i);
     bool res = (!temp_camera.isOpened());
     temp_camera.release();
     if (res)
     {
       return i;
     }
   }
   return maxTested;
}

Tested under Windows 7 x64 with :

  • OpenCV 3 [Custom Build]
  • OpenCV 2.4.9
  • OpenCV 2.4.8

With 0 to 3 Usb Cameras

BlouBlou
  • 630
  • 6
  • 13
  • crashing on opencv 2.4.8 when opening camera if does not exists. – Prasaathviki Jun 25 '14 at 08:08
  • 2.4.9 doesnt crash for me (Windows 8.1). However it also finds a camera that is blocked by a different process which will crash if you try to receive a new image and try to use it without a `.isEmpty` check (because the image grabbed by a blocked cam is empty). Somehow OpenCV crashed my camera and didnt unlock it even after pc restart, so I had to deactivate it in the OS and reactivate later... – Micka Jan 28 '15 at 15:09
  • do you have your code somewhere in Git, i have also windows 8.1 and i would like to check if it's a Windows platform problem or an OpenCV bug – BlouBlou Jan 29 '15 at 01:12
  • @jgd : can you tell me witch version are you using, how many cameras, on witch system to be able to check again my code (done a year ago) – BlouBlou Aug 04 '15 at 16:47
  • @BlouBlou Windows, opencv 2.4.2, on tablet with two cameras. And the other test in laptop, with same version of opencv and only one camera – Juan Garcia Aug 05 '15 at 08:04
  • probably depends on device drivers etc installed... Now, about one year later, same code crashes for me. I've installed a GigE camera (not connected while testing) some months ago. – Micka Dec 10 '15 at 09:43
  • Trying to access a camera with a number higher than is available is undefined behaviour. So on some systems it will crash and others it won't. – Jonathan. Jan 19 '16 at 15:40
9

This is a very old post but I found that under Python 2.7 on Ubuntu 14.04 and OpenCv 3 none of the solutions here worked for me. Instead I came up with something like this in Python:

import cv2

def clearCapture(capture):
    capture.release()
    cv2.destroyAllWindows()

def countCameras():
    n = 0
    for i in range(10):
        try:
            cap = cv2.VideoCapture(i)
            ret, frame = cap.read()
            cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            clearCapture(cap)
            n += 1
        except:
            clearCapture(cap)
            break
    return n

print countCameras()

Maybe someone will find this useful.

Artur
  • 445
  • 6
  • 13
5

I do this in Python:

def count_cameras():
    for i in range(10):
        temp_camera = cv.CreateCameraCapture(i-1)
        temp_frame = cv.QueryFrame(temp_camera)
        del(temp_camera)
        if temp_frame==None:
            del(temp_frame)
            return i-1 #MacbookPro counts embedded webcam twice

Sadly Opencv opens the Camera object anyway, even if there is nothing there, but if you try to extract its content, there will be nothing to attribute to. You can use that to check your number of cameras. It works in every platform I tested so it is good.

The reason for returning i-1 is that MacBookPro Counts its own embedded camera twice.

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
NinjasAtWork
  • 71
  • 1
  • 3
2

Python 3.6:

import cv2

# Get the number of cameras available
def count_cameras():
    max_tested = 100
    for i in range(max_tested):
        temp_camera = cv2.VideoCapture(i)
        if temp_camera.isOpened():
            temp_camera.release()
            continue
        return i

print(count_cameras())
FooBar167
  • 2,721
  • 1
  • 26
  • 37
1

I have also faced similar kind of issue. I solved the problem by using videoInput.h library instead of Opencv for enumerating the cameras and passed the index to Videocapture object. It solved my problem.