适用于Android的OpenCV:无法加载级联分类器错误 [英] OpenCV for Android: failed to load cascade classifier error

查看:517
本文介绍了适用于Android的OpenCV:无法加载级联分类器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用openCV库.我想用它来检测眼睛.我已经使用了本教程中可用的FdActivity代码:

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/

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

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.

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

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

failed to load cascade classifier 

仅当mJavaDetector或mJavaDetectorEye为空时才从这些行生成以下内容:

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

                    } 

我猜到"haarcascade_eye_right.xml"的路径不正确,或者xml文件不存在,这是引起错误的原因吗?

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?

如果是,我如何拥有xml文件,并将其确切存储在哪里?如果不是,那么是什么原因引起的?

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

注意:我使用的是Android Studio.

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.

推荐答案

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

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

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

...

对我有用.

这篇关于适用于Android的OpenCV:无法加载级联分类器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆