Search code examples
c++opencvip-cameradigest-authenticationopencv3.3

OpenCV VideoCapture digest authentication


I have an ongoing project accessing multiple IP cameras through opencv VideoCapture, working for most of them.

I ve got a new Dahua PTZ camera that uses digest authentication, and the VideoCapture in OpenCV can't open it. Through WireShark i could see that the camera is returning a 401 Unaothorized.

I found nothing on OpenCV documentation about auth problems.

Maybe i need to use something else that is not OpenCV to solve this?

Here is a minimum working code (if you have a camera to test).

#include <iostream>
#include <imgproc.hpp>
#include <opencv.hpp>
#include <highgui.hpp>

using namespace std;
using namespace cv;
int main(){
    while(1){
        VideoCapture cap("http://login:password@111.111.111.111/cgi-bin/snapshot.cgi");
        if(!cap.isOpened()){
            cout << "bug" << endl;
            continue;
        }
        Mat frame;
        cap >> frame;
        imshow("test", frame);
    }
}

And here is the camera response:

HTTP Unaothorized Response


Solution

  • I solved the problem by using the camera's rtsp stream instead of the http image. thank you! (if you have this problem in your ip camera try the rtsp stream, they should have a command in the documentation).

    Final working code in my Dahua camera:

    #include <iostream>
    #include <imgproc.hpp>
    #include <opencv.hpp>
    #include <highgui.hpp>
    using namespace std;
    using namespace cv;
    int main(){
        VideoCapture cap("rtsp://login:password@111.111.111.111/cam/realmonitor?channel=1?subtype=0");
        if(!cap.isOpened()){
            cout << "bug" << endl;
            return 1;
        }
    
        Mat frame;
        cap >> frame;
        imshow("test", frame);
    
    }
    

    For some reason opencv can perform the digest authentication when using rtsp.