Opencv cannot access my webcam - opencv

I have trouble with accessing webcam using opencv 2.4.3.
My System:
Hp Probook 4530s - HP Fixed HD Webcam
Ubuntu 12.10
OpenCV 2.4.3
İf I want to capture my built-in camera i get ERROR: capture is NULL
I'm using http://opencv.willowgarage.com/wiki/CameraCapture sample code.
Sample code is:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
// A Simple Camera Capture Framework
int main() {
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Show the image captured from the camera in the window and repeat
while ( 1 ) {
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "mywindow", frame );
// Do not release the frame!
//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}
I also tried with xawtv -hwscan using typing terminal. I get this output:
looking for available devices
port 129-144
type : Xvideo, image scaler
name : Intel(R) Textured Video`
/dev/video0: OK
[ -device /dev/video0 ]
type : libv4l
name : HP HD Webcam [Fixed]
flags: capture
then I can access my webcam typing xawtv video0. I think I have no trouble with my webcam.
I have trouble with opencv.

I solved my problem few minutes ago. And I decided share my solution for people who handling similar error.
First I didnt install some of below packets ( I dont remember which of them, so I paste all of them)
libjpeg62-dev
libtiff4-dev
zlib1g-dev
libjasper-dev
libavcodec-dev
libdc1394-22-dev
libgstreamer0.10-dev
libgstreamer-plugins-base0.10-dev
libavformat-dev
libv4l-dev
libswscale-dev
Then You should configure your cmake process with this code
cmake -D CMAKE_BULD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON USE_V4L=ON WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON USE_GStreamer=ON ..
Please notice USE_V4L=ON this code..
I hope you solve after reading my solution.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture webcam;
webcam.open(0);
if(!webcam.isOpened())//**EDITED**
{
std::cout<<"CANNOT OPEN CAM"<<std::endl;
return -1;
}
Mat frame;
while(true)
{
webcam >> frame;
imshow("TEST",frame);
waitKey(20);
}
return 0;
}
Try the above code...

In some cases it is down to the inbuilt cameras response time (as it was in my case). I discovered that the in-built webcam on my HP G62 only "wakes up" after the first opencv cap.read(frame) call. Therefore to get a positive read from the camera (and hence no error later in the code) I made the call several times before proceeding:
if (!cap.read(frame))
{
if(!cap.read(frame))
{
if(!cap.read(frame))
{
if(!cap.read(frame))
{
printf("Cam read error");
}
}
}
}
For me the optimum was 4 read calls, which ensured my camera was awake and on before running through the main block of code. It is possible that a simple "waitKey" call would work and only two read calls, although I haven't tried this.

Related

How to operate an PS eye with openCV

I would like to use the external PS eye cam to save 30 frames a second if that is possible.
I don't know where to find guides or the code it self since I'm pretty sure it should be somewhere online.
Anyhelp would be appreciated. Thanks in advance!
So, getting the ps3eye up and running differs depending on which OS you're running. If you're running any flavor of Debian, the drivers are already there and the code I've got below should work fine.
If you're on Windows, you'll have to find a driver for it. CodeLibrary has a driver already made, but you'll have to pay $3 for it. Link's here.
I don't know for Mac, but a bit of digging found this, which might work.
Once you've got the drivers installed, you should be able to access it like any other camera.
Simple bit of code for that is below:
#include "stdafx.h"
#include <opencv/cxcore.h>
#include <opencv2\core\mat.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/videoio/videoio.hpp>
using namespace cv;
using namespace std;
int main() {
Mat image;
bool escnotpressed = true;
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
cap.set(CV_CAP_PROP_FPS, 30); //sets framerate
String capturePath = "C:/this/is/a/path.avi";
Size frameSize = Size(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
VideoWriter savedCapture;
savedCapture.open(capturePath, VideoWriter::fourcc('M','J','P','G'), 30.0, frameSize, true);
if (!savedCapture.isOpened()) {
return -2;
}
while(escnotpressed) { //loops
cap >> image;
if (image.empty()) {
cout << "camera feed got interrupted" << endl;
return 5; //dies if camera feed is interrupted for some reason
}
imshow("Image", image);
savedCapture << image;
int c = waitKey(10);
if( (char)c == 27 ) { escnotpressed = false;}
}
savedCapture.release();
cout << "Done!" << endl;
}
EDIT: If you've got multiple webcams on your computer, you might need to change that VideoCapture cap(0) to VideoCapture cap(x), where x gives you the right camera.

Error In OpenCv

I installed opencv and configure it from tutorials, but when I am trying a code implementing it, all what is related to opencv is shown as an error as shown in the figure. Can you please help me? I really need it
#include "stdafx.h"
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int Main( void)
{
CvCapture* capture = 0;
// Start Capture From WebCam
capture = cvCaptureFromCAM( CV_CAP_ANY );
if( !capture )
{
cout << "No camera Detected" << endl;
}
// Create New Window
cvNamedWindow( "My OpenCV WebCam", CV_WINDOW_AUTOSIZE );
if( capture )
{
cout << "WebCam Is In capture" << endl;
for(;;)
{
// Get Captured Image And Show It In The New Window
// You Can Do Save It Or Filter It
IplImage* iplImg = cvQueryFrame( capture );
// Use This To Filter Image
//cvNot(iplImg, iplImg);
cvShowImage( "My OpenCV WebCam", iplImg );
if( waitKey( 10 ) >= 0 )
break;
}
}
cvReleaseCapture( &capture );
cvDestroyWindow( "My OpenCV WebCam" );
return 0;
}
From the comments to the question it turned out that the OP was linking to OpenCV 2.4.8, instead of OpenCV 3.0.0.
Correcting the linked libraries name, as well as the library path, solved the issue.

IP camera capture

I am trying to capture the stream of two IP cameras directly connected to a mini PCIe dual gigabit expansion card in a nVidia Jetson TK1.
I achieved to capture the stream of both cameras using gstreamer with the next command:
gst-launch-0.10 rtspsrc location=rtsp://admin:123456#192.168.0.123:554/mpeg4cif latency=0 ! decodebin ! ffmpegcolorspace ! autovideosink rtspsrc location=rtsp://admin:123456#192.168.2.254:554/mpeg4cif latency=0 ! decodebin ! ffmpegcolorspace ! autovideosink
It displays one window per camera, but gives this output just when the capture starts:
WARNING: from element /GstPipeline:pipeline0/GstAutoVideoSink:autovideosink1/GstXvImageSink:autovideosink1-actual-sink-xvimage: A lot of buffers are being dropped.
Additional debug info:
gstbasesink.c(2875): gst_base_sink_is_too_late (): /GstPipeline:pipeline0/GstAutoVideoSink:autovideosink1/GstXvImageSink:autovideosink1-actual-sink-xvimage:
There may be a timestamping problem, or this computer is too slow.
---> TVMR: Video-conferencing detected !!!!!!!!!
The stream is played good, with "good" synchronization also between cameras, but after a while, suddenly one of the cameras stops, and usually few seconds later the other one stops too. Using an interface snifer like Wireshark I can check that the rtsp packets are still sending from the cameras.
My purpose is to use this cameras to use them as a stereo camera using openCV. I am able to capture the stream with OpenCV with the following function:
camera[0].open("rtsp://admin:123456#192.168.2.254:554/mpeg4cif");//right
camera[1].open("rtsp://admin:123456#192.168.0.123:554/mpeg4cif");//left
It randomnly starts the capture good or bad, synchronized or not, with delay or not, but after a while is impossible to use the captured images as you can observe in the image:
And the output while running the openCV program usually is this: (I have copied the most complete one)
[h264 # 0x1b9580] slice type too large (2) at 0 23
[h264 # 0x1b9580] decode_slice_header error
[h264 # 0x1b1160] left block unavailable for requested intra mode at 0 6
[h264 # 0x1b1160] error while decoding MB 0 6, bytestream (-1)
[h264 # 0x1b1160] mmco: unref short failure
[h264 # 0x1b9580] too many reference frames
[h264 # 0x1b1160] pps_id (-1) out of range
The used cameras are two SIP-1080J modules.
Anyone knows how to achieve a good capture using openCV? First of all get rid of those h264 messages and have stable images while the program executes.
If not, how can I improve the pipelines and buffers using gstreamer to have a good capture without the sudden stop of the stream?. Although I never captured through openCV using gstreamer, perhaps some day I will know how to do it and solve this problem.
Thanks a lot.
After some days of deep search and some attempts, I turned on directly to use the gstreamer-0.10 API. First I learned how to use it with the tutorials from http://docs.gstreamer.com/pages/viewpage.action?pageId=327735
For most of the tutorials, you just need to install libgstreamer0.10-dev and some other packages. I installed all by:
sudo apt-get install libgstreamer0*
Then copy the code of the example you want to try into a .c file and type from the terminal in the folder where the .c file is located (In some examples you have to add more libs to pkg-config):
gcc basic-tutorial-1.c $(pkg-config --cflags --libs gstreamer-0.10) -o basic-tutorial-1.c
After that I did not feel lost I started to try to mix some c and c++ code. You can compile it using a proper g++ command, or with a CMakeLists.txt or the way you want to... As I am developing with a nVidia Jetson TK1, I use Nsight Eclipse Edition and I need to configure the project properties properly to be able to use the gstreamer-0.10 libs and the openCV libs.
Mixing some code, finally I am able to capture the streams of my two IP cameras in real time without appreciable delay, without bad decoding in any frame and both streams synchronized. The only thing left that I have not solved yet is the obtaining of frames in color and not in gray scale when (I have tried with other CV_ values with "segmentation fault" result):
v = Mat(Size(640, 360),CV_8U, (char*)GST_BUFFER_DATA(gstImageBuffer));
The complete code is next where I capture using gstreamer, transform the capture to a openCV Mat object and then show it. The code is for just a capture of one IP camera. You can replicate the objects and methods for capture multiple cameras at the same time.
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/video.hpp>
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <gst/app/gstappbuffer.h>
#include <glib.h>
#define DEFAULT_LATENCY_MS 1
using namespace cv;
typedef struct _vc_cfg_data {
char server_ip_addr[100];
} vc_cfg_data;
typedef struct _vc_gst_data {
GMainLoop *loop;
GMainContext *context;
GstElement *pipeline;
GstElement *rtspsrc,*depayloader, *decoder, *converter, *sink;
GstPad *recv_rtp_src_pad;
} vc_gst_data;
typedef struct _vc_data {
vc_gst_data gst_data;
vc_cfg_data cfg;
} vc_data;
/* Global data */
vc_data app_data;
static void vc_pad_added_handler (GstElement *src, GstPad *new_pad, vc_data *data);
#define VC_CHECK_ELEMENT_ERROR(e, name) \
if (!e) { \
g_printerr ("Element %s could not be created. Exiting.\n", name); \
return -1; \
}
/*******************************************************************************
Gstreamer pipeline creation and init
*******************************************************************************/
int vc_gst_pipeline_init(vc_data *data)
{
GstStateChangeReturn ret;
// Template
GstPadTemplate* rtspsrc_pad_template;
// Create a new GMainLoop
data->gst_data.loop = g_main_loop_new (NULL, FALSE);
data->gst_data.context = g_main_loop_get_context(data->gst_data.loop);
// Create gstreamer elements
data->gst_data.pipeline = gst_pipeline_new ("videoclient");
VC_CHECK_ELEMENT_ERROR(data->gst_data.pipeline, "pipeline");
//RTP UDP Source - for received RTP messages
data->gst_data.rtspsrc = gst_element_factory_make ("rtspsrc", "rtspsrc");
VC_CHECK_ELEMENT_ERROR(data->gst_data.rtspsrc,"rtspsrc");
printf("URL: %s\n",data->cfg.server_ip_addr);
g_print ("Setting RTSP source properties: \n");
g_object_set (G_OBJECT (data->gst_data.rtspsrc), "location", data->cfg.server_ip_addr, "latency", DEFAULT_LATENCY_MS, NULL);
//RTP H.264 Depayloader
data->gst_data.depayloader = gst_element_factory_make ("rtph264depay","depayloader");
VC_CHECK_ELEMENT_ERROR(data->gst_data.depayloader,"rtph264depay");
//ffmpeg decoder
data->gst_data.decoder = gst_element_factory_make ("ffdec_h264", "decoder");
VC_CHECK_ELEMENT_ERROR(data->gst_data.decoder,"ffdec_h264");
data->gst_data.converter = gst_element_factory_make ("ffmpegcolorspace", "converter");
VC_CHECK_ELEMENT_ERROR(data->gst_data.converter,"ffmpegcolorspace");
// i.MX Video sink
data->gst_data.sink = gst_element_factory_make ("appsink", "sink");
VC_CHECK_ELEMENT_ERROR(data->gst_data.sink,"appsink");
gst_app_sink_set_max_buffers((GstAppSink*)data->gst_data.sink, 1);
gst_app_sink_set_drop ((GstAppSink*)data->gst_data.sink, TRUE);
g_object_set (G_OBJECT (data->gst_data.sink),"sync", FALSE, NULL);
//Request pads from rtpbin, starting with the RTP receive sink pad,
//This pad receives RTP data from the network (rtp-udpsrc).
rtspsrc_pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (data->gst_data.rtspsrc),"recv_rtp_src_0");
// Use the template to request the pad
data->gst_data.recv_rtp_src_pad = gst_element_request_pad (data->gst_data.rtspsrc, rtspsrc_pad_template,
"recv_rtp_src_0", NULL);
// Print the name for confirmation
g_print ("A new pad %s was created\n",
gst_pad_get_name (data->gst_data.recv_rtp_src_pad));
// Add elements into the pipeline
g_print(" Adding elements to pipeline...\n");
gst_bin_add_many (GST_BIN (data->gst_data.pipeline),
data->gst_data.rtspsrc,
data->gst_data.depayloader,
data->gst_data.decoder,
data->gst_data.converter,
data->gst_data.sink,
NULL);
// Link some of the elements together
g_print(" Linking some elements ...\n");
if(!gst_element_link_many (data->gst_data.depayloader, data->gst_data.decoder, data->gst_data.converter, data->gst_data.sink, NULL))
g_print("Error: could not link all elements\n");
// Connect to the pad-added signal for the rtpbin. This allows us to link
//the dynamic RTP source pad to the depayloader when it is created.
if(!g_signal_connect (data->gst_data.rtspsrc, "pad-added",
G_CALLBACK (vc_pad_added_handler), data))
g_print("Error: could not add signal handler\n");
// Set the pipeline to "playing" state
g_print ("Now playing A\n");
ret = gst_element_set_state (data->gst_data.pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline A to the playing state.\n");
gst_object_unref (data->gst_data.pipeline);
return -1;
}
return 0;
}
static void vc_pad_added_handler (GstElement *src, GstPad *new_pad, vc_data *data) {
GstPad *sink_pad = gst_element_get_static_pad (data->gst_data.depayloader, "sink");
GstPadLinkReturn ret;
GstCaps *new_pad_caps = NULL;
GstStructure *new_pad_struct = NULL;
const gchar *new_pad_type = NULL;
g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));
/* Check the new pad's name */
if (!g_str_has_prefix (GST_PAD_NAME (new_pad), "recv_rtp_src_")) {
g_print (" It is not the right pad. Need recv_rtp_src_. Ignoring.\n");
goto exit;
}
/* If our converter is already linked, we have nothing to do here */
if (gst_pad_is_linked (sink_pad)) {
g_print (" Sink pad from %s already linked. Ignoring.\n", GST_ELEMENT_NAME (src));
goto exit;
}
/* Check the new pad's type */
new_pad_caps = gst_pad_get_caps (new_pad);
new_pad_struct = gst_caps_get_structure (new_pad_caps, 0);
new_pad_type = gst_structure_get_name (new_pad_struct);
/* Attempt the link */
ret = gst_pad_link (new_pad, sink_pad);
if (GST_PAD_LINK_FAILED (ret)) {
g_print (" Type is '%s' but link failed.\n", new_pad_type);
} else {
g_print (" Link succeeded (type '%s').\n", new_pad_type);
}
exit:
/* Unreference the new pad's caps, if we got them */
if (new_pad_caps != NULL)
gst_caps_unref (new_pad_caps);
/* Unreference the sink pad */
gst_object_unref (sink_pad);
}
int vc_gst_pipeline_clean(vc_data *data) {
GstStateChangeReturn ret;
GstStateChangeReturn ret2;
/* Cleanup Gstreamer */
if(!data->gst_data.pipeline)
return 0;
/* Send the main loop a quit signal */
g_main_loop_quit(data->gst_data.loop);
g_main_loop_unref(data->gst_data.loop);
ret = gst_element_set_state (data->gst_data.pipeline, GST_STATE_NULL);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline A to the NULL state.\n");
gst_object_unref (data->gst_data.pipeline);
return -1;
}
g_print ("Deleting pipeline\n");
gst_object_unref (GST_OBJECT (data->gst_data.pipeline));
/* Zero out the structure */
memset(&data->gst_data, 0, sizeof(vc_gst_data));
return 0;
}
void handleKey(char key)
{
switch (key)
{
case 27:
break;
}
}
int vc_mainloop(vc_data* data)
{
GstBuffer *gstImageBuffer;
Mat v;
namedWindow("view",WINDOW_NORMAL);
while (1) {
gstImageBuffer = gst_app_sink_pull_buffer((GstAppSink*)data->gst_data.sink);
if (gstImageBuffer != NULL )
{
v = Mat(Size(640, 360),CV_8U, (char*)GST_BUFFER_DATA(gstImageBuffer));
imshow("view", v);
handleKey((char)waitKey(3));
gst_buffer_unref(gstImageBuffer);
}else{
g_print("gsink buffer didn't return buffer.");
}
}
return 0;
}
int main (int argc, char *argv[])
{
setenv("DISPLAY", ":0", 0);
strcpy(app_data.cfg.server_ip_addr, "rtsp://admin:123456#192.168.0.123:554/mpeg4cif");
gst_init (&argc, &argv);
if(vc_gst_pipeline_init(&app_data) == -1) {
printf("Gstreamer pipeline creation and init failed\n");
goto cleanup;
}
vc_mainloop(&app_data);
printf ("Returned, stopping playback\n");
cleanup:
return vc_gst_pipeline_clean(&app_data);
return 0;
}
I hope this helps!! ;)
uri = 'rtsp://admin:123456#192.168.0.123:554/mpeg4cif'
gst_str = ("rtspsrc location={} latency={} ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, width=(int){}, height=(int){}, format=(string)BGRx ! videoconvert ! appsink sync=false").format(uri, 200, 3072, 2048)
cap= cv2.VideoCapture(gst_str,cv2.CAP_GSTREAMER)
while(True):
_,frame = cap.read()
if frame is None:
break
cv2.imshow("",frame)
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()

Can't open video by OpenCv ErrorUnhandled exception at 0x7695c41f in moving movie.exe:

I write a very simple program to test to open a video by openCV, but I fail.
Here is the code.
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv ;
void main( void) {
namedWindow( "Example3", WINDOW_AUTOSIZE );
VideoCapture cap;
cap.open( "D:\a.mp4" );
cv::Mat frame;
while( 1 ) {
cap >> frame;
//cap.read(frame);
if( !frame.data ) break;
cv::imshow( "Example3", frame );
waitKey(33);
}
}
It just flash the windows, However, if I comment the if( !frame.data ) break;
I will get this:
Unhandled exception at 0x7695c41f in moving movie.exe: Microsoft C++ exception: cv::Exception at memory location 0x0045f968..
Any idea?
cap.open( "D:\a.mp4" ); should be:
cap.open( "D:\\a.mp4" );
Try to put your video in the "current directory" so that you can give the path simply as cap.open( "a.mp4" );.
So, your program should look like following:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv ;
void main( void)
{
namedWindow( "Example3", WINDOW_AUTOSIZE );
VideoCapture cap;
cap.open( "a.mp4" );
cv::Mat frame;
while( 1 )
{
cap >> frame;
//cap.read(frame);
if( !frame.data ) break;
cv::imshow( "Example3", frame );
waitKey(33);
}
}
PS: I think the way you are giving path for video is also wrong, it should be forward slash / not backward which you are using i.e. \

Cannot open videos in Opencv

I am trying to open .avi file in opencv. When I run the code I do not get any error but the video does not play. I searched around and I guess it requires ffmpeg to be installed. So I installed it using
sudo apt-get install libavformat-dev libavcodec-dev libavfilter-dev libswscale-dev
It still does not work. Maybe the compiler needs to know where to find ffmpeg. I am using gcc compiler.
Thanks.
#Paul R: I have just copied the code from the Learning Opencv textbook, so wasn't including it. The code:
#include<stdio.h>
#include<highgui.h>
int main( int argc, char** argv ) {
cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
printf("Inside");
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "Example2", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Example2" );
}
#praks411:
printf("%s",argv[1]);
CvCapture* capture = cvCreateFileCapture( argv[1] );
if(!capture)
{
printf("failed\n");
return -1;
}
It prints the argument correctly and the capture fails.
I think it could be the also the problem of your installation as you have installed ffmpeg after installing opencv, why don't you follow this link for proper installation on ubuntu.
https://help.ubuntu.com/community/OpenCV
http://opencvstart.blogspot.it/2012/12/install-opencv-in-ubuntu-1204.html
I would also recommend to put check on cvCapture if you are able to create correct capture pointer and also try to print argv[1] to see if you are getting proper file name or not.

Resources