Ask Your Question
0

Face Detection Capture and Store in Mobile Storage

asked 2015-01-12 20:56:02 -0600

JVJCJOHN gravatar image

updated 2015-01-13 05:08:35 -0600

I am getting error about storing the captured detected face in my Face Detection. Here is the code of Face Detection from tutorials and i also add a datatype aww = bitmap , as bitmap collector.

Code in face Detection :

protected Bitmap processFrame(VideoCapture capture) {
        capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
        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();

        if (mDetectorType == JAVA_DETECTOR)
        {
            if (mJavaDetector != null)
                mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                        , new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());


             if (mZoomCorner == null || mZoomWindow == null)
                    CreateAuxiliaryMats();


             Rect[] facesArray = faces.toArray();

             for (int i = 0; i < facesArray.length; i++){
                Rect r = facesArray[i];
                 Core.rectangle(mGray, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3);
                 Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3);

                 eyearea = new Rect(r.x +r.width/8,(int)(r.y + (r.height/4.5)),r.width - 2*r.width/8,(int)( r.height/3.0));
                 Core.rectangle(mRgba,eyearea.tl(),eyearea.br() , new Scalar(255,0, 0, 255), 2); 
                 Rect eyearea_right = new Rect(r.x +r.width/16,(int)(r.y + (r.height/4.5)),(r.width - 2*r.width/16)/2,(int)( r.height/3.0));
                 Rect eyearea_left = new Rect(r.x +r.width/16 +(r.width - 2*r.width/16)/2,(int)(r.y + (r.height/4.5)),(r.width - 2*r.width/16)/2,(int)( r.height/3.0));
                 Core.rectangle(mRgba,eyearea_left.tl(),eyearea_left.br() , new Scalar(255,0, 0, 255), 2); 
                 Core.rectangle(mRgba,eyearea_right.tl(),eyearea_right.br() , new Scalar(255, 0, 0, 255), 2);

                 if(learn_frames<5){
                    teplateR = get_template(mCascadeER,eyearea_right,24);
                    teplateL = get_template(mCascadeEL,eyearea_left,24);
                    learn_frames++;
                 }else{


                 match_value = match_eye(eyearea_right,teplateR,FdActivity.method); 

                 match_value = match_eye(eyearea_left,teplateL,FdActivity.method); 
                    ;
                    }
                    Imgproc.resize(mRgba.submat(eyearea_left), mZoomWindow2, mZoomWindow2.size());
                    Imgproc.resize(mRgba.submat(eyearea_right), mZoomWindow, mZoomWindow.size());

                    }
        }
        else if (mDetectorType == NATIVE_DETECTOR)
        {
            if (mNativeDetector != null)
                mNativeDetector.detect(mGray, faces);
        }
        else
        {
            Log.e(TAG, "Detection method is not selected!");
        }

        Rect[] facesArray = faces.toArray();
        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.ARGB_8888);

        try {
            Utils.matToBitmap(mRgba, bmp);
        } catch(Exception e) {
            Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
            bmp.recycle();
            bmp = null;
        }
        aww = bmp;
        return bmp;
    }

After that, ive made an onclick listener to capture the face detected. Here is the code :

private void storeImage(Bitmap image,Context l) {
        File pictureFile = getOutputMediaFile(l);
        if (pictureFile == null) {
            Log.d(TAG,
                    "Error creating media file, check storage permissions: ");// e.getMessage());
            return;
        } 
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            image.compress(Bitmap.CompressFormat.JPEG, 40, fos);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }  
    }
    private  File getOutputMediaFile(Context l){
        // To be safe, you should check that the SDCard is ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-01-13 02:15:18 -0600

berak gravatar image

updated 2015-01-13 03:25:43 -0600

you can save the Mat from the face-detection straight away, without converting to Bitmap or such:

 Rect[] facesArray = faces.toArray();
 if (facesArray.length == 0) // better safe, than sorry.
        return;
 Rect found = facesArray[0];
 Mat img = mRgba.submat(found);

String folder = Environment.getExternalStorageDirectory().getPath() + "/ocv";
String fname = folder.getAbsolutePath() + "/" + "face.png";
Highgui.imwrite(fname, img);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-01-12 20:56:02 -0600

Seen: 458 times

Last updated: Jan 13 '15