Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV for Android: failed to load cascade classifier error

This is the first time I use openCV library. I want to use it to detect the eyes. I have used the FdActivity code available in this tutorial:

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

The tutorial uses OpenCV 2.4.6, but I have downloaded version 3.1 in my project. Due to the version differences I have changed the lines that uses putText, rectangle, and circle to be imported from imgproc instead of Core. This is all what I've changed. I have added haarcascade_lefteye_2splits.xml and lbpcascade_frontalface.xml to the raw folder under res folder.

When running the app I get this error in the logcat:

failed to load cascade classifier 

Which is only generated from these lines in if mJavaDetector or mJavaDetectorEye is empty:

 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()) {
                            Log.e(TAG, "Failed to load cascade classifier");
                            mJavaDetector = null;
                        } else
                            Log.i(TAG, "Loaded cascade classifier from "
                                    + mCascadeFile.getAbsolutePath());

                        mJavaDetectorEye = new CascadeClassifier(
                                cascadeFileER.getAbsolutePath());
                        if (mJavaDetectorEye.empty()) {
                            Log.e(TAG, "Failed to load cascade classifier");
                            mJavaDetectorEye = null;
                        } else
                            Log.i(TAG, "Loaded cascade classifier from "
                                    + mCascadeFile.getAbsolutePath());



                        cascadeDir.delete();

                    } 

I guess the path to "haarcascade_eye_right.xml" is not correct, or the xml file doesn't exist, is this what is causing the error?

If yes, how can I have the xml file, and where exactly shall I store it? If no, what is causing the problem?

Note: I use Android Studio.

I'd appreciate any help in this regard, I've been trying for a while, but I couldn't solve it.

like image 932
Dania Avatar asked Jan 22 '16 18:01

Dania


1 Answers

I got it. Although I have no idea about WHY. ...

mJavaDetector = new CascadeClassifier( mCascadeFile.getAbsolutePath() );
//must add this line
mJavaDetector.load( mCascadeFile.getAbsolutePath() );

...

it works for me.

like image 56
William Liu Avatar answered Nov 05 '22 23:11

William Liu