如何通过opencv实时检测人脸?

时间:2018-07-13 19:43:56

标签: android opencv

这是代码,但相机在脸上未显示任何正方形(代码中没有错误) 其他一切正常,应用程序正在打开相机也在打开。

我在网上进行了大量搜索,但是我无法解决,因此请帮助它。........

package com.phoenixer.nao.opencv2;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener {

// Used to load the 'native-lib' library on application startup.
static {
    System.loadLibrary("native-lib");
}


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

private BaseLoaderCallback baseLoaderCallback = 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.activity_opencv_camera2);
        File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
        File mCascadeFile = new File(cascadeDir, "activity_opencv_camera2.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
    cameraBridgeViewBase.enableView();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);




    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    cameraBridgeViewBase = new JavaCameraView(this, -1);
    setContentView(cameraBridgeViewBase);
    cameraBridgeViewBase.setCvCameraViewListener(this);


}

/**
 * A native method that is implemented by the 'native-lib' native library,
 * which is packaged with this application.
 */
public native String stringFromJNI();


@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 inputFrame) {
    // Create a grayscale image
    Imgproc.cvtColor(inputFrame, 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++)
        Imgproc.rectangle(inputFrame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0, 255), 6);


    return inputFrame;
}


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

}

0 个答案:

没有答案