【问题标题】:OpenCV for Android: failed to load cascade classifier errorOpenCV for Android:无法加载级联分类器错误
【发布时间】:2016-04-29 11:11:38
【问题描述】:

这是我第一次使用 openCV 库。我想用它来检测眼睛。我使用了本教程中提供的FdActivity 代码:

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

本教程使用 OpenCV 2.4.6,但我在我的项目中下载了 3.1 版本。由于版本差异,我将使用 putText、矩形和圆形的行更改为从 imgproc 而不是 Core 导入。这就是我所改变的一切。我已将 haarcascade_lefteye_2splits.xml 和 lbpcascade_frontalface.xml 添加到 res 文件夹下的 raw 文件夹中。

运行应用程序时,我在 logcat 中收到此错误:

failed to load cascade classifier 

仅在 mJavaDetector 或 mJavaDetectorEye 为空时从这些行生成:

 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();

                    } 

我猜“haarcascade_eye_right.xml”的路径不正确,或者xml文件不存在,这是导致错误的原因吗?

如果是,我怎样才能拥有 xml 文件,我应该将它存储在哪里?如果不是,是什么原因造成的?

注意:我使用的是 Android Studio。

在这方面我很感激任何帮助,我已经尝试了一段时间,但我无法解决它。

【问题讨论】:

    标签: android xml opencv haar-classifier eye-detection


    【解决方案1】:

    我明白了。虽然我不知道为什么。 ...

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

    ...

    它对我有用。

    【讨论】:

    • 是的,这绝对是一个错误,指定路径应该已经调用 load()。感谢您为我节省了时间!
    • omg,我非常感谢@william Liu……与 github 中的示例和所有示例相比,我花了好几个小时,最后它是一个 opencv 错误……谢谢你威廉。 :-)
    • 首先他们在 3 版本中削减了 hog 级联,它是如此的错误
    • 但对我来说它仍然是空的
    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 2011-12-30
    • 2012-07-05
    • 2017-11-21
    • 2015-12-16
    • 1970-01-01
    • 2015-04-21
    相关资源
    最近更新 更多