Android+OpenCV获取摄像头并人脸检测

一、Android+OpenCV环境配置

      参考https://blog.csdn.net/KayChanGEEK/article/details/86493632

      我采用的是Android Studio 3.5.2版本,opencv-3.4.10-android-sdk,

      

     build.gradle(Module:openCVLibrary3410)为:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 29
    buildToolsVersion "28.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 29
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

 

 build.gradle(Module:app)为:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation project(path: ':openCVLibrary3410')
}

 

 

    Android+OpenCV获取摄像头并人脸检测_第1张图片

 

Android+OpenCV获取摄像头并人脸检测_第2张图片

 

二、CameraBridgeViewBase获取摄像头,并OpenCV逐像素处理

        参考https://blog.csdn.net/linshuhe1/article/details/51208745?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase

 

三、OpenCV人脸检测

       参考https://www.jianshu.com/p/1fc91f9c9a67

 

四、整合并运行通过主要的代码

 MainActivity.java文件:

package com.example.myapplication;

import java.io.File;
import java.lang.System;
import android.Manifest;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.util.Log;
import java.io.InputStream;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Size;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Arrays;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import org.opencv.android.CameraBridgeViewBase;
import org.opencv.core.Core;
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;


//https://blog.csdn.net/linshuhe1/article/details/51208745?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-7.nonecase
public class MainActivity extends Activity implements CvCameraViewListener2{
    private String TAG = "OpenCV_Test";
    //OpenCV的相机接口
    private CameraBridgeViewBase cameraView;

    //缓存相机每帧输入的数据
    private Mat mRgba,mTmp,mRgb;

    //按钮组件
    private Button mButtonSwitchCamera;

    private CascadeClassifier classifier;
    private int mAbsoluteFaceSize = 0;
    private boolean isFrontCamera = false;

     //手动装载openCV库文件,以保证手机无需安装OpenCV Manager
    //必须有,否则initClassifier()报错;
    static {System.loadLibrary("opencv_java3");}


    /**
     * 通过OpenCV管理Android服务,异步初始化OpenCV
     */
    BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
    {
        @Override
        public void onManagerConnected(int status){
            switch (status)
            {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG,"OpenCV loaded successfully");
                    cameraView.enableView();
                    break;
                default:
                    break;
            }
        }
    };

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

        initClassifier();    //必须先static {System.loadLibrary("opencv_java3");}
        initWindowSettings();

        cameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view);

        isFrontCamera = false;
        cameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);

        cameraView.enableFpsMeter();   //显示帧率
        cameraView.setMaxFrameSize(1280,720);
        cameraView.setCvCameraViewListener(this);

        mButtonSwitchCamera = (Button) findViewById(R.id.switch_camera);
        mButtonSwitchCamera.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if(R.id.switch_camera == v.getId()){
                    cameraView.disableView();

                    if (isFrontCamera) {
                        cameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);
                        isFrontCamera = false;
                    } else {
                        cameraView.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
                        isFrontCamera = true;
                    }

                    cameraView.enableView();
                }
            }
        });
    }


    @Override
    public void onResume()
    {
        super.onResume();
        if (!OpenCVLoader.initDebug())
        {
            Log.d(TAG,"OpenCV library not found!");
        }
        else {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    };

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if(cameraView!=null)
        {
            cameraView.disableView();
        }
    };

    //处理相机的开始
    @Override
    public void onCameraViewStarted(int width, int height)
    {
        // TODO Auto-generated method stub
        mRgba = new Mat(height, width, CvType.CV_8UC4);
        mRgb  = new Mat(height, width, CvType.CV_8UC3);
        mTmp = new Mat(height, width, CvType.CV_8UC4);
    }

    //处理相机的停止
    @Override
    public void onCameraViewStopped()
    {
        // TODO Auto-generated method stub
        mRgba.release();
        mTmp.release();
    }

    /**
     * 图像处理都写在此处,可以处理摄像机拍摄的每一帧图像
     */
    @Override
    public Mat onCameraFrame(CvCameraViewFrame inputFrame)
    {
        //mRgba = inputFrame.rgba();

        //灰化处理
        // Imgproc.cvtColor(inputFrame.gray(), mRgba, Imgproc.COLOR_GRAY2RGBA,4);

        // Canny边缘检测
        //Imgproc.Canny(inputFrame.gray(), mTmp, 80, 100);
        //Imgproc.cvtColor(mTmp, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);

//        Imgproc.cvtColor(mRgba,mRgb, Imgproc.COLOR_RGBA2RGB);
//
//        int pv = 0;
//        int channels = mRgb.channels();
//        int width = mRgb.cols();
//        int height = mRgb.rows();
//        byte[] data = new byte[channels*width*height];
//        mRgb.get(0,0,data);
//        for(int i=0;i 0)
            {
                mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
            }
        }

        MatOfRect faces = new MatOfRect();
        if (classifier != null)
            classifier.detectMultiScale(_mGray, faces, 1.1, 2, 2,
                    new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
        Rect[] facesArray = faces.toArray();
        Scalar faceRectColor = new Scalar(0, 255, 0, 255);
        for (Rect faceRect : facesArray)
            Imgproc.rectangle(_mRgba, faceRect.tl(), faceRect.br(), faceRectColor, 3);

        return _mRgba;
    }

    // 初始化窗口设置, 包括全屏、横屏、常亮
    private void initWindowSettings() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    // 初始化人脸级联分类器,必须先初始化
    private void initClassifier() {
        try {
            InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
            File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
            File cascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
            FileOutputStream os = new FileOutputStream(cascadeFile);

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            is.close();
            os.close();

            classifier = new CascadeClassifier(cascadeFile.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


 

activity_main.xml文件:








    

 

AndroidManifest.xml




    
    
    

    
    

    
    

    


    
        
            
                

                
            
        
    

 

 

 

 

你可能感兴趣的:(Android图像处理,android,opencv,人脸检测)