android opencv人脸检测程序

原创 2015年11月19日 13:39:47
今天用Android 纯Java opencv测试了一下人脸检测速度,首先搭建opencvAndroid开发环境见


http://jingyan.baidu.com/article/948f592448183bd80ff5f9d1.html

搭好环境开始简单编程,尽管opencv在Android上检测速度确实有待提高还是贴上源码(本人于小米手机测试OK  三星手机貌似有点问题)

MainActivity.java

package com.example.facedec;

import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import org.opencv.android.BaseLoaderCallback; 
import org.opencv.android.LoaderCallbackInterface; 
import org.opencv.android.OpenCVLoader; 
import org.opencv.android.Utils; 
import org.opencv.core.Core;
import org.opencv.core.Mat; 
import org.opencv.core.MatOfRect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc; 
import org.opencv.objdetect.CascadeClassifier;
 
import android.os.Bundle; 
import android.provider.MediaStore.Images.ImageColumns;
import android.app.Activity; 
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText;
import android.widget.ImageView; 
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

 final private static String TAG = "StaticrecognitionActivity"; 
     final private int PICTURE_CHOOSE = 1; 
  
     private ImageView imageView = null; 
     private Bitmap img = null; 
     private Button buttonDetect = null; 
     private TextView textView = null; 
     private EditText editText = null; 
  
     // OpenCV类库加载并初始化成功后的回调函数,在此我们不进行任何操作 
     private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) { 
         @Override 
         public void onManagerConnected(int status) { 
             switch (status) { 
             case LoaderCallbackInterface.SUCCESS: { 
             } 
                 break; 
             default: { 
                 super.onManagerConnected(status); 
             } 
                 break; 
             } 
         } 
     }; 

 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button button = (Button) this.findViewById(R.id.button1); 
  button.setOnClickListener(new OnClickListener() { 
   
              public void onClick(View arg0) { 
                  // get a picture form your phone 
                  Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
                  photoPickerIntent.setType("image/*"); 
                  startActivityForResult(photoPickerIntent, PICTURE_CHOOSE); 
              } 
          }); 
   
          //editText = (EditText) this.findViewById(R.id.editText1); 
          textView = (TextView) this.findViewById(R.id.textView1); 
   
          buttonDetect = (Button) this.findViewById(R.id.button2); 
        //  buttonDetect.setVisibility(View.INVISIBLE); 
          buttonDetect.setOnClickListener(new OnClickListener() { 
              public void onClick(View arg0) { 
   
                  textView.setText("Waiting ...");  
   
                  String xmlfilePath = "//sdcard//haarcascade_frontalface_alt2.xml"; 
   
                  CascadeClassifier faceDetector = new CascadeClassifier( 
                          xmlfilePath); 
   
                  // Bitmap bmptest = BitmapFactory.decodeResource(getResources(), 
                  // R.drawable.lena); 
                  Mat testMat = new Mat(); 
                  Utils.bitmapToMat(img, testMat); 
   
                  // Detect faces in the image. 
                  // MatOfRect is a special container class for Rect. 
                  MatOfRect faceDetections = new MatOfRect(); 
                  faceDetector.detectMultiScale(testMat, faceDetections); 
   
                  Log.i(String.format("Detected %s faces", 
                          faceDetections.toArray().length), ""); 
   
                  int facenum = 0; 
                  // Draw a bounding box around each face. 
                  for (org.opencv.core.Rect rect : faceDetections.toArray()) { 
                   
                      Core.rectangle( 
                              testMat, 
                              new org.opencv.core.Point(rect.x, rect.y), 
                              new org.opencv.core.Point(rect.x + rect.width, rect.y + rect.height), 
                              new Scalar(255, 0, 0));
                     
                      ++facenum; 
                  } 
   
                  // Save the visualized detection. 
                  // Bitmap bmpdone = Bitmap.createBitmap(bmptest.getWidth(), 
                  // bmptest.getHeight(), Config.RGB_565); 
                  Utils.matToBitmap(testMat, img); 
                  imageView.setImageBitmap(img); 
                  textView.setText("Facecount:" + facenum);  
              } 
          }); 
   
          imageView = (ImageView) this.findViewById(R.id.image_view); 
          imageView.setImageBitmap(img);

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
 
  @Override 
  protected void onActivityResult(int requestCode, int resultCode, 
              Intent intent) { 
          super.onActivityResult(requestCode, resultCode, intent); 
   
          // the image picker callback 
          if (requestCode == PICTURE_CHOOSE) { 
              if (intent != null) { 
   
                  Cursor cursor = getContentResolver().query(intent.getData(), null, null, null, null); 
                  cursor.moveToFirst(); 
                  int idx = cursor.getColumnIndex(ImageColumns.DATA); 
                  String fileSrc = cursor.getString(idx); 
   
                  Options options = new Options(); 
                  options.inJustDecodeBounds = true; 
                 img = BitmapFactory.decodeFile(fileSrc, options); 
   
                  options.inSampleSize = Math.max(1, (int) Math.ceil(Math.max( 
                          (double) options.outWidth / 1024f, 
                          (double) options.outHeight / 1024f))); 
                  options.inJustDecodeBounds = false; 
                  img = BitmapFactory.decodeFile(fileSrc, options); 
                  textView.setText("Clik Detect. ==>"); 
   
                  imageView.setImageBitmap(img); 
                  buttonDetect.setVisibility(View.VISIBLE); 
              } else { 
                  Log.d(TAG, "idButSelPic Photopicker canceled"); 
              } 
         } 
      } 
   
      @Override 
      public void onResume() { 
          super.onResume(); 
          // 通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 
          // OpenCV_2.4.10_Manager_2.4_*.apk程序包,存在于OpenCV安装包的apk目录中 
          OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_10, this, 
                  mLoaderCallback); 
      } 

}

整体项目源码见


相关文章推荐

android下的openCV人脸检测使用例程

  • 2017年11月16日 13:39
  • 4.17MB
  • 下载

Python和OpenCV简单的人脸检测程序

最近想要搞人脸识别的东西,所以先弄了个简单的人脸检测的程序,网上找的许多都无法使用,不知道是不是Opencv版本的关系,或者是和Python结合后产生的问题,感觉很多API都是错的,这里用的是open...
  • zawdd
  • zawdd
  • 2012年09月26日 17:14
  • 1586

人脸检测之基于Opencv和MFC创作的换脸小程序

人脸检测之基于Opencv和MFC创作的换脸小程序 1.   背景 Opencv已经接触了有段日子了,无论是Python还是C++,都在控制台内运行,使用Opencv的imshow观察结果。最近在...

基于opencv3人脸检测程序(c++)

  • 2017年06月12日 16:37
  • 1.36MB
  • 下载

基于openCV的图片人脸检测程序

  • 2012年05月16日 01:35
  • 4KB
  • 下载

opencv程序十二:人脸检测

本篇介绍图像处理与模式识别中最热门的一个领域——人脸检测(人脸识别)。人脸检测可以说是学术界的宠儿,在不少EI,SCI高级别论文都能看到它的身影。甚至很多高校学生的毕业设计都会涉及到人脸检测。当然人脸...

opencv人脸检测程序源代码

  • 2012年12月13日 11:23
  • 850KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android opencv人脸检测程序
举报原因:
原因补充:

(最多只允许输入30个字)