Added face datection Android sample
This commit is contained in:
8
samples/android/face-detection/.classpath
Normal file
8
samples/android/face-detection/.classpath
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="src" path="gen"/>
|
||||||
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
|
<classpathentry kind="src" path="OpenCV-2.3.1_src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
40
samples/android/face-detection/.project
Normal file
40
samples/android/face-detection/.project
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Sample - face-detection</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
<linkedResources>
|
||||||
|
<link>
|
||||||
|
<name>OpenCV-2.3.1_src</name>
|
||||||
|
<type>2</type>
|
||||||
|
<locationURI>_android_OpenCV_2_3_1_df28900a/src</locationURI>
|
||||||
|
</link>
|
||||||
|
</linkedResources>
|
||||||
|
</projectDescription>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#Wed Jun 29 04:36:40 MSD 2011
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.5
|
||||||
31
samples/android/face-detection/AndroidManifest.xml
Normal file
31
samples/android/face-detection/AndroidManifest.xml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="org.opencv.samples.fd"
|
||||||
|
android:versionCode="1"
|
||||||
|
android:versionName="1.0">
|
||||||
|
|
||||||
|
<supports-screens android:resizeable="true"
|
||||||
|
android:smallScreens="true"
|
||||||
|
android:normalScreens="true"
|
||||||
|
android:largeScreens="true"
|
||||||
|
android:anyDensity="true" />
|
||||||
|
|
||||||
|
<application android:label="@string/app_name" android:icon="@drawable/icon">
|
||||||
|
<activity android:name="FdActivity"
|
||||||
|
android:label="@string/app_name"
|
||||||
|
android:screenOrientation="landscape"
|
||||||
|
android:configChanges="keyboardHidden|orientation">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
</application>
|
||||||
|
|
||||||
|
<uses-sdk android:minSdkVersion="8" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.CAMERA"/>
|
||||||
|
<uses-feature android:name="android.hardware.camera" />
|
||||||
|
<uses-feature android:name="android.hardware.camera.autofocus" />
|
||||||
|
|
||||||
|
</manifest>
|
||||||
3
samples/android/face-detection/default.properties
Normal file
3
samples/android/face-detection/default.properties
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
android.library.reference.1=../../OpenCV-2.3.1
|
||||||
|
# Project target.
|
||||||
|
target=android-8
|
||||||
BIN
samples/android/face-detection/res/drawable/icon.png
Normal file
BIN
samples/android/face-detection/res/drawable/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.6 KiB |
1505
samples/android/face-detection/res/raw/lbpcascade_frontalface.xml
Normal file
1505
samples/android/face-detection/res/raw/lbpcascade_frontalface.xml
Normal file
File diff suppressed because it is too large
Load Diff
4
samples/android/face-detection/res/values/strings.xml
Normal file
4
samples/android/face-detection/res/values/strings.xml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<string name="app_name">OpenCV Sample - face-detection</string>
|
||||||
|
</resources>
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package org.opencv.samples.fd;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.Window;
|
||||||
|
|
||||||
|
public class FdActivity extends Activity {
|
||||||
|
private static final String TAG = "Sample::Activity";
|
||||||
|
|
||||||
|
private MenuItem mItemFace50;
|
||||||
|
private MenuItem mItemFace40;
|
||||||
|
private MenuItem mItemFace30;
|
||||||
|
private MenuItem mItemFace20;
|
||||||
|
|
||||||
|
public static float minFaceSize = 0.5f;
|
||||||
|
|
||||||
|
public FdActivity() {
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Called when the activity is first created. */
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
Log.i(TAG, "onCreate");
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
setContentView(new FdView(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
Log.i(TAG, "onCreateOptionsMenu");
|
||||||
|
mItemFace50 = menu.add("Face size 50%");
|
||||||
|
mItemFace40 = menu.add("Face size 40%");
|
||||||
|
mItemFace30 = menu.add("Face size 30%");
|
||||||
|
mItemFace20 = menu.add("Face size 20%");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
Log.i(TAG, "Menu Item selected " + item);
|
||||||
|
if (item == mItemFace50)
|
||||||
|
minFaceSize = 0.5f;
|
||||||
|
else if (item == mItemFace40)
|
||||||
|
minFaceSize = 0.4f;
|
||||||
|
else if (item == mItemFace30)
|
||||||
|
minFaceSize = 0.3f;
|
||||||
|
else if (item == mItemFace20)
|
||||||
|
minFaceSize = 0.2f;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
package org.opencv.samples.fd;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.opencv.*;
|
||||||
|
import org.opencv.objdetect.CascadeClassifier;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
|
class FdView extends SampleCvViewBase {
|
||||||
|
private static final String TAG = "Sample::FdView";
|
||||||
|
private Mat mRgba;
|
||||||
|
private Mat mGray;
|
||||||
|
|
||||||
|
private CascadeClassifier mCascade;
|
||||||
|
|
||||||
|
public FdView(Context context) {
|
||||||
|
super(context);
|
||||||
|
|
||||||
|
try {
|
||||||
|
InputStream is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface);
|
||||||
|
File cascadeDir = context.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();
|
||||||
|
|
||||||
|
mCascade = new CascadeClassifier(cascadeFile.getAbsolutePath());
|
||||||
|
if (mCascade.empty()) {
|
||||||
|
Log.e(TAG, "Failed to load cascade classifier");
|
||||||
|
mCascade = null;
|
||||||
|
} else
|
||||||
|
Log.i(TAG, "Loaded cascade classifier from " + cascadeFile.getAbsolutePath());
|
||||||
|
|
||||||
|
cascadeFile.delete();
|
||||||
|
cascadeDir.delete();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
super.surfaceChanged(_holder, format, width, height);
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
// initialize Mats before usage
|
||||||
|
mGray = new Mat();
|
||||||
|
mRgba = new Mat();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Bitmap processFrame(VideoCapture capture) {
|
||||||
|
capture.retrieve(mRgba, highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
|
||||||
|
capture.retrieve(mGray, highgui.CV_CAP_ANDROID_GREY_FRAME);
|
||||||
|
|
||||||
|
if (mCascade != null) {
|
||||||
|
int height = mGray.rows();
|
||||||
|
int faceSize = Math.round(height * FdActivity.minFaceSize);
|
||||||
|
List<Rect> faces = new LinkedList<Rect>();
|
||||||
|
mCascade.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE
|
||||||
|
, new Size(faceSize, faceSize));
|
||||||
|
|
||||||
|
for (Rect r : faces)
|
||||||
|
core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
|
||||||
|
|
||||||
|
if (android.MatToBitmap(mRgba, bmp))
|
||||||
|
return bmp;
|
||||||
|
|
||||||
|
bmp.recycle();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
super.run();
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
// Explicitly deallocate Mats
|
||||||
|
if (mRgba != null)
|
||||||
|
mRgba.dispose();
|
||||||
|
if (mGray != null)
|
||||||
|
mGray.dispose();
|
||||||
|
|
||||||
|
mRgba = null;
|
||||||
|
mGray = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package org.opencv.samples.fd;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.opencv.*;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
|
public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
|
private static final String TAG = "Sample::SurfaceView";
|
||||||
|
|
||||||
|
private SurfaceHolder mHolder;
|
||||||
|
private VideoCapture mCamera;
|
||||||
|
|
||||||
|
public SampleCvViewBase(Context context) {
|
||||||
|
super(context);
|
||||||
|
mHolder = getHolder();
|
||||||
|
mHolder.addCallback(this);
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
Log.i(TAG, "surfaceCreated");
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCamera != null && mCamera.isOpened()) {
|
||||||
|
Log.i(TAG, "before mCamera.getSupportedPreviewSizes()");
|
||||||
|
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||||
|
Log.i(TAG, "after mCamera.getSupportedPreviewSizes()");
|
||||||
|
int mFrameWidth = width;
|
||||||
|
int mFrameHeight = height;
|
||||||
|
|
||||||
|
// selecting optimal camera preview size
|
||||||
|
{
|
||||||
|
double minDiff = Double.MAX_VALUE;
|
||||||
|
for (Size size : sizes) {
|
||||||
|
if (Math.abs(size.height - height) < minDiff) {
|
||||||
|
mFrameWidth = (int) size.width;
|
||||||
|
mFrameHeight = (int) size.height;
|
||||||
|
minDiff = Math.abs(size.height - height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mCamera.set(highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
|
||||||
|
mCamera.set(highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
Log.i(TAG, "surfaceCreated");
|
||||||
|
mCamera = new VideoCapture(highgui.CV_CAP_ANDROID);
|
||||||
|
if (mCamera.isOpened()) {
|
||||||
|
(new Thread(this)).start();
|
||||||
|
} else {
|
||||||
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
Log.e(TAG, "Failed to open native camera");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
|
if (mCamera != null) {
|
||||||
|
synchronized (this) {
|
||||||
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
Log.i(TAG, "Starting processing thread");
|
||||||
|
while (true) {
|
||||||
|
Bitmap bmp = null;
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
|
if (mCamera == null)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (!mCamera.grab()) {
|
||||||
|
Log.e(TAG, "mCamera.grab() failed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
bmp = processFrame(mCamera);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bmp != null) {
|
||||||
|
Canvas canvas = mHolder.lockCanvas();
|
||||||
|
if (canvas != null) {
|
||||||
|
canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null);
|
||||||
|
mHolder.unlockCanvasAndPost(canvas);
|
||||||
|
}
|
||||||
|
bmp.recycle();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.i(TAG, "Finishing processing thread");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<string name="app_name">OpenCV sample - image-manipulations</string>
|
<string name="app_name">OpenCV Sample - image-manipulations</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -139,6 +139,16 @@ class ImageManipulationsView extends SampleCvViewBase {
|
|||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// Explicitly deallocate Mats
|
// Explicitly deallocate Mats
|
||||||
|
if (mZoomWindow != null)
|
||||||
|
mZoomWindow.dispose();
|
||||||
|
if (mZoomCorner != null)
|
||||||
|
mZoomCorner.dispose();
|
||||||
|
if (mBlurWindow != null)
|
||||||
|
mBlurWindow.dispose();
|
||||||
|
if (mGrayInnerWindow != null)
|
||||||
|
mGrayInnerWindow.dispose();
|
||||||
|
if (mRgbaInnerWindow != null)
|
||||||
|
mRgbaInnerWindow.dispose();
|
||||||
if (mRgba != null)
|
if (mRgba != null)
|
||||||
mRgba.dispose();
|
mRgba.dispose();
|
||||||
if (mGray != null)
|
if (mGray != null)
|
||||||
@@ -149,6 +159,11 @@ class ImageManipulationsView extends SampleCvViewBase {
|
|||||||
mRgba = null;
|
mRgba = null;
|
||||||
mGray = null;
|
mGray = null;
|
||||||
mIntermediateMat = null;
|
mIntermediateMat = null;
|
||||||
|
mRgbaInnerWindow = null;
|
||||||
|
mGrayInnerWindow = null;
|
||||||
|
mBlurWindow = null;
|
||||||
|
mZoomCorner = null;
|
||||||
|
mZoomWindow = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user