Image Capture with OpenCV - Select Timeout Error - c++

I would like to save a captured image from my webcam attached BeagleBone Black, however when I run the following code, I get the error: select timeout.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio.hpp"
using namespace std;
using namespace cv;
int main(){
cout<< "Hello, OpenCV version: "<< CV_VERSION << endl;
Mat frame;
VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH , 320);
cap.set(CV_CAP_PROP_FRAME_HEIGHT , 240);
cap >> frame;
imwrite("camCap.jpg", frame);
return 0;
}
When I compile and run the code:
root#beaglebone:/home/iroh/test# g++ test.cpp -lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -o test
root#beaglebone:/home/iroh/test# ./test
Hello, OpenCV version: 3.1.0
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != NULL) in Mat, file /home/iroh/Downloads/opencv-3.1.0/modules/core/include/opencv2/core/mat.inl.hpp, line 410
terminate called after throwing an instance of 'cv::Exception'
what(): /home/iroh/Downloads/opencv-3.1.0/modules/core/include/opencv2/core/mat.inl.hpp:410: error: (-215) total() == 0 || data != NULL in function Mat
Aborted
What can be the cause of that problem?
According to the error output, my frame is empty. It is first time I attached this webcam to BeagleBone Black. It works properly on my PC, but I do not know whether it is working successfully with BeagleBone Black.
Here is the dmesg output:
[ 4276.642456] usb 1-1: new high-speed USB device number 2 using musb-hdrc
[ 4277.411212] usb 1-1: New USB device found, idVendor=1e4e, idProduct=0110
[ 4277.411257] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 4277.411284] usb 1-1: Product: USB2.0 Camera
[ 4277.411310] usb 1-1: Manufacturer: Etron Technology, Inc.
[ 4277.532768] uvcvideo: Found UVC 1.00 device USB2.0 Camera (1e4e:0110)
[ 4277.542871] input: USB2.0 Camera as /devices/platform/ocp/47400000.usb/47401c00.usb/musb-hdrc.1.auto/usb1/1-1/1-1:1.0/input/input1
[ 4277.547293] usbcore: registered new interface driver uvcvideo
[ 4277.547322] USB Video Class driver (1.1.1)
What shall I do?
Thanks!

I was running into this same issue and managed to fix it.
Check my answer here

I met the same issue, I guess your working environment was Ubuntu system.
I changed my virtual machine setting "USB2.0 -> USB3.0".
Hope it helps you!

Related

Running PiCamera on Ubuntu in C++

I'm trying to get my PiCamera Module v2.1 running on my RaspberryPi4. Unfortunately I must install the Ubuntu 19.10 64bit distribution. So far so good.
I've installed Opencv4. There was some big trouble because it seems like Ubuntu does not come with VideoCore, raspi-config etc. So I downloaded and updated my firmware with sudo rpi-update and installed userland.
First I tried to open the camera with Videocapture cap(0), but this throws a bunch of errors (see here Ubuntu 19.10: Enabling and using Raspberry Pi Camera Module v2.1) and I've read that this is only for usb cameras (actually i can't believe this, because under Raspbian, I can use the module like this)
So I googled and found this repo https://github.com/cedricve/raspicam. I've installed it, but even with this I cannot get it running.
Again here is what I've down:
install opencv4
update firmware
install userland
writing start_x=1 and "gpu_mem=128" to /boot/firmware/config.txt
doing modprobe bcm2835-v4l2
sudo vcgencmd get_camera results in supported and detected = 1
When I use sudo raspistill -o test.jpg a window opens and the image is saved. But there are some errors:
mmal: mmal_vc_shm_init: could not initialize vc shared memory service
mmal: mmal_vc_component_create: failed to initialise shm for 'vc.camera_info' (7:EIO)
mmal: mmal_component_create_core: could not create component 'vc.camera_info' (7)
mmal: Failed to create camera_info component
Also I need to start it with sudo, although I've run sudo usermod -a -G video ubuntu several times (also rebooted). Strange, isn't?
My example script for accessing the camera is:
#include <iostream>
#include <raspicam/raspicam_cv.h>
using namespace std;
int main ( int argc,char **argv ) {
time_t timer_begin,timer_end;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount=100;
//set camera params
Camera.set( cv::CAP_PROP_FORMAT, CV_8UC1 );
//Open camera
cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
//Start capture
cout<<"Capturing "<<nCount<<" frames ...."<<endl;
time ( &timer_begin );
for ( int i=0; i<nCount; i++ ) {
Camera.grab();
Camera.retrieve ( image);
if ( i%5==0 ) cout<<"\r captured "<<i<<" images"<<std::flush;
}
cout<<"Stop camera..."<<endl;
Camera.release();
}
Compilation is successful:
sudo g++ stream.cpp -I/usr/local/include/opencv4 -I/usr/local/include -L/usr/local/lib -L/opt/vc/lib -lraspicam_cv -lopencv_core -lraspicam -lmmal -lmmal_core -lmmal_util -lopencv_highgui -lmmal_vc_client -lvcos -lbcm_host -o stream
Executing stream (even with sudo) results in:
Opening Camera...
mmal: mmal_component_create_core: could not find component 'vc.ril.camera'
Failed to create camera componentopen Failed to create camera component/home/raspicam/src/private/private_impl.cpp 103
Error opening the camera
Does anyone have an idea what I can try?
Thanks !
I had this error while compiling a ROS node for the raspicam.
I fixed it by adding the following to my CMakeLists.txt:
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-as-needed")
The issue was that the reference to the library containing the 'vc.ril.camera' was optimized out by the linker and count not be found at run time.
Hopefully it will work for you.

opencv cv::VideoCapture indecipherable errors

OpenCV version 3.4.3
Xcode version 10.1 (10B61)
macOS version 10.14.2 (18C54) Mojave
I am using C++ to run an extremely simple program.
This code is used to access the macbook webcam.
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
cv::VideoCapture camera(0);
return 0;
}
These are the messages from the Xcode console.
I couldn't find any resources to debug this situation.
CMIO_Unit_ScopeElement.h:200:SafeGetElement Throwing err: -67454
CMIOUnitFigBaseObjectImpl.c:246:CMIOUnitCreateFromDescription Invalid paramater
CMIO_Unit_Input_Device.cpp:244:GetPropertyInfo CMIOUInputFromProcs::GetPropertyInfo() failed for id 102, Error: -67456
CMIO_DAL_PlugIn.cpp:269:StreamCopyBufferQueue got an error from the plug-in routine, Error: 1852797029
CMIOHardware.cpp:339:CMIOObjectGetPropertyData the System is exiting
CMIOHardware.cpp:339:CMIOObjectGetPropertyData the System is exiting
Program ended with exit code: 0

Opencv raspberry pi 3 video play c++

Im currently working on video processing project on raspberry pi 3 using OpenCV libraries. As a guide im reading opencv2 computer vision application programming cookbook. If you are familiar with this book, it explains everything on windows visual studio. But im able to compile things using cmake. And everything works fine.
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
int main()
{
// Open the video file
cv::VideoCapture capture("../bike.avi");
// check if video successfully opened
if (!capture.isOpened()){
std::cout<<"Error loading video!.."<<std::endl;
return 1;
}
// Get the frame rate
double rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame in ms
// corresponds to video frame rate
int delay= 1000/rate;
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file.
// Not required since called by destructor
capture.release();
}
In the book writer uses this code. And i know this code works on linux windows etc but not on raspberry pi. I changed bike.avi with a video that i recorded with raspicam. raspivid -o bike.h264 -h 620 -w 480 -fps 15. But i still get Error loading video!...
Ps: i can play bike.avi video that i downloaded from books website via vlc player using ssh -X.
My CMakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
project(salt)
FIND_PACKAGE(OpenCV REQUIRED)
add_executable(a.out main.cpp)
TARGET_LINK_LIBRARIES(a.out ${OpenCV_LIBS})
I figured out the problem. OpenCV with usb webcams work fine on raspberry pi. But when it comes using raspverry pi camera, its not supported. That is why some developers created RaspiCam libraries which works together OpenCV. They even provide cmake configurations. I installed it and capturing video around 25fps working great. This solution is for C++ users. If u are coding with python, just search python raspberry pi camera OpenCV.

Access an IP camera with Raspberry Pi2, OpenCV and GStreamer: Gstremaer unable to start pipeline

I'm trying to get frames from an IP camera. I can get frames with FFMPEG or GStreamer from my Ubuntu 14.04 PC and omxplayer or gst-launch-1.0 from Raspberry Pi. But when I want to access camera with c++ OpenCV from Raspberry Pi, it is giving me an error about GStreamer pipeline;
(ocvip:2870): GStreamer-WARNING **: Failed to load plugin '/usr/local/lib/gstreamer-1.0/libgstrtsp.so': /usr/local/lib/gstreamer-1.0/libgstrtsp.so: undefined symbol: gst_rtsp_connection_set_tls_validation_flags
GStreamer Plugin: Embedded video playback halted; module u-uridecodebin, reported: URI was not accepted by any element
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline) in icvStartPipeline, file /opt/opencv-2.4.10/modules/highgui/src/cap_gstreamer.cpp, line 383
what(): /opt/opencv-2.4.10/modules/highgui/src/cap_gstreamer.cpp:383: error: (-2) GStreamer unable to start pipeline in function icvStartPipeline
Aborted
code:
const string videoAddress = "rtsp://user:password#xxx.xxx.x.xxx:xxx/cam/realmonitor?channel=1&subtype=1";
int main() {
cv::VideoCapture video;
cv::Mat im;
cv::namedWindow("IP", 1);
video.open(videoAddress);
if (video.isOpened()) {
video >> im;
if (!im.data) break;
cv::imshow("IP", im);
if (cv::waitKey(20) >= 0) break;
}
}
I can access camera with same rtsp address, omxplayer is showing normal gst-launch is showing frames rotated by 180 degrees. I've built GStreamer-1.0 using this page because prebuilt libraries hasn't found by cmake. I found this issue. There are two fixes int this bug but I'm not sure are these the correct solutions and solve my problem because I'm not accessing camera with video.open(0) so I couldn't do like video.open(IPAddress + CV_CAP_PVAPI)?
Thanks.

opencv highgui error

I installed the library opencv in my ubuntu pc and i wrote a program that takes a video from a webcam and it works.
Yesterday I installed the driver for video capture "media_build" to take a video from a video grabber, but the same program doesn't work, while if I open "video for linux 2" on VLC it works.
This is the error:
libv4l2: error set_fmt gave us a different result then try_fmt!
HIGHGUI ERROR: libv4l unable convert to requested pixfmt
HIGHGUI ERROR: V4L: device /dev/video0: Unable to query number of channels
ERROR: capture is NULL
The instruction is:
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
any suggestion?
thanks
I think your program is crashing. If so you can add if(capture!=NULL)
{// your normal code here}else{// display some error message}. In this case the program wont crash. Probably your video driver isn't providing a interface known to openCv.
Maybe you can use this command:
sudo chmod 666 /dev/video0