Ask Your Question

Face Detection is not working portrait mode in android plateform

asked 2014-03-18 04:19:06 -0600

Manoj Kumar gravatar image

updated 2014-03-18 05:02:05 -0600

Hi, I am creating an application for face recognition using opencv in android.App is working good in landscape mode. I want to use this app in portrait mode for this I added following code in onCameraFrame method :-

    int height = mGray.rows();
    int faceSize = Math.round(height * 0.4f);
    Mat temp = mGray.clone();
    Core.transpose(mGray, temp);
    Core.flip(temp, temp, -1);
    MatOfRect faces = new MatOfRect();
    mJavaDetector.detectMultiScale(temp, faces, 1.1, 2, 2, new Size(faceSize, faceSize), new Size());

I also did changes in CameraBridgeViewBase class, I replaced following lines of code

canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
                         new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2),
                         (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2),
                         (int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()),
                         (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null);


                     Matrix matrix = new Matrix();
                     matrix.preTranslate( (int)(canvas.getWidth() - mCacheBitmap.getWidth()) / 2,(canvas.getHeight() - mCacheBitmap.getHeight()) / 2);
                     if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                     matrix.postRotate(270f,( (int)canvas.getWidth()) / 2,(canvas.getHeight()) / 2);
                     canvas.drawBitmap(mCacheBitmap, matrix, new Paint());

But its not working.Face detector mark is not coming exactly surrounding user's face. It is coming slightly down and right side of the face. I have attached screenshot of this.


Please help me to resolve this problem. Thanks in advance !!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-06-17 12:23:32 -0600

Sveta gravatar image

faces that are detected are detected based on transposed and flipped gray Matrix. So you should flip your Rect coordinates also.

This is how i did it.

Core.transpose(mGray, mGray);
Core.flip(mGray, mGray, 0);

and when you draw Rectangle you do this :

for(int i=0, f=mFacesArray.length; i<f; i++){
tl.x = mFacesArray[i].tl().y;
tl.y = mFacesArray[i].tl().x;
br.x = mFacesArray[i].br().y;
br.y = mFacesArray[i].br().x;
Core.rectangle(mRgba, tl, br, STROKE_COLOR, 3);

tl and br are Point objects of course.

edit flag offensive delete link more


it doesn't work for me i have face same issue for face-detection in portrait mode.

Akhil Patel gravatar imageAkhil Patel ( 2019-07-11 00:55:42 -0600 )edit

Question Tools


Asked: 2014-03-18 04:19:06 -0600

Seen: 2,816 times

Last updated: Jun 17 '14