【问题解决】Android使用Javacv加载分类器
来源:互联网 发布:java解析wsdl 编辑:程序博客网 时间:2024/06/05 15:32
在Android中使用Javacv做人脸检测时,在Javacv(我用的是1.2版本)的samples里有这么一段加载分类器的代码:
public FaceView(FacePreview context) throws IOException { super(context); // Load the classifier file from Java resources. File classifierFile = Loader.extractResource(getClass(), "/org/bytedeco/javacv/facepreview/haarcascade_frontalface_alt.xml", context.getCacheDir(), "classifier", ".xml"); if (classifierFile == null || classifierFile.length() <= 0) { throw new IOException("Could not extract the classifier file from Java resource."); } // Preload the opencv_objdetect module to work around a known bug. Loader.load(opencv_objdetect.class); classifier = new CvHaarClassifierCascade(cvLoad(classifierFile.getAbsolutePath())); classifierFile.delete(); if (classifier.isNull()) { throw new IOException("Could not load the classifier file."); } storage = CvMemStorage.create(); }
在自己的项目中使用时遇到了几个需要注意的问题:
1. 分类器文件的存放位置
在assets目录下新创建classifier文件夹,将haarcascade_frontalface_alt.xml放到此目录下,那么获取分类器文件路径的代码如下:
File classifierFile = Loader.extractResource(context.getClass(), "/assets/classifier/haarcascade_frontalface_alt.xml", context.getCacheDir(), "classifier", ".xml");if (classifierFile == null || classifierFile.length() <= 0) { throw new IOException("Could not extract the classifier file from Java resource.");}
注:File extractResource(Class cls, String name, File directory, String prefix, String suffix)函数参数的意义:
- cls:加载分类器的类
- name:分类器文件的路径
- directory:用于临时存放读取的分类器,如果为null,该函数使用默认的路径
- prefix:生成临时文件时所需的前缀
- suffix:生成临时文件时所需的后缀
2. 加载分类器文件出错
运行的时候程序挂在了这一行:
classifier = new CvHaarClassifierCascade(cvLoad(classifierFile.getAbsolutePath()));
也就是加载分类器文件时出错了,报错的信息为:
Unspecified error (The node does not represent a user object (unknown type?)) in function cvRead…
原因是分类器文件对应的opencv版本不一致导致的。
我是按照Javacv在github的changelog.md中所说的Opencv3.0.0来下对应的haarcascade_frontalface_alt.xml的,结果报了上面的错误,无奈试一下Opencv3.1.0的,也是如此,只好查它的issue,结果发现:
https://github.com/bytedeco/javacv/issues/102
按照所说的到
https://raw.githubusercontent.com/Itseez/opencv/2.4.10/data/haarcascades/haarcascade_frontalface_alt.xml
下了一份,就OK了(MD)。
- 【问题解决】Android使用Javacv加载分类器
- android 使用javacv进行录像[模仿vine]
- android 使用javacv进行录像[模仿vine]
- 关于在Android中使用JavaCV的问题
- android+javaCV环境配置
- 【Android】Test JavaCV & OpenCV
- 【Android】JavaCV and OpenCV
- 使用javacv注意点
- javacv使用笔记
- Android加载机器学习分类器问题
- javacv
- javacv
- JavaCV
- JavaCV的配置及使用
- JavaCV的配置及使用
- JavaCV的配置及使用
- CentOS环境下使用javaCV
- 【Android】Android使用OpenCV实现人脸识别(OpenCV+JavaCV)
- linux ubuntu 无法获得锁解决办法
- Chrome的线程和进程模型
- Android 常用工具类
- Gradle for Android系列之二 build.gradle文件
- javase笔记
- 【问题解决】Android使用Javacv加载分类器
- UX,程序猿对你有意见
- Vr射线生成
- 使用nginx获取get/post参数值并返回给客户端
- 关于bootstrap的字体图标问题
- JS检查变量是否存在
- iOS 悬浮按钮
- 浅谈Linux下的五种I/O模型
- 理解D3D—(2)最多混合几层texture