View Full Version : OpenCV C++ Bad file descriptor. USB Lifecam
jreneew2
09-02-2016, 19:48
Hello all, I am having an issue with the USB Lifecam and getting an image to an an openCV mat. My code works fine with an axis camera, but because ours broke a couple days ago I am trying to get the USB camera to work with the code. It opens the stream fine, but ends up with a width and height of zero. The error log is:
IM AM IN PROCESS IMAGE!
VIDIOC_QBUF: Bad file descriptor
Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
Here is our code:
// opens up the camera stream and tries to load it
videoCapture = cv::VideoCapture();
// replaces the ##.## with your team number
videoCapture.open(0);
// Example
// cap.open("http://10.30.19.11/mjpg/video.mjpg");
// wait until it is opened
while(!videoCapture.isOpened()){
printf("videoCapture could not open!\n");
}
// time to actually process the acquired images
processImage();
processImage:
videoCapture.read(matOriginal);
if(matOriginal.empty()) {
break;
}
Im not really sure why but it seems like the image is empty. I tried putting the image grabbing in a loop, because I hear it could take a while to grab an image. However, no dice.
I'm really hoping someone else has had a similar issue because our team cannot afford another axis camera.
Thanks,
Drew
Turing'sEgo
09-02-2016, 20:36
Instead of videoCapture.read(matOriginal), try videoCapture >> matOriginal.
PaulDavis1968
09-02-2016, 22:28
Instead of videoCapture.read(matOriginal), try videoCapture >> matOriginal.
I can't remember Drew, but did we not try that?
jreneew2
09-02-2016, 22:29
Yes, we did try that and had the same effect.
Turing'sEgo
09-02-2016, 23:03
I figured it wouldn't make a difference but you never know.
Would you link to all your vision code so I can take a look at it and play around with it?
A fairly simple thing you could try would be to reinstall opencv through terminal:
sudo apt-get remove --purge opencv
sudo apt-get install libopencv-dev
This won't give you gpu support (I don't think at least), but it's better to have something than nothing.
Edit 1
I found your code on another thread.
A thing I found was that you declare videoCapture, then inside a while loop you say: videoCapture = cv::VideoCapture(); Seems a little redundant. I'm not sure this is the cause to your error or not. Moving on to the next line, videoCapture.open("http://axis-camera.local/mjpg/video.mjpg"); isn't wrong, but it may be incorrect to have it in a while loop....
Edit 2
I just tested calling .open(0) in a while loop. While it did run, it ran at <1fps. I also tested having videoCapture = cv::VideoCapture(); inside a while loop too, and all it did was further bog down the program.
Could you run in debugger mode and let me know which line is causing the error?
Edit 3
Giving this a little more thought: I am not rehearsed in IP cameras, so this could be completely off, but...
Would you test the following code for me? http://pastebin.com/6vrLBVtE
jreneew2
10-02-2016, 05:29
Yes. I will test it tonight. Thank you for all the help.
I just tried the code you posted and it did not work. This was the error I got.
VIDIOC_REQBUFS: Cannot allocate memory
VIDIOC_QBUF: Bad file descriptor
No frame
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/OpenCVArm/opencv-2.4.10/modules/highgui/src/window.cpp, line 261
Unable to stop the stream.: Inappropriate ioctl for device
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
terminate called after throwing an instance of 'cv::Exception'
what(): /home/OpenCVArm/opencv-2.4.10/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow
I cant run the command you gave me because it cant find the command sudo. I used 2168's opencv library's. I'm not sure if that makes a difference. I moved the videoCapture = cv::VideoCapture(); and .open(0) outside the loop and it makes no difference.
I'm pretty sure it connects to the camera find but it grabs an empty frame for some reason.
Thanks.
jreneew2
10-02-2016, 16:48
Also, it works fine with the axis camera and connects, just not the usb. But, we cant use the axis camera because it broke a couple of days ago.
How about just opening the camera using :
cv:VideoCapture videoCapture(0);
That should be enough to open the camera and use it. All of the other stuff is just extra steps that could go wrong.
There's a simple example at http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocap ture which should just work if you want to rule out the rest of the code being the problem.
jreneew2
11-02-2016, 18:13
I tested out the example code and it still says bad file descriptor still. I have the most simple code and still the same error.
Here it is:
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
void testVision() {
cv::VideoCapture vcap(0);
cv::Mat image;
//open the video stream and make sure it's opened
std::cout << "in testVision" << std::endl;
std::cout << "about to go into loop" << std::endl;
while(1) {
std::cout << "in loop" << std::endl;
vcap >> image;
//std::cout << "No frame" << std::endl;
std::cout << "Width: " << image.cols << std::endl;
std::cout << "Height: " << image.rows << std::endl;
//if(cv::waitKey(1) >= 0) break;
}
}
I'm honestly completely confused because multiple teams have had this working with no issue.
jreneew2
12-02-2016, 11:33
Alright, I got the USB camera working. I reimaged the roboRIO, installed opencv 3.0 on the roboRIO and now it works! Thanks to the robotpy who helped me get this working and their repository with opencv code.
Their code is here:https://github.com/robotpy/roborio-opencv
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.