@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { if (D) Log.i(TAG, "OpenCV loaded successfully"); mOpenCvCameraView.enableView(); Handler postHandler = new Handler(); postHandler.postDelayed( new Runnable() { @Override public void run() { // TODO Auto-generated method stub mMatchImageUtil.loadCachedFiles(); mProgressDialog.dismiss(); } }, 50); } break; default: { super.onManagerConnected(status); } break; } }
private void initializeOpenCVDependencies() { try { InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface); File cascadeDir = getDir("cascade", Context.MODE_PRIVATE); File 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 the cascade classifier cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath()); } catch (Exception e) { Log.e("OpenCVActivity", "Error loading cascade", e); } openCvCameraView.enableView(); }
@Override public boolean onOptionsItemSelected(MenuItem item) { String toastMesage = new String(); if (item == mItemSwitchCamera) { mOpenCvCameraView.setVisibility(SurfaceView.GONE); mIsJavaCamera = !mIsJavaCamera; if (mIsJavaCamera) { mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.java_surface_view); toastMesage = "Java Camera"; } else { mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.native_surface_view); toastMesage = "Native Camera"; } mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); mOpenCvCameraView.enableView(); Toast toast = Toast.makeText(this, toastMesage, Toast.LENGTH_LONG); toast.show(); } return true; }
@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); /* //Ce qui suit concerne la reconnaissance d'objet, à commenter si non souhaité try { // On charge le fichier XML contenant les données du classifieur (on l'a ajouté au dossier res/raw) InputStream is = getResources().openRawResource(R.raw.banana); 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(); mCascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath()); if (mCascadeClassifier.empty()) { Log.e(TAG, "Failed to load cascade classifier"); mCascadeClassifier = 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); } //Fin de la partie sur la reconnaissance d'image */ mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } }
@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } }
@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); mOpenCvCameraView.enableView(); mOpenCvCameraView.setOnTouchListener(ColorBlobDetectionActivity.this); } break; default: { super.onManagerConnected(status); } break; } }
/** 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); }
@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { Log.i(TAG, "OpenCV loaded successfully"); /* Now enable camera view to start receiving frames */ mOpenCvCameraView.setOnTouchListener(PuzzleActivity.this); mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } }
@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { // OpenCV loaded successfully! // Load native library AFTER OpenCV initialization initialize(); mOpenCvCameraView.enableView(); } break; default: { super.onManagerConnected(status); } break; } }
@Override public boolean onOptionsItemSelected(MenuItem item) { String toastMesage = new String(); Log.i(TAG, "called onOptionsItemSelected; selected item: " + item); if (item == mItemSwitchCamera) { mOpenCvCameraView.setVisibility(SurfaceView.GONE); mIsJavaCamera = !mIsJavaCamera; mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camview); toastMesage = "Java Camera"; mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE); mOpenCvCameraView.setCvCameraViewListener(this); mOpenCvCameraView.enableView(); Toast toast = Toast.makeText(this, toastMesage, Toast.LENGTH_LONG); toast.show(); } return true; }
@Override public void onManagerConnected(int status) { switch (status) { case LoaderCallbackInterface.SUCCESS: { mOpenCvCameraView.enableView(); outputFrame = new Mat(); marker = new Mat(); logo = new Mat(); Bitmap logoBmp = BitmapFactory.decodeResource(getResources(), R.drawable.logo); Utils.bitmapToMat(logoBmp, logo); Imgproc.resize(logo, logo, new Size(512, 512)); outputLogo = logo.clone(); timer = new Timer(); // Detection Task (runs in background) timer.schedule( new TimerTask() { // Hamming codes private final int[][] hamming_matrix = { {0, 0, 1, 1, 1}, {0, 1, 0, 1, 1}, {0, 1, 1, 0, 1}, {0, 1, 1, 1, 0}, {1, 0, 0, 1, 1}, {1, 0, 1, 0, 1}, {1, 0, 1, 1, 0}, {1, 1, 0, 0, 1}, {1, 1, 0, 1, 0}, {1, 1, 1, 0, 0}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 0}, {1, 1, 1, 0, 1}, {1, 1, 0, 1, 1}, {1, 1, 0, 0, 0}, {1, 0, 1, 1, 1} }; // Marker Detector object private MarkerDetector detector = new MarkerDetector(); /* Decodes a marker * Uses the planar marker image to try to read it's value * Returns -1 if the value was incorrect */ public int decodeMarker() { if (frame == null) { return -1; } else { // Inverse homography to get the planar marker image Imgproc.warpPerspective( frame, marker, homography, new Size(512, 512), Imgproc.WARP_INVERSE_MAP); int accum = 0; // Accumulator (marker value) int check = 0; // Check (Hamming codes of the image) int correctCheck = 0; // Hamming codes og the Accumulator int counter = 0; // Bit counter (Current bit being read) for (int y = 0; y < 6; ++y) { for (int x = 0; x < 6; ++x) { // Ignore the colored corners if (!(y <= 1 && (x <= 1 || x >= 4)) && !(y >= 4 && (x <= 1 || x >= 4))) { // Read the value from the middle of the square double[] val = marker.get(41 + 470 * y / 5, 41 + 470 * x / 5); if (val != null) { if (val[0] < 64 && val[1] < 64 && val[2] < 64) { if (counter < 15) { accum |= (1 << counter); for (int i = 0; i < 5; ++i) { correctCheck ^= (hamming_matrix[counter][i]) << i; } } else { check |= (1 << counter - 15); } } counter++; } } } } if (check == correctCheck) { return accum; } else { return -1; } } } @Override public void run() { // If there's a frame, try to find the marker and get the homography if (frame != null && frame.cols() > 0 && frame.rows() > 0) { Mat newHomography = detector.getHomography(frame); homography = newHomography; // If the marker was found, decode it's value if (homography != null) { int newMarkerValue = decodeMarker(); // If a valid value was found, update the current value if (newMarkerValue != -1) { markerValue = newMarkerValue; } } } } }, 1000, 20); } break; default: { super.onManagerConnected(status); } break; } }
@Override public void onResume() { super.onResume(); mOpenCvCameraView.enableView(); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); isTraining = getIntent().getBooleanExtra("Training", true); setContentView(R.layout.face_detect_surface_view); screenWidth = MainPageActivity.getScreenHeight(); screenHeight = MainPageActivity.getScreenWidth(); mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.fd_activity_surface_view); mOpenCvCameraView.setCvCameraViewListener(this); captureButton = (ImageView) findViewById(R.id.capturebutton); captureText = (TextView) findViewById(R.id.capturetext); captureButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { closeSoftInput(); if (capturingImage) { captureText.setText("Start Capturing"); capturingImage = false; captureButton.setImageResource(R.drawable.capturestart); } else { captureText.setText("Stop Capturing"); captureButton.setImageResource(R.drawable.capturestop); capturingImage = true; } } }); backButton = (ImageView) findViewById(R.id.backbutton); backButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { goBack(); } }); saveButton = (Button) findViewById(R.id.addtodbbutton); saveButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { closeSoftInput(); if (isTraining) { insertToDatabase(); } else { // deleteFromDatabase(); updateThisPerson(); } } }); nameEdit = (EditText) findViewById(R.id.nameedit); nameEdit.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI); deleteButton = (Button) findViewById(R.id.deletefromdbbutton); resetImagesForTraining(); initOtherViews(); if (!isTraining) { final int personIndex = getIntent().getIntExtra("personIndex", -1); new Thread( new Runnable() { @Override public void run() { FaceDetectionUtils.faceDataSource.open(); persons = FaceDetectionUtils.faceDataSource.getAllPersons(); FaceDetectionUtils.faceDataSource.close(); thisPerson = persons.get(personIndex); setImagesForDatabaseEdit(); FaceDetectionActivity.this.runOnUiThread( new Runnable() { @Override public void run() { nameEdit.setText(thisPerson.getName()); saveButton.setText("Update this person"); } }); } }) .start(); deleteButton.setVisibility(View.VISIBLE); deleteButton.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { closeSoftInput(); deleteFromDatabase(); } }); } mOpenCvCameraView.enableView(); bgLayout = (LinearLayout) findViewById(R.id.face_detect_layout); bgLayout.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { closeSoftInput(); return false; } }); }
@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; } }