FaceDetection is not happening over the proper region in an ImageViewer

asked 2013-10-18 04:01:13 -0600

bunta gravatar image

updated 2013-10-18 05:16:18 -0600

I am trying to do a FaceDetection over an Image displayed over Android ImageView. I have made the ImageView as fulls screen mode, then I loaded the Image from storage as-

IplImage image = cvLoadImage(strImageFilePath, // filename
                CV_LOAD_IMAGE_GRAYSCALE);

Then I ran the classifiers and then tried to draw the rectangle but its coming some other region-

canvas.drawRect(new Rect(x, y, x + w, y + h), paint);

Following is the code sample-

public void process(String strImageFilePath) {


        ClassifierLoader classifierLoader = ClassifierLoader.getInstance(getContext());
        classifier = classifierLoader.getClassifier_face();
        classifier_mouth = classifierLoader.getClassifier_mouth();
        IplImage image = cvLoadImage(strImageFilePath, // filename
                CV_LOAD_IMAGE_GRAYSCALE);
        cvClearMemStorage(storage);
        Log.i("imageimage ", ""+image.height()+" Width "+image.width());


        cvClearMemStorage(storage);
        faces = cvHaarDetectObjects(image, classifier, storage, 1.1, 4,
                CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH);
        mouth = cvHaarDetectObjects(image, classifier_mouth, storage, 1.1, 3,
                CV_HAAR_FIND_BIGGEST_OBJECT | CV_HAAR_DO_ROUGH_SEARCH);

        invalidate();

        Log.i("AYA ", "IMAGE AAYA " + image+" FAC "+faces.total()+" mouth "+mouth.total()+ " THIS "+this.getWidth() );

    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextSize(20);
        paint.setStrokeWidth(8);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(20, 40, 5, paint);
        if(faces != null){
        if (faces.total() > 0) {

            String s = "Processed Face";
            float textWidth = paint.measureText(s);
            canvas.drawText(s, (getWidth() - textWidth) / 2, 20, paint);
            CvRect r = new CvRect(cvGetSeqElem(faces, 0));
            int x = r.x(), y = r.y(), w = r.width(), h = r.height();
            Log.i("IMAGE VIEW DRAW ", "x "+x+" y "+y+" w "+w+" h "+h);
            canvas.drawRect(new Rect(x, y, x + w, y + h), paint);

            if(mouth.total() > 0){
                CvRect rct = new CvRect(cvGetSeqElem(mouth, 0));
                canvas.drawRect(new Rect(rct.x(),rct.y(),rct.x()+rct.width(),rct.y()+rct.height()), paint);
            }

        }
    }
    }

I believe I need to do something with the height of IplImage but since I am directly loading the same from storage , it might be the issue. I actually wanted to load the image from image viewer but I couldn;t get it :( http://answers.opencv.org/question/22636/how-to-load-an-iplimage-from-android-imageview/?comment=22662#comment-22662

This is how algorithm is currently drawing the rectangle-

image description

on Using drawing rectangle as-

 canvas.drawRect(new Rect(x, y, w, y ), paint);

the Mouth rectange is not at all visible.

image description

One observation I found is that the rectangle it draw is not for the face size, but it will match if I open the same window in portrait mode, so It may be something doing for portrait mode, and I need to somewhere invert the image. I am here directly just opening the Image in landscape mode

and If I don;t use full screen mode, then things are quite better like -

image description

But still not the perfect, using canvas.drawRect(new Rect(x, y, x + w, y + h), paint); Other case doesn;t even show the rectangle

edit retag flag offensive close merge delete

Comments

1

your rect is wrong.

canvas.drawRect(new Rect(x, y, w, h), paint); // not x+w, y+h
berak gravatar imageberak ( 2013-10-18 04:10:52 -0600 )edit

actually i tried that and in that case Face rectangle goes more left and mouth rectangle doesn't even visible. Thats why I modified that part

bunta gravatar imagebunta ( 2013-10-18 04:17:00 -0600 )edit
1

yea, wrong fix. just don't .

berak gravatar imageberak ( 2013-10-18 04:22:21 -0600 )edit
1
  • must ... try ... not ... to ... make ... remark ... on ... old ... API ... usage *
StevenPuttemans gravatar imageStevenPuttemans ( 2013-10-18 04:36:09 -0600 )edit

@steven , which all APi's are old here , i will then change them .. considering Android and javaCV are the only choices left with me, then

bunta gravatar imagebunta ( 2013-10-18 04:50:30 -0600 )edit

You are using CvRect for example, which is C-API, however Rect does the same but uses the new C++ API. But I am wondering if this is the same for the Java & Android API, missed that part obviously :P

StevenPuttemans gravatar imageStevenPuttemans ( 2013-10-18 05:00:11 -0600 )edit

@bunta, can you revert the 'fullscreen' change ? you're sampling an img from disk, but drawing on a probably upscaled one

berak gravatar imageberak ( 2013-10-18 05:04:25 -0600 )edit

updated the screenshot , its better but not perfect, since it was not perfect thats why I moved to full screen mode, but i think I need to work in this only

bunta gravatar imagebunta ( 2013-10-18 05:17:34 -0600 )edit
1

it does not seem to respect the image -origin now. check your xml / properties / layout stuff for strange settings there. it's probably not a code problem now

berak gravatar imageberak ( 2013-10-18 05:22:25 -0600 )edit

hello,actually I'm not able to help with your problem.I'm new in android & opencv.I'm trying to use opencv library to cope with realtime facedetection on android.This http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html was the sample I found.I tried to use this sample to build an project on android ,but failed.There were some error that I couldn't figure out.Then I saw your post,so I wonder would you please show me your src code to help with my problem.

Ruby gravatar imageRuby ( 2014-01-27 06:56:15 -0600 )edit