1

I want to use face and eye classifiers in my application. The concept is to copy the contents of the classifiers xml files you have under Resources.Raw and add them to some folder under the application using InputStream and OutputStream, then try to load these classifiers from that folder to use them in the app. I'm using the code available here for this purpose,

http://romanhosek.cz/android-eye-detection-updated-for-opencv-2-4-6/

Here is the code portion that loads the classifiers:

 try {
                // load cascade file from application resources
                InputStream is = getResources().openRawResource(
                        R.raw.lbpcascade_frontalface);
                File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
                mCascadeFile = new File(cascadeDir,
                        "lbpcascade_frontalface.xml");
                FileOutputStream os = new FileOutputStream(mCascadeFile);

                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = is.read(buffer)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                is.close();
                os.close();

                // --------------------------------- load left eye
                // classificator -----------------------------------
                InputStream iser = getResources().openRawResource(
                        R.raw.haarcascade_lefteye_2splits);
                File cascadeDirER = getDir("cascadeER",
                        Context.MODE_PRIVATE);
                File cascadeFileER = new File(cascadeDirER,
                        "haarcascade_eye_right.xml");
                FileOutputStream oser = new FileOutputStream(cascadeFileER);

                byte[] bufferER = new byte[4096];
                int bytesReadER;
                while ((bytesReadER = iser.read(bufferER)) != -1) {
                    oser.write(bufferER, 0, bytesReadER);
                }
                iser.close();
                oser.close();

                mJavaDetector = new CascadeClassifier(
                        mCascadeFile.getAbsolutePath());
                if (mJavaDetector.empty()) {
                    Toast.makeText(getApplicationContext(), "face classifier error", Toast.LENGTH_LONG).show();
                    Log.e(TAG, "Failed to load cascade face classifier");
                    mJavaDetector = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from "
                            + mCascadeFile.getAbsolutePath());

                mJavaDetectorEye = new CascadeClassifier(
                        cascadeFileER.getAbsolutePath());
                if (mJavaDetectorEye.empty()) {
                    Toast.makeText(getApplicationContext(), "eye classifer error", Toast.LENGTH_LONG).show();
                    Log.e(TAG, "Failed to load cascade eye classifier");
                    mJavaDetectorEye = null;
                } else
                    Log.i(TAG, "Loaded cascade classifier from "
                            + mCascadeFile.getAbsolutePath());



                cascadeDir.delete();

            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
            }

When running the app I get these two error messages in the logcat:

Failed to load cascade face classifier

Failed to load cascade eye classifier

As can be seen from the code, these are only generated if there is a problem loading the classifiers.

I use Android Studio, and OpenCV for Android V 3.1. I have also tried to download the latest SDK build tools, but that didn't solve the problem. project.

Can anyone please tell me how to solve this problem?

Thanks.

Dania
  • 1,648
  • 4
  • 31
  • 57

2 Answers2

3

This question has been answered here

It's a bug in OpenCV v3.1, all you need to do is modify your code as follows;

     mJavaDetector = new CascadeClassifier(
                            mCascadeFile.getAbsolutePath());
     mJavaDetector.load(mCascadeFile.getAbsolutePath());

and

      mJavaDetectorEye = new CascadeClassifier(
                    cascadeFileER.getAbsolutePath());
      mJavaDetectorEye.load(cascadeFileER.getAbsolutePath());
Community
  • 1
  • 1
Ifeoluwa
  • 114
  • 8
1

There is a bug in the opencv4android v3.1. If you use intead the 2.4.11 version, it should work. I have not try the 3.0 version, so maybe it works.

Regards