7

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.

1 Answer 1

17

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.

Sign up to request clarification or add additional context in comments.

4 Comments

yeah that's definitely a bug, specifying a path should already call load(). thanks for saving me hours!
omg, i cannot thank you enough @william Liu ... spending hours and hours on this, compared with the examples in github and all, and finally its a opencv bug.... thankyou william. :-)
first they cut hog cascade in 3 ver and it's so buggy
but for me it still remains empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.