Ask Your Question

Revision history [back]

How to capture the photo or video of the person when their face is being detected.

So, I am trying to make a face recognition app using the OpenCV library. I am successfully able to detect the faces of the people using haarcascade. I am currently using the javaCameraView to show the live feed of the camera. I want to capture the person's picture or record a video when their face is being detected. I tried using onPictureTaken method but it has been deprecated as of android 5.0

How to capture the photo or video of the person when their face is being detected.

So, I am trying to make a face recognition app using the OpenCV library. I am successfully able to detect the faces of the people using haarcascade. I am currently using the javaCameraView to show the live feed of the camera. I want to capture the person's picture or record a video when their face is being detected. I tried using onPictureTaken method but it has been deprecated as of android 5.0

Code :

CascadeClassifier faceDetector;

BaseLoaderCallback baseLoaderCallback = new BaseLoaderCallback(MainActivity.this) {


    @Override
    public void onManagerConnected(int status) throws IOException {
        switch (status) {
            case BaseLoaderCallback.SUCCESS: {


                InputStream is = getResources().openRawResource(R.raw.haarcascade_frontalface_alt2);
                File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
                cascFile = new File(cascadeDir, "haarcascade_frontalface_alt2.xml");

                FileOutputStream fos = new FileOutputStream(cascFile);
                byte[] buffer = new byte[4096];
                int bytesRead;

                while ((bytesRead = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, bytesRead);
                }
                is.close();
                fos.close();

                faceDetector = new CascadeClassifier(cascFile.getAbsolutePath());
                if (faceDetector.empty()) {
                    faceDetector = null;
                } else {
                    cascadeDir.delete();
                }
                javaCameraView.enableView();

                break;


            }
            default: {
                super.onManagerConnected(status);
                break;
            }

        }


    }
};


static {

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    javaCameraView = findViewById(R.id.my_camera_view);
    javaCameraView.setVisibility(SurfaceView.VISIBLE);
    javaCameraView.setCvCameraViewListener(MainActivity.this);
    btnCapture = findViewById(R.id.btnCapture);

    btnCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

//TALKING ABOUT THIS CODE

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
            String currentDateandTime = sdf.format(new Date());
            String fileName = Environment.getExternalStorageState() +
                    "/sample_picture_" + currentDateandTime + ".jpg";
            Toast.makeText(MainActivity.this,  fileName + " saved", Toast.LENGTH_SHORT).show();
            String filename = "/storage/emulated/0/DCIM/Camera/SampleImg.jpg";
            Imgcodecs.imwrite(filename,mRGBA);
        }
    });

}

@Override
public void onCameraViewStarted(int width, int height) {
    mRGBA = new Mat(height, width, CvType.CV_8UC4);
    mGray = new Mat();

}

@Override
public void onCameraViewStopped() {
    mRGBA.release();
    mGray.release();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) throws IOException {
    mRGBA = inputFrame.rgba();
    mGray = inputFrame.gray();
    //detect face

    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(mRGBA, faceDetections);

    for (Rect rect : faceDetections.toArray()) {
        Imgproc.rectangle(mRGBA, new Point(rect.x, rect.y),
                new Point(rect.x + rect.width, rect.y + rect.height),
                new Scalar(255, 0, 0));


    }



    return mRGBA;
}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (javaCameraView != null) {
        javaCameraView.disableView();
    }
}

@Override
protected void onPause() {
    super.onPause();
    if (javaCameraView != null) {
        javaCameraView.disableView();
    }
}

@Override
protected void onResume() {
    super.onResume();

    if (OpenCVLoader.initDebug()) {
        Log.d(TAG, "OpenCV connected successfully");
        try {
            baseLoaderCallback.onManagerConnected(BaseLoaderCallback.SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Log.d(TAG, "OpenCV not connected successfully");
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, baseLoaderCallback);
    }

}

I tried creating a button that could be used to capture the picture. I tried this code in onClick method from StackOverflow but it still doesn't save to my storage.