/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Log.i(TAG, "called onCreate"); super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.face_detect_surface_view); // // try { // load cascade file from application resources InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml"); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath()); if (mJavaDetector.empty()) { Log.e(TAG, "Failed to load cascade classifier"); mJavaDetector = null; } else Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath()); mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0); // hujiawei cascadeDir.delete(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "Failed to load cascade. Exception thrown: " + e); } mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view); mOpenCvCameraView.enableView(); // mOpenCvCameraView.setCvCameraViewListener(this); }
public FdView(Context context) { super(context); try { InputStream is = context.getResources().openRawResource(R.raw.lbpcascade_frontalface); File cascadeDir = context.getDir("cascade", Context.MODE_PRIVATE); mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml"); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); // --------------------------------- load left eye classificator // ----------------------------------- InputStream iser = context.getResources().openRawResource(R.raw.haarcascade_lefteye_2splits); File cascadeDirER = context.getDir("cascadeER", Context.MODE_PRIVATE); File cascadeFileER = new File(cascadeDirER, "haarcascade_eye_right.xml"); FileOutputStream oser = new FileOutputStream(cascadeFileER); byte[] bufferER = new byte[4096]; int bytesReadER; while ((bytesReadER = iser.read(bufferER)) != -1) { oser.write(bufferER, 0, bytesReadER); } iser.close(); oser.close(); // ---------------------------------------------------------------------------------------------------- // --------------------------------- load right eye classificator // ------------------------------------ InputStream isel = context.getResources().openRawResource(R.raw.haarcascade_lefteye_2splits); File cascadeDirEL = context.getDir("cascadeEL", Context.MODE_PRIVATE); File cascadeFileEL = new File(cascadeDirEL, "haarcascade_eye_left.xml"); FileOutputStream osel = new FileOutputStream(cascadeFileEL); byte[] bufferEL = new byte[4096]; int bytesReadEL; while ((bytesReadEL = isel.read(bufferEL)) != -1) { osel.write(bufferEL, 0, bytesReadEL); } isel.close(); osel.close(); // ------------------------------------------------------------------------------------------------------ mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath()); mCascadeER = new CascadeClassifier(cascadeFileER.getAbsolutePath()); mCascadeEL = new CascadeClassifier(cascadeFileER.getAbsolutePath()); if (mJavaDetector.empty() || mCascadeER.empty() || mCascadeEL.empty()) { Log.e(TAG, "Failed to load cascade classifier"); mJavaDetector = null; mCascadeER = null; mCascadeEL = null; } else Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath()); mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0); cascadeDir.delete(); cascadeFileER.delete(); cascadeDirER.delete(); cascadeFileEL.delete(); cascadeDirEL.delete(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "Failed to load cascade. Exception thrown: " + e); } }
@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); try { // load cascade file from application resources InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml"); FileOutputStream os = new FileOutputStream(mCascadeFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } is.close(); os.close(); // --------------------------------- load left eye // classificator ----------------------------------- InputStream iser = getResources().openRawResource(R.raw.haarcascade_lefteye_2splits); File cascadeDirER = getDir("cascadeER", Context.MODE_PRIVATE); File cascadeFileER = new File(cascadeDirER, "haarcascade_eye_right.xml"); FileOutputStream oser = new FileOutputStream(cascadeFileER); byte[] bufferER = new byte[4096]; int bytesReadER; while ((bytesReadER = iser.read(bufferER)) != -1) { oser.write(bufferER, 0, bytesReadER); } iser.close(); oser.close(); mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath()); if (mJavaDetector.empty()) { Log.e(TAG, "Failed to load cascade classifier"); mJavaDetector = null; } else Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath()); mJavaDetectorEye = new CascadeClassifier(cascadeFileER.getAbsolutePath()); if (mJavaDetectorEye.empty()) { Log.e(TAG, "Failed to load cascade classifier"); mJavaDetectorEye = null; } else Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath()); cascadeDir.delete(); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, "Failed to load cascade. Exception thrown: " + e); } mOpenCvCameraView.setCameraIndex(1); mOpenCvCameraView.enableFpsMeter(); mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } }