Access mjpeg-streamer in OpenCV 3.0 - c++

I have a raspberry pi b+ uploading a video stream with the mjpeg-streamer.
On the Raspberry Pi
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -y -r 340x240 -f 10 -d /dev/video0" -o "/usr/local/lib/output_http.so -w /usr/local/www"
This creates a video stream accessible by
http://10.1.111.150:8080/?action=stream
making a jpeg image ->
Live stream on the firefox browser
Now, what I would like to do is open this stream with OpenCV 3.0.0 in Visual Studio 2013.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
int q;
VideoCapture cap;
//cap.open(0) for internal camera
//
cap.open("http://10.1.111.150:8080/?action=stream");
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl << "Press q to continue:";
cin >> q;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
namedWindow("MyNegativeVideo", CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;
Mat contours;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
flip(frame, frame, 1);
imshow("MyVideo", frame); //show the frame in "MyVideo" window
Canny(frame, contours, 500, 1000, 5, true);
imshow("MyNegativeVideo", contours);
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;}
This is an example code that works to open the internal webcam on my laptop when I usecap.open(0)
But trying to open the ip camera from its addresscap.open("http://10.1.111.150:8080/?action=stream");
is not working.
UPDATE
I've made some changes on the RPi by running
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so -y -r 340x240 -f 10 -d /dev/video0" -o "/usr/local/lib/output_rtsp.so -p /usr/local/www"
the difference being the output_rtsp.so -p
By the looks the webcam (active light is on) and the program running in the raspberry pi's terminal, I can assume the stream is working. I can't confirm this through a browser though.
Running
cap.open("rtsp://10.1.111.150:8080/?action=stream");
in the openCV code does not open the stream.
Can anyone tell me where I'm going wrong?

Related

OpenCV Video Capture

I am using OpenCV 2.4.8 and Visual Studio 2013 to run the following simple VideoCapture program. The program is intended to capture the video from the webcam and display it.
However, the program works fine only for the FIRST TIME (after signing in windows), and doesn't work second time.
The problem I get on debugging is :
After executing this line - "bool bSuccess = cap.read(frame);" frame variable is still NULL.
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
char key;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if(!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
Mat frame;
while(1)
{
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if(waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
This happen because the camera is not correctly closed after first instance of program. You should try to close the console by esc button and not by clicking X.
Could you try and read more than a single frame before breaking the loop? This may be similar to this problem, where a corrupted first frame / slow camera set up was the only problem.
Unable to read frames from VideoCapture from secondary webcam with OpenCV

how to record webcam video with opencv2.4.9?

#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "ERROR: Cannot open the video file" << endl;
return -1;
}
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
I am developing this project using Visual Studio 2012 express and opencv_2.4.9 and windows 7 x64.
But, this code is not working.
Just write out this message "ERROR: Failed to write the video".
Use try, catch to see if you can get a more detailed error message.
One thing I remember when writing videos is that you need to verify you have the codecs installed and in the correct directory or added as a PATH environment variable.
opencv_ffmpeg.dll was the issue for me, and making sure it was in the correct directory and being renamed to opencv_ffmpeg249.dll

opencv videocapture c++ not working 2 times

I tried the following code for capturing a video from my webcam:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
namedWindow("Changed", CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat imgH = frame + Scalar(75, 75, 75);
imshow("MyVideo", frame); //show the frame in "MyVideo" window
imshow("Changed", imgH);
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Now here's my problem:
After debugging that program for the first time everything works as expected. But when debugging for a second time (after changing some lines in the code) it cannot read from the camera.
Does anyone have a hint for me how to solve that problem?
Thanks!
The code you posted seems to be working absolutely fine in my case, and the output is as intended.
However please make sure that your webcam is switched on before you run the program, this is important.
Since i have a YouCam client in my computer for the webcam, therefore it shows that i need to start youcam.
Since i dont have enough reputation to post an image, so please see the following link in order to view the output i got when webcam not already switched on.
http://i.imgur.com/h4bTZ7z.png
Hope this helps!!

Visual Studio 2012 error: The application was unable to start correctly (0xc0000007b)

I am trying to run a opencv program. I have configured opencv accordingly but I am getting the Visual Studio 2012 error "The application was unable to start correctly (0xc0000007b)."
Following is the code I am trying to run.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//get the width of frames of the video
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
//get the height of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
//wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
You are probably not including all of your libraries properly. Use Dependency Walker to check whether you are missing anything.

Capturing from Macbook Pro iSight with Opencv

I'm trying to capture frames from a Macbook Pro's iSight using OpenCV 2.4.6, and built using the Apple LLVM 4.2 compiler on Xcode.
However, I don't receive any frames. Usually I set up a while loop to run until the frame is full, but the one below runs for ~30 seconds with no result. How can I debug this?
void testColourCapture() {
cv::VideoCapture capture = cv::VideoCapture(0); //open default camera
if(!capture.isOpened()) {
fprintf( stderr, "ERROR: ColourInput capture is NULL \n" );
}
cv::Mat capFrame;
int frameWaits = 0;
while (capFrame.empty()) {
capture.read(capFrame);
//capture >> capFrame;
cvWaitKey(30);
frameWaits++;
std::cout << "capture >> capFrame " << frameWaits << "\n";
if (frameWaits > 1000) {
break;
}
}
imshow("capFrame", capFrame);
}
I have ensured it is not multi-threaded. Also, capture.isOpened is always returning true.
EDIT: It appears others have had this problem: OpenCV wont' capture from MacBook Pro iSight
EDIT: My procedure for installing opencv was:
$ sudo port selfupdate
$ sudo port install opencv
Then, I dragged libopencv_core.dylib, libopencv_highgui.dylib, libopencv_imgproc.dylib and libopencv_video.dylib into the Frameworks folder of my Xcode project, from /opt/local/lib
OpenCV 2.4.6 is broken and doesn't work with the iSight camera. So install 2.4.5 instead. I've written a step-for-step guide for this: http://accidentalprogramming.blogspot.ch/2013/10/opencv-installation-on-mac-os-x.html
I got it working with following code:
VideoCapture cap = VideoCapture(0); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while(1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}