Fix Android samples for devices having front camera only (Nexus 7)
This commit is contained in:
@@ -38,8 +38,4 @@ public class DetectionBasedTracker
|
||||
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_tracker");
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
public class FdActivity extends Activity {
|
||||
private static final String TAG = "Sample-FD::Activity";
|
||||
private static final String TAG = "OCVSample::Activity";
|
||||
|
||||
private MenuItem mItemFace50;
|
||||
private MenuItem mItemFace40;
|
||||
@@ -42,6 +42,7 @@ public class FdActivity extends Activity {
|
||||
mView.setDetectorType(mDetectorType);
|
||||
mView.setMinFaceSize(0.2f);
|
||||
setContentView(mView);
|
||||
|
||||
// Check native OpenCV camera
|
||||
if( !mView.openCamera() ) {
|
||||
AlertDialog ad = new AlertDialog.Builder(mAppContext).create();
|
||||
@@ -49,13 +50,14 @@ public class FdActivity extends Activity {
|
||||
ad.setMessage("Fatal error: can't open camera!");
|
||||
ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
});
|
||||
ad.show();
|
||||
}
|
||||
} break;
|
||||
|
||||
/** OpenCV loader cannot start Google Play **/
|
||||
case LoaderCallbackInterface.MARKET_ERROR:
|
||||
{
|
||||
@@ -80,28 +82,27 @@ public class FdActivity extends Activity {
|
||||
};
|
||||
|
||||
public FdActivity() {
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
mDetectorName = new String[2];
|
||||
mDetectorName[FdView.JAVA_DETECTOR] = "Java";
|
||||
mDetectorName[FdView.NATIVE_DETECTOR] = "Native (tracking)";
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
Log.i(TAG, "onPause");
|
||||
if (mView != null)
|
||||
Log.i(TAG, "called onPause");
|
||||
if (null != mView)
|
||||
mView.releaseCamera();
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
Log.i(TAG, "onResume");
|
||||
Log.i(TAG, "called onResume");
|
||||
super.onResume();
|
||||
|
||||
Log.i(TAG, "Trying to load OpenCV library");
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
|
||||
{
|
||||
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack)) {
|
||||
Log.e(TAG, "Cannot connect to OpenCV Manager");
|
||||
}
|
||||
}
|
||||
@@ -109,7 +110,7 @@ public class FdActivity extends Activity {
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
Log.i(TAG, "onCreate");
|
||||
Log.i(TAG, "called onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
@@ -117,7 +118,7 @@ public class FdActivity extends Activity {
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
Log.i(TAG, "onCreateOptionsMenu");
|
||||
Log.i(TAG, "called onCreateOptionsMenu");
|
||||
mItemFace50 = menu.add("Face size 50%");
|
||||
mItemFace40 = menu.add("Face size 40%");
|
||||
mItemFace30 = menu.add("Face size 30%");
|
||||
@@ -128,7 +129,7 @@ public class FdActivity extends Activity {
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Log.i(TAG, "Menu Item selected " + item);
|
||||
Log.i(TAG, "called onOptionsItemSelected; selected item: " + item);
|
||||
if (item == mItemFace50)
|
||||
mView.setMinFaceSize(0.5f);
|
||||
else if (item == mItemFace40)
|
||||
@@ -137,8 +138,7 @@ public class FdActivity extends Activity {
|
||||
mView.setMinFaceSize(0.3f);
|
||||
else if (item == mItemFace20)
|
||||
mView.setMinFaceSize(0.2f);
|
||||
else if (item == mItemType)
|
||||
{
|
||||
else if (item == mItemType) {
|
||||
mDetectorType = (mDetectorType + 1) % mDetectorName.length;
|
||||
item.setTitle(mDetectorName[mDetectorType]);
|
||||
mView.setDetectorType(mDetectorType);
|
||||
|
@@ -22,22 +22,22 @@ import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
class FdView extends SampleCvViewBase {
|
||||
private static final String TAG = "Sample-FD::View";
|
||||
private Mat mRgba;
|
||||
private Mat mGray;
|
||||
private File mCascadeFile;
|
||||
private CascadeClassifier mJavaDetector;
|
||||
private DetectionBasedTracker mNativeDetector;
|
||||
private static final String TAG = "OCVSample::View";
|
||||
private Mat mRgba;
|
||||
private Mat mGray;
|
||||
private File mCascadeFile;
|
||||
private CascadeClassifier mJavaDetector;
|
||||
private DetectionBasedTracker mNativeDetector;
|
||||
|
||||
private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);
|
||||
private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);
|
||||
|
||||
public static final int JAVA_DETECTOR = 0;
|
||||
public static final int NATIVE_DETECTOR = 1;
|
||||
public static final int JAVA_DETECTOR = 0;
|
||||
public static final int NATIVE_DETECTOR = 1;
|
||||
|
||||
private int mDetectorType = JAVA_DETECTOR;
|
||||
private int mDetectorType = JAVA_DETECTOR;
|
||||
|
||||
private float mRelativeFaceSize = 0;
|
||||
private int mAbsoluteFaceSize = 0;
|
||||
private float mRelativeFaceSize = 0;
|
||||
private int mAbsoluteFaceSize = 0;
|
||||
|
||||
public void setMinFaceSize(float faceSize) {
|
||||
mRelativeFaceSize = faceSize;
|
||||
@@ -62,6 +62,7 @@ class FdView extends SampleCvViewBase {
|
||||
super(context);
|
||||
|
||||
try {
|
||||
// load cascade file from application resources
|
||||
InputStream is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface);
|
||||
File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE);
|
||||
mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
|
||||
@@ -90,10 +91,13 @@ class FdView extends SampleCvViewBase {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
|
||||
}
|
||||
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i(TAG, "called surfaceCreated");
|
||||
synchronized (this) {
|
||||
// initialize Mats before usage
|
||||
mGray = new Mat();
|
||||
|
@@ -10,7 +10,7 @@ import android.graphics.Paint;
|
||||
import android.util.Log;
|
||||
|
||||
public class FpsMeter {
|
||||
private static final String TAG = "Sample::FpsMeter";
|
||||
private static final String TAG = "OCVSample::FpsMeter";
|
||||
int step;
|
||||
int framesCouner;
|
||||
double freq;
|
||||
|
@@ -14,7 +14,7 @@ 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 static final String TAG = "OCVSample::BaseView";
|
||||
|
||||
private SurfaceHolder mHolder;
|
||||
private VideoCapture mCamera;
|
||||
@@ -28,77 +28,66 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
|
||||
public boolean openCamera() {
|
||||
Log.i(TAG, "openCamera");
|
||||
synchronized (this) {
|
||||
public synchronized boolean openCamera() {
|
||||
Log.i(TAG, "Opening Camera");
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
releaseCamera();
|
||||
mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
|
||||
if (!mCamera.isOpened()) {
|
||||
Log.e(TAG, "Failed to open native camera");
|
||||
releaseCamera();
|
||||
return false;
|
||||
}
|
||||
Log.e(TAG, "Can't open native camera");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void releaseCamera() {
|
||||
Log.i(TAG, "releaseCamera");
|
||||
synchronized (this) {
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
public synchronized void releaseCamera() {
|
||||
Log.i(TAG, "Releasing Camera");
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setupCamera(int width, int height) {
|
||||
Log.i(TAG, "setupCamera("+width+", "+height+")");
|
||||
synchronized (this) {
|
||||
if (mCamera != null && mCamera.isOpened()) {
|
||||
List<Size> sizes = mCamera.getSupportedPreviewSizes();
|
||||
int mFrameWidth = width;
|
||||
int mFrameHeight = height;
|
||||
public synchronized void setupCamera(int width, int height) {
|
||||
if (mCamera != null && mCamera.isOpened()) {
|
||||
Log.i(TAG, "Setup Camera - " + width + "x" + height);
|
||||
List<Size> sizes = 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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
|
||||
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
Log.i(TAG, "surfaceChanged");
|
||||
Log.i(TAG, "called surfaceChanged");
|
||||
setupCamera(width, height);
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceCreated");
|
||||
Log.i(TAG, "called surfaceCreated");
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceDestroyed");
|
||||
releaseCamera();
|
||||
Log.i(TAG, "surfaceDestroyed2");
|
||||
Log.i(TAG, "called surfaceDestroyed");
|
||||
}
|
||||
|
||||
protected abstract Bitmap processFrame(VideoCapture capture);
|
||||
|
||||
public void run() {
|
||||
Log.i(TAG, "Starting processing thread");
|
||||
Log.i(TAG, "Started processing thread");
|
||||
mFps.init();
|
||||
|
||||
while (true) {
|
||||
@@ -128,7 +117,6 @@ public abstract class SampleCvViewBase extends SurfaceView implements SurfaceHol
|
||||
bmp.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
Log.i(TAG, "Finishing processing thread");
|
||||
Log.i(TAG, "Finished processing thread");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user