Search code examples
c++opencvvectorface-recognition

C++ Debug Assertion Failed While Using Opencv


I am trying to compare two image and check whether they belongs to same person. This code giving Debug assertion failed Like here. I have checked, it can access to photos. Then what is the problem. People say that you are trying to access the something which is not actually exist. However, I could not find anything about it in my code. Regards;

        #include <iostream>
        #include <string>
        #include "opencv2\core\core.hpp"
        #include "opencv2\core.hpp"
        #include "opencv2\face.hpp"
        #include "opencv2\highgui\highgui.hpp"
       #include "opencv2\objdetect\objdetect.hpp"
        #include "opencv2\opencv.hpp"
        #include<direct.h>
        #include "vector"


      using namespace std;
      using namespace cv;
      using namespace cv::face;

     int main() {
       vector<Mat> img;
       img[0]= imread("C://Users//Gökhan//Desktop//faces//faces442.jpg");
       Mat img2 = imread("C://Users//Gökhan//Desktop//faces//faces448.jpg");
       vector<int> label;
       label[0] = 1;
       Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create();
       model->train(img[0], label[0]);
       int predictedLabel = model->predict(img2);
       if (predictedLabel == 1) {
       cout << "found";
  }

  }

Solution

  • You are trying to access elements of vectors that have no elements. You have to allocate elements before accessing. You can use the constructor with specifying the number of elements to allocate to allocate elements, for example. To do this, change the lines

    vector<Mat> img;
    vector<int> label;
    

    to

    vector<Mat> img(1);
    vector<int> label(1);