【android+opencv人脸检测】

最近在做人脸识别,现在初步只做了人脸检测,比较简单,仅供参考。


功能是打开图片,进行人脸检测,输出人脸个数和检测范围。

.......省略包

public class Staticdetection2Activity extends Activity {

	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.activity_staticdetection2);
		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 ...");

				// use the red paint
				/*
				 * Paint paint = new Paint(); paint.setColor(Color.RED);
				 * paint.setStrokeWidth(Math.max(img.getWidth(),
				 * img.getHeight()) / 100f);
				 * 
				 * // create a new canvas Bitmap bitmap =
				 * Bitmap.createBitmap(img.getWidth(), img.getHeight(),
				 * img.getConfig()); Canvas canvas = new Canvas(bitmap);
				 * canvas.drawBitmap(img, new Matrix(), null);
				 * 
				 * float x, y, w, h;
				 * 
				 * x = y = w = h = 100;
				 * 
				 * // change percent value to the real size x = x / 100 *
				 * img.getWidth(); w = w / 100 * img.getWidth() * 0.7f; y = y /
				 * 100 * img.getHeight(); h = h / 100 * img.getHeight() * 0.7f;
				 * 
				 * // draw the box to mark it out canvas.drawLine(x - w, y - h,
				 * x - w, y + h, paint); canvas.drawLine(x - w, y - h, x + w, y
				 * - h, paint); canvas.drawLine(x + w, y + h, x - w, y + h,
				 * paint); canvas.drawLine(x + w, y + h, x + w, y - h, paint);
				 * 
				 * img = bitmap;
				 */

				String xmlfilePath = "/sdcard/FaceDetect/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 (Rect rect : faceDetections.toArray()) {
					Core.rectangle(
							testMat,
							new Point(rect.x, rect.y),
							new 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);

				/*Staticdetection2Activity.this.runOnUiThread(new Runnable() {

					public void run() {
						// show the image
						imageView.setImageBitmap(img);
						// textView.setText("Finished, "+ count + " faces.");
						textView.setText("Finished, " + " faces.");
						// set edit text
						// editText.setText(str);
					}
				});*/
			}
		});

		imageView = (ImageView) this.findViewById(R.id.imageView1);
		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.staticdetection2, menu);
		return true;
	}

	@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.3.2_Manager_2.4_*.apk程序包,存在于OpenCV安装包的apk目录中
		OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,
				mLoaderCallback);
	}
}

【android+opencv人脸检测】_第1张图片

【android+opencv人脸检测】_第2张图片



你可能感兴趣的:(【android】)