Face Detection In Xamarin.Android

Face Detection

Firstly, we can create our main layout having a button to take picture using camera and name it the following:

Main.axml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:padding="10dip">  
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Click Take a Picture button"  
  11.         android:gravity="center_horizontal"  
  12.         android:layout_weight="1.0" />  
  13.     <Button  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:id="@+id/take_picture"  
  17.         android:layout_margin="5dip"  
  18.         android:text="Take Picture"  
  19.         android:layout_gravity="center_horizontal" />  
  20. </LinearLayout>   
Now we need a layout named detectlayout.axml having a ImageView to display the picture which we had captured using camera and a Button to detect faces in that Image.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <ImageView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:id="@+id/image_view"  
  10.         android:layout_weight="1.0"  
  11.         android:src="@android:drawable/ic_menu_report_image" />  
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/detect_face"  
  16.         android:text="Detect Face"  
  17.         android:layout_gravity="center_horizontal" />  
  18. </LinearLayout>   
Now let's start coding.

Firstly, we want to open an activity to take picture.
  1. private void openCamera()  
  2. {  
  3.    Intent intent = new Intent (Android.Provider.MediaStore.ActionImageCapture);  
  4.    StartActivityForResult (intent, TAKE_PICTURE_CODE);  
  5. }   
Now we want to Process the captured picture and display it on the detectlayout.axml.
  1. private void processCameraImage(Intent intent)  
  2. {  
  3.     //Change layout to main Layout  
  4.     SetContentView(Resource.Layout.detectlayout);  
  5.   
  6.     Button detect_face = FindViewById<Button> (Resource.Id.detect_face);  
  7.   
  8.     detect_face.Click += detect_face_Clicked;  
  9.   
  10.     ImageView image_view = FindViewById<ImageView> (Resource.Id.image_view);  
  11.   
  12.     //Set image   
  13.     image_view.SetImageBitmap(cameraBitmap);  
  14. }   
Add our main ingredient, here we detecting the faces on the picture and drawing a square to mark those faces.
  1. private void detectFaces(){  
  2.     //first check if picture has been taken  
  3.     if(null != cameraBitmap){  
  4.         //get width of a picture  
  5.         int width = cameraBitmap.Width;  
  6.         //get height of a picture  
  7.         int height = cameraBitmap.Height;  
  8.         //Initialize a facedetector with the picture dimensions and the max number of faces to check  
  9.         FaceDetector detector = new FaceDetector(width, height, MainActivity.MAX_FACES);  
  10.         //Create an array of faces with the number of max faces to check  
  11.         FaceDetector.Face[] faces = new FaceDetector.Face[MainActivity.MAX_FACES];  
  12.   
  13.         //create a main bitmap  
  14.         Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);  
  15.         //create a dither paint  
  16.         Paint ditherPaint = new Paint();  
  17.         //create a draw paint  
  18.         Paint drawPaint = new Paint();  
  19.   
  20.         //set true dither to dither paint variable  
  21.         ditherPaint.Dither = true;  
  22.         //set color red for the square  
  23.         drawPaint.Color = Color.Green;  
  24.         //set stroke to style  
  25.         drawPaint.SetStyle(Paint.Style.Stroke);  
  26.         //set stroke width  
  27.         drawPaint.StrokeWidth = 2;  
  28.   
  29.         //Create a canvas  
  30.         Canvas canvas = new Canvas();  
  31.         //set bitmap to canvas  
  32.         canvas.SetBitmap(bitmap565);  
  33.         //draw bitmap to canvas  
  34.         canvas.DrawBitmap(cameraBitmap, 00, ditherPaint);  
  35.   
  36.         //get a number of faces detected  
  37.         int facesFound = detector.FindFaces(bitmap565, faces);  
  38.         //mid face point  
  39.         PointF midPoint = new PointF();  
  40.         //eye distance variable  
  41.         float eyeDistance = 0.0f;  
  42.         //confidence variable  
  43.         float confidence = 0.0f;  
  44.         //print numbre of faces found  
  45.         System.Console.WriteLine ("Number of faces found: " + facesFound);  
  46.   
  47.         //check if found at least one face  
  48.         if(facesFound > 0)  
  49.         {  
  50.             //for each face draw a red squeare  
  51.             for(int index=0; index<facesFound; ++index){  
  52.                 // get midpoint of a face  
  53.                 faces[index].GetMidPoint(midPoint);  
  54.                 //get eye distance  
  55.                 eyeDistance = faces[index].EyesDistance();  
  56.                 //get confidence  
  57.                 confidence = faces [index].Confidence ();  
  58.                 //print all parameters  
  59.                 System.Console.WriteLine ("Confidence: " + confidence +   
  60.                     ", Eye distance: " + eyeDistance +   
  61.                     ", Mid Point: (" + midPoint.X + ", " + midPoint.Y + ")");  
  62.                 //draw a square in the picture  
  63.                 canvas.DrawRect((int)midPoint.X - eyeDistance ,   
  64.                     (int)midPoint.Y- eyeDistance ,   
  65.                     (int)midPoint.X + eyeDistance,   
  66.                     (int)midPoint.Y + eyeDistance, drawPaint);  
  67.             }  
  68.         }  
  69.   
  70.         //get imageview from layout  
  71.         ImageView imageView = (ImageView)FindViewById(Resource.Id.image_view);  
  72.         //set image with the red squares to imageview  
  73.         imageView.SetImageBitmap(bitmap565);  
  74.     }  
  75. }   
Now the stage is set for the show, only we need to do is just rearrange the code together in a logical way.

Go through the attachment for the complete project. 

Up Next
    Ebook Download
    View all
    Learn
    View all