Ask Your Question
0

I want to capture video from webcam, but window disappears after capturing one frame

asked 2017-01-19 07:57:55 -0600

kartikeygupta9097 gravatar image

updated 2017-01-19 08:20:43 -0600

berak gravatar image

Hi. I'm trying to capture video from my webcam via OpenCV C++. The program works fine, but the window terminates after displaying just one frame. I've tried Python examples as well on my computer, and they work as advertised. Help please.

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
    VideoCapture stream1(0);   //0 is the id of video device.0 if you have only one camera.
    if (!stream1.isOpened())//check if video device has been initialised
        cout << "cannot open camera";
    //unconditional loop
    while (true)
    {
        Mat cameraFrame;
        stream1.read(cameraFrame);
        imshow("cam", cameraFrame);
        if (waitKey(0) >= 0)
            break;
    }
    return 0;
}
edit retag flag offensive close merge delete

Comments

show your code, please. also if there are any messages, those, too !

berak gravatar imageberak ( 2017-01-19 08:08:28 -0600 )edit

@berak This is the link (https://gist.github.com/kartikeygupta/69b675d96829f52880dcb952c7a544c5 (https://gist.github.com/kartikeygupta...)).

kartikeygupta9097 gravatar imagekartikeygupta9097 ( 2017-01-19 08:15:39 -0600 )edit

It just displays one still image and that's it. The guy who wrote the code promised that it'll display a live, moving video stream.

kartikeygupta9097 gravatar imagekartikeygupta9097 ( 2017-01-19 08:17:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-01-19 08:22:30 -0600

berak gravatar image

updated 2017-01-19 08:27:34 -0600

problem is here:

  waitKey(0);  //  -- will wait **forever**.

rather use:

  if (waitKey(10) == 27)  //  -- sleep for 10 microseconds, wait for "esc".
        break;
edit flag offensive delete link more

Comments

So now, it displays the the output of the webcam for a quick second, and then the program terminates. What should I do to display it like a video, and just one peek at a still image.

Here's the python example that works fine... https://gist.github.com/kartikeygupta/15e10e02bb35e1fce821f8ff6ef723b9 (https://gist.github.com/kartikeygupta...)

kartikeygupta9097 gravatar imagekartikeygupta9097 ( 2017-01-19 08:27:43 -0600 )edit

you can no more compare the waitKey output against 0 in opencv3.2 (recent changes)

rather check a specific key, lie the 'esc' one, or 'q'.

berak gravatar imageberak ( 2017-01-19 08:54:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-19 07:57:55 -0600

Seen: 841 times

Last updated: Jan 19 '17