Access IP Cameras Using OpenCV

The code below is for accessing the Axis IP camera using OpenCV. When you start the program, first displays "Error opening cap_ffmpeg_impl ...", and then the camera is not found.

#include <opencv\cv.h> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include <iostream> #include <stdio.h> using namespace std; using namespace cv; int main() { Mat frame; namedWindow("video", 1); VideoCapture cap("http://IPADDRESS/video.mjpg"); if(!cap.isOpened()) { cout<<"Camera not found"<<endl; getchar(); return -1; } while ( cap.isOpened() ) { cap >> frame; if(frame.empty()) break; imshow("video", frame); if(waitKey(30) >= 0) break; } return 0; } 

Where am I going wrong?

+7
c ++ opencv video-streaming ip-camera
source share
3 answers

I had a similar problem when trying to display an IP camera using a public IP camera. Opencv needs a typical type of URL to open the camera. Try the url from the code below. Here is the code that worked for me.

 int main(int, char**) { cv::VideoCapture vcap; cv::Mat image; // This works on a D-Link CDS-932L const std::string videoStreamAddress = "http://ID: PASSWORD@IPADDRESS :PORTNO/mjpeg.cgi?user=ID&password=ID:PASSWORD&channel=0&.mjpg"; //open the video stream and make sure it opened if(!vcap.open(videoStreamAddress)) { std::cout << "Error opening video stream or file" << std::endl; return -1; } for(;;) { if(!vcap.read(image)) { std::cout << "No frame" << std::endl; cv::waitKey(); } cv::imshow("Output Window", image); if(cv::waitKey(1) >= 0) break; } } 

Copy this code as it is and try.

 #include <stdio.h> #include <opencv2/opencv.hpp> #include <iostream> int main(int, char**) { cv::VideoCapture vcap; cv::Mat image; // This works on a D-Link CDS-932L const std::string videoStreamAddress = "http://USER: PWD@IPADDRESS :8088/mjpeg.cgi?user=USERNAME&password=PWD&channel=0&.mjpg"; //open the video stream and make sure it opened if(!vcap.open(videoStreamAddress)) { std::cout << "Error opening video stream or file" << std::endl; return -1; } for(;;) { if(!vcap.read(image)) { std::cout << "No frame" << std::endl; cv::waitKey(); } cv::imshow("Output Window", image); if(cv::waitKey(1) >= 0) break; } } 
+6
source share

The following steps for the Axis M1004-W connected to my computer via an Ethernet cable:

  • In the browser of your choice (I use Chrome) go to the IP address of the camera. Provide credentials if necessary.
  • You should watch live broadcast from the camera. Right-click the video stream and select Inspect Element (or its equivalent in non-Chrome browsers).
  • You should see the src variable - this is what you can use in OpenCV for direct access to the camera. Mine is /mjpg/video.mjpg , and I'm sure yours will be alike.

The address you specify to OpenCV should look like this:

 http://<USERNAME>:<PASSWORD>@<IP_ADDRESS>/<the value of src> 

It looks like this:

 http://uname: login@192.168.0.0 /mjpg/video.mjpg 

I entered my address in my code and see the video stream from the OpenCV window.

+2
source share

I installed the application β€œMini WebCam” on my iphone and used it as an ip camera with β€œ http://192.168.1.103 ” as it was addressed. In addition, I used this piece of code:

 VideoCapture capture; Mat image; if (!capture.open("http://192.168.1.103/video.cgi?.mjpg")) { cout << "Error opening video stream or file" << endl; return -1; } .... 

it works. ( http://192.168.1.103/video.cgi?.mjpg )

0
source share

All Articles