How to open usb cam instead of webcam in VideoCapture?

asked 2018-04-06 10:24:10 -0600

MechLc gravatar image

updated 2018-04-06 10:34:47 -0600

I have a small issue I want to open my usb camera instead of my webcam. Here is my C++ code it is very simple because I am learning OpenCV at the moment.

// Device ID: xinput list
// Video Stream: ls -ltrh /dev/video*

#include <opencv2/opencv.hpp>
#include <iostream>

#define webCam 8 
#define usbCam 17

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
 //Open the default video camera
 VideoCapture cap(usbCam);

 // if not success, exit program
 if (cap.isOpened() == false)  
 {
  cout << "Cannot open the video camera" << endl;
  cin.get(); //wait for any key press
  return -1;
 } 

 double dWidth = cap.get(CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
 double dHeight = cap.get(CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

 cout << "Resolution of the video : " << dWidth << " x " << dHeight << endl;

 string window_name = "My Camera Feed";
 namedWindow(window_name); //create a window called "My Camera Feed"

 while (true)
 {
  Mat frame;
  bool bSuccess = cap.read(frame); // read a new frame from video 

  //Breaking the while loop if the frames cannot be captured
  if (bSuccess == false) 
  {
   cout << "Video camera is disconnected" << endl;
   cin.get(); //Wait for any key press
   break;
  }

  //show the frame in the created window
  imshow(window_name, frame);

  //wait for for 10 ms until any key is pressed.  
  //If the 'Esc' key is pressed, break the while loop.
  //If the any other key is pressed, continue the loop 
  //If any key is not pressed withing 10 ms, continue the loop 
  if (waitKey(10) == 27)
  {
   cout << "Esc key is pressed by user. Stoppig the video" << endl;
   break;
  }
 }

 return 0;

}

Here is my Device ID

⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ SYNAPTICS Synaptics Large Touch Screen    id=11   [slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad                id=14   [slave  pointer  (2)]
⎜   ↳ Logitech M510                             id=16   [slave  pointer  (2)]
⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Video Bus                                 id=7    [slave  keyboard (3)]
    ↳ Video Bus                                 id=8    [slave  keyboard (3)]
    ↳ Power Button                              id=9    [slave  keyboard (3)]
    ↳ Integrated_Webcam_HD: Integrate           id=10   [slave  keyboard (3)]
    ↳ Dell WMI hotkeys                          id=12   [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=13   [slave  keyboard (3)]
    ↳ DELL Wireless hotkeys                     id=15   [slave  keyboard (3)]
    ↳ Microsoft LifeCam VX-5000: Micr           id=17   [slave  keyboard (3)]
    ↳ Lauro Cabral’s Keyboard                   id=18   [slave  keyboard (3)]
    ↳ 10:4F:A8:85:B2:13                         id=19   [slave  keyboard (3)]

EDIT1: Microsoft LifeCam is my USB camera

edit retag flag offensive close merge delete

Comments

please remove screenshot and put results as text.

instead of 8 and 17 try 0 and 1

LBerger gravatar imageLBerger ( 2018-04-06 10:30:26 -0600 )edit

Just updated

MechLc gravatar imageMechLc ( 2018-04-06 10:35:29 -0600 )edit

I did and doesnt work

MechLc gravatar imageMechLc ( 2018-04-06 10:35:56 -0600 )edit

What does it mean "doesn't work "? an error message or "Video camera is disconnected" ?

LBerger gravatar imageLBerger ( 2018-04-06 10:37:36 -0600 )edit

as in the code only opens the webcam instead of the usb camera regardless if the param is a 1,0.

MechLc gravatar imageMechLc ( 2018-04-06 10:39:39 -0600 )edit