在 android 中使用 openCV 进行实时人脸检测

Real time face detection using openCV in android

我正在尝试使用 android 中的摄像头检测人脸。 我正在使用 openCV3.1 lib 进行人脸检测。我找到了一篇关于这个主题的文章 HERE,我尝试实现相同的但我不知道为什么我的代码不起作用,虽然它没有显示任何错误。下面是我的代码。

public class OpenCVActivity extends Activity
    implements CvCameraViewListener {

private CameraBridgeViewBase openCvCameraView;
private CascadeClassifier cascadeClassifier;
private Mat grayscaleImage;
private int absoluteFaceSize;

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
                initializeOpenCVDependencies();
                break;
            default:
                super.onManagerConnected(status);
                break;
        }
    }
};

private void initializeOpenCVDependencies() {

    try {
        // Copy the resource into a temp file so OpenCV can load it
        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());
    } catch (Exception e) {
        Log.e("OpenCVActivity", "Error loading cascade", e);
    }

    // And we are ready to go
    openCvCameraView.enableView();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    openCvCameraView = new JavaCameraView(this, -1);
    setContentView(openCvCameraView);
    openCvCameraView.setCvCameraViewListener(this);
}

@Override
public void onCameraViewStarted(int width, int height) {
    grayscaleImage = new Mat(height, width, CvType.CV_8UC4);

    // The faces will be a 20% of the height of the screen
    absoluteFaceSize = (int) (height * 0.2);
}

@Override
public void onCameraViewStopped() {
}

@Override
public Mat onCameraFrame(Mat aInputFrame) {
    // Create a grayscale image
    Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB);

    MatOfRect faces = new MatOfRect();

    // Use the classifier to detect faces
    if (cascadeClassifier != null) {
        cascadeClassifier.detectMultiScale(grayscaleImage, faces, 1.1, 2, 2,
                new Size(absoluteFaceSize, absoluteFaceSize), new Size());
    }

    // If there are any faces found, draw a rectangle around it
    Rect[] facesArray = faces.toArray();
    for (int i = 0; i <facesArray.length; i++) {
        Log.i("face : ","Detected");
        Imgproc.rectangle(aInputFrame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0, 255), 3);
    }

    return aInputFrame;
}

@Override
public void onResume() {
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);
}

有人能告诉我哪里错了吗? 或者,是否有关于使用 openCV 进行人脸检测的任何其他教程?

如有任何帮助,我们将不胜感激!

正如您之前提到的,您正在使用 3.1.1 的 OpenCVLibrary。 但是,您在下面提到的版本适用于 2.4.6

例如: @Override public void onResume() { super.onResume(); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback); }

改为使用,

public void onResume() { super.onResume(); OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback); }