Ask Your Question
0

better ways to re-initialize webcam via VideoCapture?

asked 2019-04-19 04:46:56 -0600

gino0717 gravatar image

I'm considering a condition: if my computer is running but the USB of a webcam is accidentally unplugged. I want to write something to make the webcam can re-initialize.

int main()
{
   VideoCapture video(0);
    Mat frame;
   while (true) {
       if(video.isOpened()&&video.grab())
       {
           video>>frame;
           if(frame.cols>0)
           imshow("show video",frame);
           waitKey(2);
       }
       else {
               video.release();
               cout<<"wait key input"<<endl;
               char temp[1]; //get a key input
               cin>>temp;
               if(temp[0]!=NULL)
               {
                  VideoCapture video_temp(0);
                  video=video_temp;
               }
       }
   }
}
  1. not sure but it seems video.isOpened() is not always return false even if the USB webcam is not available any more after the webcam first be launched so I may need a video.grab() to do some double check.

  2. in Linux the system can immediately detect the disconnect of webcam. However, in windows system, it takes a while ( about 30 seconds ) to find out that the webcam is disconnected.

is there any better way to make the webcam re-initialize in the program?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-04-19 06:03:41 -0600

Yashas gravatar image

updated 2019-04-19 06:05:52 -0600

You can use the open method to restart webcam capture.

VideoCapture cap(0);
// some code here
cap.open(0);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-04-19 04:46:56 -0600

Seen: 3,377 times

Last updated: Apr 19 '19