我正在尝试使用android中的OpenCV检测图像中的人脸。
这是scanFaces
函数,它正在使用this分类器。
由于我是新手,所以不确定是否以正确的方式加载分类器。
private void scanFaces(Uri imageUri) throws Exception {
Bitmap bitmap = decodeBitmapUri(this, imageUri);
Mat Rgba = new Mat();
Mat grayMat= new Mat();
Utils.bitmapToMat(bitmap, Rgba);
Imgproc.cvtColor(Rgba, grayMat, Imgproc.COLOR_RGBA2GRAY);
MatOfRect faces = new MatOfRect();
File mCascadeFile = new File("file:///assets/classifier.xml");
try{
cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
}
catch(Exception e){
Log.d("cascadeClassifier: ",""+e);
}
grayBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.RGB_565);
if (cascadeClassifier != null) {
cascadeClassifier.detectMultiScale(grayMat, faces, 1.1, 2, 2,
new Size(absoluteFaceSize, absoluteFaceSize), new Size());
}
Rect[] facesArray = faces.toArray();
for (int i = 0; i <facesArray.length; i++)
Core.rectangle(grayMat, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0, 255), 3);
try{
Utils.matToBitmap(grayMat,grayBitmap);
}
catch(Exception e){
Log.d("","");
}
imageView.setImageBitmap(grayBitmap);
}
我关注的教程以这种方式访问了分类器:
InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File 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 the cascade classifier
cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
如果您想看一下教程,这里是link
-谢谢