face recognize occur error in execute train method.

asked 2018-08-29 04:37:42 -0600

rainY gravatar image

detail code :

private LoaderCallbackInterface mLoaderCallback = new LoaderCallbackInterface() {
    @Override
    public void onManagerConnected(int status) {
        if (status==LoaderCallbackInterface.SUCCESS){
               ...
               ArrayList<Mat> mDataSetImages = new ArrayList<>();
               ArrayList<Integer> mDataSetLabels = new ArrayList<>();
               InputStream inputStream = getAssets().open("at.txt");
               BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
               try {
                     String a = bufferedReader.readLine();
                     while (a!=null){
                     String[] s = a.split(";");
                     Mat mat = Imgcodecs.imread(s[0]);
                     mDataSetImages.add(mat);
                     mDataSetLabels.add(Integer.parseInt(s[1]));
                     a = bufferedReader.readLine();
               }
              inputStream.close();
              bufferedReader.close();
              trainImgWidth = mDataSetImages.get(0).cols();
              trainImgHeight = mDataSetImages.get(0).rows();
              Log.e(TAG, "getContentFromFile: trainImgWidth = "+trainImgWidth );
              Log.e(TAG, "getContentFromFile: trainImgHeight = "+trainImgHeight );
              int  labs = new int[mDataSetLabels.size()];
              for (int i=0;i<mDataSetLabels.size();i++){
                 labs[i] = mDataSetLabels.get(i);
              }

             } catch (IOException e) {
                 e.printStackTrace();
             }
              Mat mat = new Mat(labs.length(),1,CvType.CV_32S);
              mat.put(0,0,labs);
              faceRecognizer = FisherFaceRecognizer.create();
              faceRecognizer.train(mDataSetImages,mat);
              ...
        }
}

then ,error occur! The error points to the train method.
How can I solve it?

Caused by: CvException [org.opencv.core.CvException: cv::Exception: OpenCV(3.4.2-dev) ../../modules/core/src/matrix.cpp:830: error: (-13:Image step is wrong) The matrix is not continuous, thus its number of rows can not be changed in function 'cv::Mat cv::Mat::reshape(int, int) const' ] at org.opencv.face.FaceRecognizer.train_0(Native Method) at org.opencv.face.FaceRecognizer.train(FaceRecognizer.java:133)

edit retag flag offensive close merge delete

Comments

I am can only guess:

          trainImgWidth = mDataSetImages.get(0).cols();
          trainImgHeight = mDataSetImages.get(0).rows();

You are assuming that all images in your dataset have the same with and height - make sure this is true - otherwise do resizing(normalisation) on images before feeding to network.

          Mat mat = new Mat(labs.length(),1,CvType.CV_32S);
          mat.put(0,0,labs);

I am not sure what this does - imho another source of errors?

holger gravatar imageholger ( 2018-08-29 06:45:49 -0600 )edit

@holger Database image size is no problem.This database I got from AT&T Facedatabase .
And,The train method needs to pass in two arguments, one is "List<mat> src" and the other is "Mat labels".

      Mat mat = new Mat(labs.length(),1,CvType.CV_32S);
      mat.put(0,0,labs);

above code,mean int[] convert to mat. how to convert int array to mat in android?

rainY gravatar imagerainY ( 2018-08-29 20:40:17 -0600 )edit
1

as always, you have to add a check here:

Mat mat = Imgcodecs.imread(s[0], Imgcodecs.IMREAD_GRAYSCALE);  //<-- we need grayscale for this !
if (mat.empty()) .... image not read ! do something !

(the att folder may contain non-image files, like readme or thumbs.db)

berak gravatar imageberak ( 2018-08-30 00:38:52 -0600 )edit
1

then, ALL your train (and later, test) images need to have exactly the same size for this, so you want to:

Imgproc.resize(mat,mat,new Size(100,100));
berak gravatar imageberak ( 2018-08-30 00:46:27 -0600 )edit