OpenCV on Android 开发(2)实现实时人脸检测

转自我自己的简书https://www.jianshu.com/p/e285b379e48b

我参考网上的教程使用opencv内置的人脸检测模型

前期准备

在Androidstudio项目里新建一个文件夹raw路径如下OpenCV on Android 开发(2)实现实时人脸检测_第1张图片

再将opencvandroidsdk里的lbpcascade_frontalface.xml添加进去 .xml文件的路径在你存放的位置\OpenCV-android-sdk\sdk\etc\lbpcascades文件夹里
准备工作完成,可以开始码代码了

首先修改AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" android:required="false" />
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false" />
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false" />

获取摄像头的权限

其次修改activity_main.xml

添加如下代码

.opencv.android.JavaCameraView
        android:id="@+id/openCvCameraView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        app:camera_id="any"
        app:show_fps="true"
        />

创建一个JavaCameraView控件

最后MainActivity.java

代码如下

package com.example.aaa.opencvcamera;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
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.Core;
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;

import static org.opencv.imgproc.Imgproc.rectangle;


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 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{
            InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
            File casadeDir = getDir("cascade", Context.MODE_PRIVATE);
            File mCascadeFile = new File(casadeDir,"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();

            cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());

        } catch (Exception e) {
            Log.e("OpenCVActivity","Error Loading casade",e);
        }

        openCvCameraView.enableView();
    }

    @Override
    protected 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);

        /*setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        */
   }

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

        absoluteFaceSize = (int)(height * 0.2);

    }

    @Override
    public void onCameraViewStopped(){

    }

    @Override
    public Mat onCameraFrame(Mat aInputFrame){
        Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB);

        MatOfRect faces = new MatOfRect();

        if (cascadeClassifier != null){
            cascadeClassifier.detectMultiScale(grayscaleImage,faces,1.1,2,2,new Size(absoluteFaceSize,absoluteFaceSize),new Size());
        }

        Rect[] faceArray = faces.toArray();
        for (int i = 0; i //原代码是Core.rectangle(aInputFrame, faceArray[i].tl(), faceArray[i].br(), new Scalar(0, 255, 0, 255), 3);
            //但好像不行

            Imgproc.rectangle(aInputFrame, faceArray[i].tl(), faceArray[i].br(), new Scalar(0, 255, 0, 255), 3);

        return aInputFrame;
    }

    /*
    @Override
    public void onResume(){
        super.onResume();

        if(!OpenCVLoader.initDebug()){
            Log.e("log_wons", "OpenCV init error");

        }

        initializeOpenCVDependencies();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, mLoaderCallback);

    }
    */

    @Override
    public void onResume(){
        super.onResume();
        if (!OpenCVLoader.initDebug()){
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, mLoaderCallback);

        } else {
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }


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

最后我把生成的apk安装在手机里运行效果如下
OpenCV on Android 开发(2)实现实时人脸检测_第2张图片
ok了还是可以的,但是鲁棒性好像不行,误检概率有点大。
注释和每个函数的意思有空再补

你可能感兴趣的:(学习,毕设过程,毕设学习)