Search code examples
androidandroid-canvascropface-detectionondraw

Face Swap Using Face Detection Android


I am trying to do Face swap kind of application using facedetection. Till now i get the faces detected in bitmap and draw oval on the faces detected. But now i need to use the faces inside the oval so that i can swap two faces. Is it possible. I need some suggestions regarding this.

My activity class as follows

public class FaceDetectionActivity extends Activity 
{


public MyView faceview;
public ImageView gallery;
private Uri imageURI;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
    setContentView(R.layout.main);

    faceview = (MyView)findViewById(R.id.faceview);

    gallery=(ImageView)findViewById(R.id.gallery);

    gallery.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 0 );

        }
    });


}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {

        if(requestCode==0){

        imageURI = data.getData(); 


        try {
            Bitmap b = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), imageURI);

            faceview.myBitmap=b;


        } catch (FileNotFoundException e) {
 //              TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IOException e) {
 //              TODO Auto-generated catch block  
            e.printStackTrace();
        }



        faceview.invalidate(); 

        }


        faceview.invalidate(); 
    } else {
        System.exit(0);
        Log.e("result", "BAD");
    }
}


}

and my view class

public class MyView extends ImageViewTouchBase {

public RectF rectF;
public Bitmap myBitmap;
private int width, height;
private FaceDetector.Face[] detectedFaces;
private int NUMBER_OF_FACES=10;
private FaceDetector faceDetector;
private int NUMBER_OF_FACE_DETECTED;
private float eyeDistance;
Matrix mImageMatrix;

public MyView(Context context, AttributeSet attrs) 
{
    super(context, attrs);
    BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
    bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
    myBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.familyportrait,bitmapFatoryOptions);
    width=myBitmap.getWidth();
    height=myBitmap.getHeight();
    detectedFaces=new FaceDetector.Face[NUMBER_OF_FACES];
    faceDetector=new FaceDetector(width,height,NUMBER_OF_FACES);
    NUMBER_OF_FACE_DETECTED=faceDetector.findFaces(myBitmap, detectedFaces);
}

@Override
protected void onDraw(Canvas canvas)
{
    if(myBitmap!=null)
    {


    canvas.drawBitmap(myBitmap, 0,0, null);

    }
    Paint myPaint = new Paint();
    myPaint.setColor(Color.GREEN);
    myPaint.setStyle(Paint.Style.STROKE); 
    myPaint.setStrokeWidth(3);

    for(int count=0;count<NUMBER_OF_FACE_DETECTED;count++)
    {
        Face face=detectedFaces[count];
        PointF midPoint=new PointF();
        face.getMidPoint(midPoint);

        eyeDistance=face.eyesDistance();

    float   left = midPoint.x - (float)(1.4 * eyeDistance);
    float   right = midPoint.x + (float)(1.4 * eyeDistance);
    float    top = midPoint.y - (float)(1.8 * eyeDistance);
    float    bottom = midPoint.y + (float)(1.8 * eyeDistance);

    Rect imageRect = new Rect(0, 0, width, height);
     rectF = new RectF();

    rectF.set(left,top,right,bottom);

    canvas.drawOval(rectF, myPaint);


    }
}

}

Now i want the content inside the oval to be selected. Please suggest me some ideas.


Solution

  • I just figured it out. I am creating another bitmap with the variables Left,Right,Top and Bottom from the above code. and then i get a square bitmap of the faces.I am extracting circular bitmap from the square bitmap faces. Thats it.