title: How to correctly check if a camera is available?
body: <p>I am using OpenCV to open and read from several webcams. It all works fine, but I cannot seem to find a way to know if a camera is available.</p>
  
  <p>I tried this code (cam 2 does not exist):</p>
  
  <pre><code>import cv2
  try:
      c = cv2.VideoCapture(2)
  except:
      print "Cam 2 is invalid."
  </code></pre>
  
  <p>But this just prints a lot of errors:</p>
  
  <pre><code>VIDEOIO ERROR: V4L: index 2 is not correct!
  failed to open /usr/lib64/dri/hybrid_drv_video.so
  Failed to wrapper hybrid_drv_video.so
  failed to open /usr/lib64/dri/hybrid_drv_video.so
  Failed to wrapper hybrid_drv_video.so
  GStreamer Plugin: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
  OpenCV Error: Unspecified error (GStreamer: unable to start pipeline
  ) in cvCaptureFromCAM_GStreamer, file /builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_gstreamer.cpp, line 832
  VIDEOIO(cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, reinterpret_cast&lt;char *&gt;(index))): raised OpenCV exception:
  
  /builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_gstreamer.cpp:832: error: (-2) GStreamer: unable to start pipeline
   in function cvCaptureFromCAM_GStreamer
  
  OpenCV Error: Unspecified error (unicap: failed to get info for device
  ) in CvCapture_Unicap::initDevice, file /builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_unicap.cpp, line 139
  VIDEOIO(cvCreateCameraCapture_Unicap(index)): raised OpenCV exception:
  
  /builddir/build/BUILD/opencv-3.2.0/modules/videoio/src/cap_unicap.cpp:139: error: (-2) unicap: failed to get info for device
   in function CvCapture_Unicap::initDevice
  
  CvCapture_OpenNI::CvCapture_OpenNI : Failed to enumerate production trees: Can't create any node of the requested type!
  &lt;VideoCapture 0x7fa5b5de0450&gt;
  </code></pre>
  
  <p>No exception is thrown. When using <code>c.read()</code> later, I do get <code>False</code>, but I would like to do this in the initialisation phase of my program.</p>
  
  <p>So, how do I find out how many valid cameras I have or check if a certain number 'maps' to a valid one? </p>