In this article, you will learn how to create a simple desktop WPF Application that uses Face API to detect the number of faces in an image and mark the faces in the images.
What we will learn?
- How to subscribe Face API
- Generate Face API subscription key.
- Create new WPF Application (Classic Desktop).
- Configuring Face API library.
- Upload Image and detect no of Faces in Image.
Subscribe To Face API
Step 1
Go to https://www.microsoft.com/cognitive-services/en-us/
Signup with your email address to subscribe to Face API in Microsoft Cognitive Services.
Step 2
Once you are logged in, navigate to get started for free.
Step 3
Now, choose face API to subscribe to Microsoft Cognitive Services Face API. Proceed further with subscribe
Step 4
Now, generate your subscription key.
Create Application (Classic Desktop)
Step 1
Create new Project, as shown below.
Visual C# > Windows Desktop > WPF Application.
Step 2
Name the project as FaceDetectionDemo and click OK.
You will be able to see the screen given below.
Step 3 Now, we need to reference Newtonsoft.JSON in our Application.
For this, right click on Project and select Manage NuGget Package.
Browse for Newtonsoft.JSON and install.
Now, browse for Microsoft.ProjectOxford.Face and install.
Adding File Browser to Upload Image.
Step 1
Add a button on MainWindow.xaml, using designer or code. Here, I prefer adding the button, using code.
Replace <Grid></Grid> with the lines of code given below.
- <Grid x:Name="BackPanel">
- <Label x:Name="Label" FontStretch="Normal" Margin="0,0,0,0" Content="Please Upload Image to detect faces in Image" />
- <Image x:Name="FacePhoto" Stretch="Uniform" Margin="0,0,0,30" Source="http://ajaxuploader.com/images/drag-drop-file- upload.png" />
- <Button x:Name="BrowseButton" Margin="20,0,20,5" Height="35" Background="Blue" VerticalAlignment="Bottom" Content="Browse Image" Click="BrowseButton_Click" />
- </Grid>
Step 2
Add the codes to upload an image and detect the faces.
Now, go to MainWindow.xaml.cs
Add this, using Directive to the beginning of the file.
- using Microsoft.ProjectOxford.Face;
- using Microsoft.ProjectOxford.Face.Contract;
Also add line of codes given below to MainWindows class, as shown below.
- private readonly IFaceServiceClient faceServiceClient = new
- FaceServiceClient("XXXXXXXXXXXXXXXXXXXXXXXXX");
Add lines of code given below to click event of Browse button.
- private async void BrowseButton_Click(object sender, RoutedEventArgs e)
- {
-
- var openDlg = new Microsoft.Win32.OpenFileDialog();
-
-
- openDlg.Filter = "JPEG Image(*.jpg)|*.jpg";
- bool? result = openDlg.ShowDialog(this);
-
- if (!(bool)result)
- {
- return;
- }
-
- string filePath = openDlg.FileName;
-
- Uri fileUri = new Uri(filePath);
- BitmapImage bitmapSource = new BitmapImage();
-
- bitmapSource.BeginInit();
- bitmapSource.CacheOption = BitmapCacheOption.None;
- bitmapSource.UriSource = fileUri;
- bitmapSource.EndInit();
-
- FacePhoto.Source = bitmapSource;
-
-
- Label.Content = "Detecting...";
- Title = "Detecting...";
-
- FaceRectangle[] faceRects = await UploadAndDetectFaces(filePath);
- Title = String.Format("Detection Finished. {0} face(s) detected", faceRects.Length);
- Label.Content = String.Format("Detection Finished. {0} face(s) detected", faceRects.Length);
-
- if (faceRects.Length > 0)
- {
- DrawingVisual visual = new DrawingVisual();
- DrawingContext drawingContext = visual.RenderOpen();
- drawingContext.DrawImage(bitmapSource,
- new Rect(0, 0, bitmapSource.Width, bitmapSource.Height));
- double dpi = bitmapSource.DpiX;
- double resizeFactor = 96 / dpi;
-
-
- foreach (var faceRect in faceRects)
- {
- drawingContext.DrawRectangle(
- Brushes.Transparent,
- new Pen(Brushes.Blue, 2),
- new Rect(
- faceRect.Left * resizeFactor,
- faceRect.Top * resizeFactor,
- faceRect.Width * resizeFactor,
- faceRect.Height * resizeFactor
- )
- );
- }
-
- drawingContext.Close();
- RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap(
- (int)(bitmapSource.PixelWidth * resizeFactor),
- (int)(bitmapSource.PixelHeight * resizeFactor),
- 96,
- 96,
- PixelFormats.Pbgra32);
-
- faceWithRectBitmap.Render(visual);
- FacePhoto.Source = faceWithRectBitmap;
- }
-
- }
Add a new awaitable function named UploadAndDetectFaces(), which accepts imageFilePath as an object parameter.
- private async Task<FaceRectangle[]> UploadAndDetectFaces(string imageFilePath)
- {
- try
- {
- using (Stream imageFileStream = File.OpenRead(imageFilePath))
- {
- var faces = await faceServiceClient.DetectAsync(imageFileStream);
- var faceRects = faces.Select(face => face.FaceRectangle);
- return faceRects.ToArray();
- }
- }
- catch (Exception)
- {
- return new FaceRectangle[0];
- }
- }
Application Execution
Step 1
Build the Application and run.
Step 2
Browse the image.
Conclusion
You can use Microsoft Cognitive Servies Face API to detect the faces.