8

The code given below is for accessing an Axis IP camera using OpenCV. On running the program it first displays "Error in opening cap_ffmpeg_impl..." and then it displays Camera 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?

Deanie
  • 2,316
  • 2
  • 19
  • 35
praxmon
  • 5,009
  • 22
  • 74
  • 121
  • Be sure that all the codecs are somehow installed on your system. Or have you successfully open it from your local machine( test it by placing local path instead of url )? You could try to manually download and install ffmpeg(http://ffmpeg.zeranoe.com/builds/) – Jayesh Bhoi Jan 24 '14 at 05:28
  • Oh, okay. FFMPEG has to be installed before using it? – praxmon Jan 24 '14 at 05:29
  • You need change your URL which you are providing to `VideoCapture cap("http://IPADDRESS/video.mjpg");` – A B Jan 24 '14 at 05:54
  • See the [answer](http://stackoverflow.com/a/21325484/3110262) – A B Jan 24 '14 at 05:59
  • Are you able to stream the camera from browser properly ? – Mayur Jan 24 '14 at 06:07
  • @Mayur yes, the site the link that you gave me provided me the link for live stream, and that's the link I am using. And that is not working. – praxmon Jan 24 '14 at 06:13
  • Have you tried using iSpy, as i have mentioned below. Get the link which iSpy choose & try it. It worked for me. – Mayur Jan 24 '14 at 06:16
  • @Mayur Tried all the links, the same problem :/ – praxmon Jan 24 '14 at 06:26
  • Have installed the ffmpeg codec – Mayur Jan 24 '14 at 06:28
  • I just tried your code. I have faced same error which you are facing. Have you tried the code I have provided as answer.Please try it and see if it works. – Mayur Jan 24 '14 at 06:32
  • I copied your code and then I edited it and provided the correct IP address, then I installed the ffmpeg codec and still no result. – praxmon Jan 24 '14 at 06:36
  • What editting did you do ? – Mayur Jan 24 '14 at 06:36
  • I just double checked your code. Its correct. It worked with my IP camera. Now only two possibility 1> either codec issue or 2> URL. Can you share the URL ? – Mayur Jan 24 '14 at 06:43
  • @Mayur I tried the link given below for installation of ffmpeg. And I tried the updated answer, I think there's something wrong with my installation of ffmpeg. Damn, so close :/ http://www.wikihow.com/Install-FFmpeg-on-Windows – praxmon Jan 24 '14 at 06:43
  • URL: root:root@169.254.159.110:8088/mjpeg.cgi?user=root&password=root&channel=0&.mjpg; Just add http:// in front of it – praxmon Jan 24 '14 at 06:43
  • the IP address you have provided is public IP of your camera ? If yes then i can check on my side with your code with your permission. Later on please delete the comment which has your IP. – Mayur Jan 24 '14 at 06:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45977/discussion-between-prakhar-mohan-srivastava-and-mayur) – praxmon Jan 24 '14 at 06:45
  • I have the same problem too, but never solve it, I try many URL for the camera but it never work, did u modify OpenCV to open a mjpeg link? I think it's the problem here – Tu DUONG Mar 05 '14 at 15:35
  • Nah, never managed to solve this one. Sorry. – praxmon Mar 06 '14 at 12:43
  • I know this is old by now, but is it still unsolved? Having the same issue myself. – Connor Jun 06 '14 at 17:47

3 Answers3

8

I faced similar problem when trying to display IP camera using the public IP camera. Opencv needs some typical kind of URL to open the camera.Try the URL from below code. Heres 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's 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's 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;
    }   

}
Mayur
  • 789
  • 10
  • 37
  • How to find the port number of the IP camera? – praxmon Jan 24 '14 at 05:59
  • You can get the port number from the web interface of your IP camera. – Mayur Jan 24 '14 at 05:59
  • Also, the streaming link for an axis 1103 is: http://IPADDRESS/axis-cgi/mjpg/video.cgi So , what is the link that I should provide? The link with username and password? – praxmon Jan 24 '14 at 06:01
  • One shortcut which might help you is as follows. If you know about iSpy software, it provides all the possible link which can be used for displaying the camera. Also it ask's you that should iSpy select a URL which will is most suitable for displaying the video, let iSpy select the link, try to use the same link in your program it might work. – Mayur Jan 24 '14 at 06:03
  • If the problem is solved with the answer provided please mark it, so that others can also use the same. – Mayur Jan 24 '14 at 06:04
  • Go to this [link](http://www.ispyconnect.com/man.aspx?n=Axis) and find the list of URL for axis camera, this might help. – Mayur Jan 24 '14 at 06:06
  • I have added this code to VideoCapture and exposed the api but this is not working – Shridutt Kothari Jan 05 '15 at 09:25
2

The following works for an Axis M1004-W connected to my computer via ethernet cable:

  1. In the browser of your choice (I'm using Chrome), navigate to the camera's IP address. Provide credentials as necessary.
  2. You should be looking at a live stream from your camera. Right-click on the video stream and select "Inspect Element" (or its equivalent in non-Chrome browsers).
  3. You should see a variable called src - this is what you can use within OpenCV to access the camera directly. Mine is /mjpg/video.mjpg, and I bet yours will be similar.

The address you give to OpenCV should look like this:

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

This is what mine looks like:

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

I entered my address into your code and can see the video stream from an OpenCV window.

Connor
  • 199
  • 1
  • 2
  • 10
0

I installed "Mini WebCam" app on my iphone and used it as an ip camera with "http://192.168.1.103" as it's address. 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)

Mohammad Rahimi
  • 965
  • 5
  • 15