3

I'm working on camera using opencv 3.0.0 and C++ with Ubuntu 14.04 ,

When I run a program using the camera and stop it with Ctrl + C, I can't get it run for a second time and it gives me the following:

libv4l2: error turning on stream: Invalid argument
VIDIOC_STREAMON: Invalid argument
Cannot read a frame from video stream

I have tried lsusb and the camera (it's built in in laptop not external) is not recognized. Also with cheeze it tells me that there is an error when trying to open the camera.

The only solution is to reboot the laptop.

Any suggestion will be appreciated :) thanks

3

This occure when camera is not released by your program - for example when you kill it using Ctrl + C. You can use custom way to exit camera reading loop - for example wait for Esc:

while (1) { // camera loop
    cap.read(frame);
    ...
    (frame processing)
    ...
    if (waitKey(20) == 27) { // wait for 'Esc' key press for 20ms
        break; //  if 'Esc' key is pressed, break loop
    }
}

Or better, you can catch Ctrl + C (SIGINT signal):

bool stop = false;

void sigIntHandler(int signal) {
    stop = true;
}


std::signal(SIGINT, sigIntHandler);

while (!stop) { // camera loop
    cap.read(frame);
    ...
    (frame processing)
    ...
}

See: Signal handling in C++ reference

0

The problem here is with the kernel

sudo apt-get install linux-generic-lts-utopic

This has done the trick for me

0

You don't mention your platform, but on the Raspberry Pi, this error can occur when running video capture using the v4l2 driver requires and when GPU memory is not set at 128 MB or greater. Run sudo raspi-config and adjust it to 128 using the menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.