How to count cameras in OpenCV 2.3?

后端 未结 6 1139
难免孤独
难免孤独 2020-12-01 14:36

I want to get the number of available cameras.

I tried to count cameras like this:

for(int device = 0; device<10; device++) 
{
    VideoCapture ca         


        
相关标签:
6条回答
  • 2020-12-01 14:42

    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())
    
    0 讨论(0)
  • 2020-12-01 14:46

    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.

    0 讨论(0)
  • 2020-12-01 14:48

    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.

    0 讨论(0)
  • 2020-12-01 14:52

    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

    0 讨论(0)
  • 2020-12-01 14:54

    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.

    0 讨论(0)
  • 2020-12-01 14:59

    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.

    0 讨论(0)
提交回复
热议问题