CodeReview #1512 notes fixed.

pull/2/head
Alexander Smorkalov 13 years ago
parent 834a0b518c
commit 3780685fe6
  1. 4
      samples/android/face-detection/jni/Android.mk
  2. 61
      samples/android/face-detection/jni/DetectionBaseTracker.h
  3. 55
      samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp
  4. 61
      samples/android/face-detection/jni/DetectionBasedTracker_jni.h
  5. 52
      samples/android/face-detection/src/org/opencv/samples/fd/DetectionBaseTracker.java
  6. 52
      samples/android/face-detection/src/org/opencv/samples/fd/DetectionBasedTracker.java
  7. 9
      samples/android/face-detection/src/org/opencv/samples/fd/FdActivity.java
  8. 85
      samples/android/face-detection/src/org/opencv/samples/fd/FdView.java

@ -12,10 +12,10 @@ else
include $(OPENCV_MK_PATH) include $(OPENCV_MK_PATH)
endif endif
LOCAL_SRC_FILES := DetectionBaseTracker.cpp LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH) LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_base_tacker LOCAL_MODULE := detection_based_tacker
include $(BUILD_SHARED_LIBRARY) include $(BUILD_SHARED_LIBRARY)

@ -1,61 +0,0 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_opencv_samples_fd_DetectionBaseTracker */
#ifndef _Included_org_opencv_samples_fd_DetectionBaseTracker
#define _Included_org_opencv_samples_fd_DetectionBaseTracker
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_opencv_samples_fd_DetectionBaseTracker
* Method: nativeCreateObject
* Signature: (Ljava/lang/String;F)J
*/
JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeCreateObject
(JNIEnv *, jclass, jstring, jint);
/*
* Class: org_opencv_samples_fd_DetectionBaseTracker
* Method: nativeDestroyObject
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDestroyObject
(JNIEnv *, jclass, jlong);
/*
* Class: org_opencv_samples_fd_DetectionBaseTracker
* Method: nativeStart
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeStart
(JNIEnv *, jclass, jlong);
/*
* Class: org_opencv_samples_fd_DetectionBaseTracker
* Method: nativeStop
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeStop
(JNIEnv *, jclass, jlong);
/*
* Class: org_opencv_samples_fd_DetectionBaseTracker
* Method: nativeSetFaceSize
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeSetFaceSize
(JNIEnv *, jclass, jlong, jint);
/*
* Class: org_opencv_samples_fd_DetectionBaseTracker
* Method: nativeDetect
* Signature: (JJJ)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDetect
(JNIEnv *, jclass, jlong, jlong, jlong);
#ifdef __cplusplus
}
#endif
#endif

@ -1,4 +1,4 @@
#include <DetectionBaseTracker.h> #include <DetectionBasedTracker_jni.h>
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <opencv2/contrib/detection_based_tracker.hpp> #include <opencv2/contrib/detection_based_tracker.hpp>
@ -13,14 +13,12 @@
using namespace std; using namespace std;
using namespace cv; using namespace cv;
vector<Rect> RectFaces;
inline void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat) inline void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
{ {
mat = Mat(v_rect, true); mat = Mat(v_rect, true);
} }
JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeCreateObject JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeCreateObject
(JNIEnv * jenv, jclass jobj, jstring jFileName, jint faceSize) (JNIEnv * jenv, jclass jobj, jstring jFileName, jint faceSize)
{ {
const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL); const char* jnamestr = jenv->GetStringUTFChars(jFileName, NULL);
@ -42,11 +40,18 @@ JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeCr
je = jenv->FindClass("java/lang/Exception"); je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, e.what()); jenv->ThrowNew(je, e.what());
} }
catch (...)
{
LOGD("nativeCreateObject catched unknown exception");
jclass je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
return 0;
}
return result; return result;
} }
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDestroyObject JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDestroyObject
(JNIEnv * jenv, jclass jobj, jlong thiz) (JNIEnv * jenv, jclass jobj, jlong thiz)
{ {
try try
@ -62,9 +67,15 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDes
je = jenv->FindClass("java/lang/Exception"); je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, e.what()); jenv->ThrowNew(je, e.what());
} }
catch (...)
{
LOGD("nativeDestroyObject catched unknown exception");
jclass je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
}
} }
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeStart JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeStart
(JNIEnv * jenv, jclass jobj, jlong thiz) (JNIEnv * jenv, jclass jobj, jlong thiz)
{ {
try try
@ -79,10 +90,15 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeSta
je = jenv->FindClass("java/lang/Exception"); je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, e.what()); jenv->ThrowNew(je, e.what());
} }
catch (...)
{
LOGD("nativeStart catched unknown exception");
jclass je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
}
} }
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeStop JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeStop
(JNIEnv * jenv, jclass jobj, jlong thiz) (JNIEnv * jenv, jclass jobj, jlong thiz)
{ {
try try
@ -97,9 +113,15 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeSto
je = jenv->FindClass("java/lang/Exception"); je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, e.what()); jenv->ThrowNew(je, e.what());
} }
catch (...)
{
LOGD("nativeStop catched unknown exception");
jclass je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
}
} }
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeSetFaceSize JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSetFaceSize
(JNIEnv * jenv, jclass jobj, jlong thiz, jint faceSize) (JNIEnv * jenv, jclass jobj, jlong thiz, jint faceSize)
{ {
try try
@ -121,14 +143,21 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeSet
je = jenv->FindClass("java/lang/Exception"); je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, e.what()); jenv->ThrowNew(je, e.what());
} }
catch (...)
{
LOGD("nativeSetFaceSize catched unknown exception");
jclass je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
}
} }
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDetect JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDetect
(JNIEnv * jenv, jclass jobj, jlong thiz, jlong imageGray, jlong faces) (JNIEnv * jenv, jclass jobj, jlong thiz, jlong imageGray, jlong faces)
{ {
try try
{ {
vector<Rect> RectFaces;
((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray)); ((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
((DetectionBasedTracker*)thiz)->getObjects(RectFaces); ((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
vector_Rect_to_Mat(RectFaces, *((Mat*)faces)); vector_Rect_to_Mat(RectFaces, *((Mat*)faces));
@ -141,4 +170,10 @@ JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBaseTracker_nativeDet
je = jenv->FindClass("java/lang/Exception"); je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, e.what()); jenv->ThrowNew(je, e.what());
} }
catch (...)
{
LOGD("nativeDetect catched unknown exception");
jclass je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
}
} }

@ -0,0 +1,61 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_opencv_samples_fd_DetectionBasedTracker */
#ifndef _Included_org_opencv_samples_fd_DetectionBasedTracker
#define _Included_org_opencv_samples_fd_DetectionBasedTracker
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_opencv_samples_fd_DetectionBasedTracker
* Method: nativeCreateObject
* Signature: (Ljava/lang/String;F)J
*/
JNIEXPORT jlong JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeCreateObject
(JNIEnv *, jclass, jstring, jint);
/*
* Class: org_opencv_samples_fd_DetectionBasedTracker
* Method: nativeDestroyObject
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDestroyObject
(JNIEnv *, jclass, jlong);
/*
* Class: org_opencv_samples_fd_DetectionBasedTracker
* Method: nativeStart
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeStart
(JNIEnv *, jclass, jlong);
/*
* Class: org_opencv_samples_fd_DetectionBasedTracker
* Method: nativeStop
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeStop
(JNIEnv *, jclass, jlong);
/*
* Class: org_opencv_samples_fd_DetectionBasedTracker
* Method: nativeSetFaceSize
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeSetFaceSize
(JNIEnv *, jclass, jlong, jint);
/*
* Class: org_opencv_samples_fd_DetectionBasedTracker
* Method: nativeDetect
* Signature: (JJJ)V
*/
JNIEXPORT void JNICALL Java_org_opencv_samples_fd_DetectionBasedTracker_nativeDetect
(JNIEnv *, jclass, jlong, jlong, jlong);
#ifdef __cplusplus
}
#endif
#endif

@ -1,52 +0,0 @@
package org.opencv.samples.fd;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
public class DetectionBaseTracker
{
public DetectionBaseTracker(String filename, int faceSize)
{
mNativeObj = nativeCreateObject(filename, faceSize);
}
public void start()
{
nativeStart(mNativeObj);
}
public void stop()
{
nativeStop(mNativeObj);
}
public void setMinFaceSize(int faceSize)
{
nativeSetFaceSize(mNativeObj, faceSize);
}
public void detect(Mat imageGray, MatOfRect faces)
{
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
}
public void release()
{
nativeDestroyObject(mNativeObj);
mNativeObj = 0;
}
protected long mNativeObj = 0;
protected static native long nativeCreateObject(String filename, int faceSize);
protected static native void nativeDestroyObject(long thiz);
protected static native void nativeStart(long thiz);
protected static native void nativeStop(long thiz);
protected static native void nativeSetFaceSize(long thiz, int faceSize);
protected static native void nativeDetect(long thiz, long inputImage, long resultMat);
static
{
System.loadLibrary("detection_base_tacker");
}
}

@ -0,0 +1,52 @@
package org.opencv.samples.fd;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
public class DetectionBasedTracker
{
public DetectionBasedTracker(String cascadeName, int minFaceSize)
{
mNativeObj = nativeCreateObject(cascadeName, minFaceSize);
}
public void start()
{
nativeStart(mNativeObj);
}
public void stop()
{
nativeStop(mNativeObj);
}
public void setMinFaceSize(int size)
{
nativeSetFaceSize(mNativeObj, size);
}
public void detect(Mat imageGray, MatOfRect faces)
{
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
}
public void release()
{
nativeDestroyObject(mNativeObj);
mNativeObj = 0;
}
private long mNativeObj = 0;
private static native long nativeCreateObject(String cascadeName, int minFaceSize);
private static native void nativeDestroyObject(long thiz);
private static native void nativeStart(long thiz);
private static native void nativeStop(long thiz);
private static native void nativeSetFaceSize(long thiz, int size);
private static native void nativeDetect(long thiz, long inputImage, long faces);
static
{
System.loadLibrary("detection_based_tacker");
}
}

@ -26,8 +26,8 @@ public class FdActivity extends Activity {
public FdActivity() { public FdActivity() {
Log.i(TAG, "Instantiated new " + this.getClass()); Log.i(TAG, "Instantiated new " + this.getClass());
mDetectorName = new String[2]; mDetectorName = new String[2];
mDetectorName[0] = "Cascade"; mDetectorName[FdView.JAVA_DETECTOR] = "Java";
mDetectorName[1] = "DBT"; mDetectorName[FdView.NATIVE_DETECTOR] = "Native (tracking)";
} }
@Override @Override
@ -62,7 +62,8 @@ public class FdActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);
mView = new FdView(this); mView = new FdView(this);
mView.setDtetectorType(mDetectorType); mView.setDetectorType(mDetectorType);
mView.setMinFaceSize(0.2f);
setContentView(mView); setContentView(mView);
} }
@ -93,7 +94,7 @@ public class FdActivity extends Activity {
{ {
mDetectorType = (mDetectorType + 1) % mDetectorName.length; mDetectorType = (mDetectorType + 1) % mDetectorName.length;
item.setTitle(mDetectorName[mDetectorType]); item.setTitle(mDetectorName[mDetectorType]);
mView.setDtetectorType(mDetectorType); mView.setDetectorType(mDetectorType);
} }
return true; return true;
} }

@ -26,41 +26,40 @@ class FdView extends SampleCvViewBase {
private Mat mRgba; private Mat mRgba;
private Mat mGray; private Mat mGray;
private File mCascadeFile; private File mCascadeFile;
private CascadeClassifier mCascade; private CascadeClassifier mJavaDetector;
private DetectionBaseTracker mTracker; private DetectionBasedTracker mNativeDetector;
public final int CASCADE_DETECTOR = 0; private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);
public final int DBT_DETECTOR = 1;
private int mDetectorType = CASCADE_DETECTOR; public static final int JAVA_DETECTOR = 0;
public static final int NATIVE_DETECTOR = 1;
public static int mFaceSize = 200; private int mDetectorType = JAVA_DETECTOR;
private float mRelativeFaceSize = 0;
private int mAbsoluteFaceSize = 0;
public void setMinFaceSize(float faceSize) public void setMinFaceSize(float faceSize)
{ {
int height = mGray.rows(); mRelativeFaceSize = faceSize;
if (Math.round(height * faceSize) > 0); mAbsoluteFaceSize = 0;
{
mFaceSize = Math.round(height * faceSize);
}
mTracker.setMinFaceSize(mFaceSize);
} }
public void setDtetectorType(int type) public void setDetectorType(int type)
{ {
if (mDetectorType != type) if (mDetectorType != type)
{ {
mDetectorType = type; mDetectorType = type;
if (type == DBT_DETECTOR) if (type == NATIVE_DETECTOR)
{ {
Log.i(TAG, "Detection Base Tracker enabled"); Log.i(TAG, "Detection Based Tracker enabled");
mTracker.start(); mNativeDetector.start();
} }
else else
{ {
Log.i(TAG, "Cascade detectior enabled"); Log.i(TAG, "Cascade detector enabled");
mTracker.stop(); mNativeDetector.stop();
} }
} }
} }
@ -82,14 +81,14 @@ class FdView extends SampleCvViewBase {
is.close(); is.close();
os.close(); os.close();
mCascade = new CascadeClassifier(mCascadeFile.getAbsolutePath()); mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
if (mCascade.empty()) { if (mJavaDetector.empty()) {
Log.e(TAG, "Failed to load cascade classifier"); Log.e(TAG, "Failed to load cascade classifier");
mCascade = null; mJavaDetector = null;
} else } else
Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath()); Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());
mTracker = new DetectionBaseTracker(mCascadeFile.getAbsolutePath(), 0); mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);
cascadeDir.delete(); cascadeDir.delete();
@ -115,37 +114,49 @@ class FdView extends SampleCvViewBase {
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME); capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
if (mAbsoluteFaceSize == 0)
{
int height = mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0);
{
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
}
MatOfRect faces = new MatOfRect(); MatOfRect faces = new MatOfRect();
if (mDetectorType == CASCADE_DETECTOR) if (mDetectorType == JAVA_DETECTOR)
{ {
if (mCascade != null) if (mJavaDetector != null)
mCascade.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE
, new Size(mFaceSize, mFaceSize), new Size()); , new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
} }
else if (mDetectorType == DBT_DETECTOR) else if (mDetectorType == NATIVE_DETECTOR)
{ {
if (mTracker != null) if (mNativeDetector != null)
mTracker.detect(mGray, faces); mNativeDetector.detect(mGray, faces);
} }
else else
{ {
Log.e(TAG, "Detection method is not selected!"); Log.e(TAG, "Detection method is not selected!");
} }
for (Rect r : faces.toArray()) Rect[] facesArray = faces.toArray();
Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3); for (int i = 0; i < facesArray.length; i++)
Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.RGB_565/*.ARGB_8888*/); Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
try { try {
Utils.matToBitmap(mRgba, bmp); Utils.matToBitmap(mRgba, bmp);
return bmp;
} catch(Exception e) { } catch(Exception e) {
Log.e("org.opencv.samples.puzzle15", "Utils.matToBitmap() throws an exception: " + e.getMessage()); Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
bmp.recycle(); bmp.recycle();
return null; bmp = null;
} }
return bmp;
} }
@Override @Override
@ -160,8 +171,8 @@ class FdView extends SampleCvViewBase {
mGray.release(); mGray.release();
if (mCascadeFile != null) if (mCascadeFile != null)
mCascadeFile.delete(); mCascadeFile.delete();
if (mTracker != null) if (mNativeDetector != null)
mTracker.release(); mNativeDetector.release();
mRgba = null; mRgba = null;
mGray = null; mGray = null;

Loading…
Cancel
Save