Display detected face image on Android

asked 2017-01-13 18:31:10 -0600

Pramu gravatar image

Hello.

I'm trying to develop a mobile app where I would perform some operations on detected face images. So first I want to display detected images on the screen. I used example opencvsamplefacedetection as a starting point. Then I created a imageview object (detectedFace). Following is what I attempted:

   public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

    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());
    }
    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);

    }


    **if (facesArray.length>=1){
        Rect Reg = facesArray[0];
        Mat m = mGray.submat(Reg);
        Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(m, bm);
        // find the imageview and draw it!
        ImageView iv = (ImageView) findViewById(R.id.detectedFace);
        iv.setImageBitmap(bm);
    }**

    return mRgba;
}

When I run this I found that at iv.setImageBitmap(bm) my program crashes. Why is this? Is there a reason why I can't change attributes within this class? Please help me to figure out how this can be done. Thanks!

edit retag flag offensive close merge delete