public boolean detectFaces(Bitmap image) {
Mat mat = new Mat();
Utils.bitmapToMat(image, mat);
InputStream inputStream = getResources().openRawResource(R.raw.haarcascade_frontalface_default);
CascadeClassifier faceDetector = new CascadeClassifier();
String cascadeFilePath = getCacheDir() + "/haarcascade_frontalface_default.xml";
try (FileOutputStream fos = new FileOutputStream(cascadeFilePath)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
Log.e("GONG6", "Error loading cascade file", e);
return false;
}
// 检查文件是否存在
File cascadeFile = new File(cascadeFilePath);
if (!cascadeFile.exists()) {
Log.e("GONG6", "Cascade file does not exist: " + cascadeFilePath);
return false;
}
// 加载分类器
if (!faceDetector.load(cascadeFilePath)) {
Log.e("GONG6", "分类器加载成功");
return false;
} else {
Log.i("GONG6", "分类器加载失败");
}
MatOfRect faces = new MatOfRect();
faceDetector.detectMultiScale(mat, faces);
if (faces.toArray().length > 0) {
Log.i("GONG6", "检测到人脸");
return true;
}
Log.i("GONG6", "未检测到人脸");
return false;
}
能看看这分类器加载失败吗