Introduction
In this article, we are going to create Microsoft Azure Cognitive Service Face API in half an hour. Microsoft Azure Cognitive Services are sets of APIs and Services available for the developers to make their applications more interactive and intelligent. This was formerly known as Project Oxford. Here Microsoft uses Machine Learning in the background to create these APIs. So far, we have the preceding list of APIs. You can always see this article on my blog here.
- Emotion and video detection
- Facial Recognition
- Speech Recognition
- Vision Recognition
- Speech and language understanding
Prerequisites
- Azure Subscription
- Visual Studio
If you don’t have any Azure subscription, please sign up for a subscription here.
Download the source code
You can always download the source code used for this video from here
Create the Face API in Azure portal
To know how to create a Face Recognition API in Azure portal, please see the video here.
Add the reference to Microsoft.ProjectOxford.Face to your project
Before we start coding, please make sure that you add the reference to Microsoft.ProjectOxford.Face from NuGet package manager.
Microsoft.ProjectOxford.Face
Using the code
To get started, open your Visual Studio and create a new WPF application.
Add Image and button control to MainWindow.xaml
- <Grid>
- <Image Stretch="UniformToFill" x:Name="FaceImage" HorizontalAlignment="Left" Margin="0,0,0,30" />
- <Button x:Name="BtnUpload" VerticalAlignment="Bottom" Content="Upload the image" Margin="20,5" Height="20" Click="BtnUpload_Click" />
- </Grid>
Create an instance of IFaceServiceClient
- private readonly IFaceServiceClient _faceServiceClient = new FaceServiceClient("key", "end point");
If you are selecting the location as Southeast Asia while creating the Face API in portal, it is mandatory to use the second parameter, that is, your end point.
Load the image to image control
- private async void BtnUpload_Click(object sender, RoutedEventArgs e)
- {
-
- var openFrame = new Microsoft.Win32.OpenFileDialog
- {
- Filter = "JPEG Image(*.jpg)|*.jpg"
- };
- var result = openFrame.ShowDialog(this);
- if (!(bool) result) return;
- var filePath = openFrame.FileName;
- var fileUri = new Uri(filePath);
- var bitMapSource = new BitmapImage();
- bitMapSource.BeginInit();
- bitMapSource.CacheOption = BitmapCacheOption.None;
- bitMapSource.UriSource = fileUri;
- bitMapSource.EndInit();
- FaceImage.Source = bitMapSource;
- }
Detect the faces count
-
-
-
-
-
- private async Task < FaceRectangle[] > DetectTheFaces(string filePath) {
- try {
- using(var imgStream = File.OpenRead(filePath)) {
- var faces = await _faceServiceClient.DetectAsync(imgStream);
- var faceRectangles = faces.Select(face => face.FaceRectangle);
- return faceRectangles.ToArray();
- }
- } catch (Exception e) {
- Console.WriteLine(e);
- throw;
- }
- }
Draw the rectangles in faces found
-
- Title = "Detecting....";
- FaceRectangle[] facesFound = await DetectTheFaces(filePath);
- Title = $ "Found {facesFound.Length} faces";
-
- if (facesFound.Length <= 0) return;
- var drwVisual = new DrawingVisual();
- var drwContext = drwVisual.RenderOpen();
- drwContext.DrawImage(bitMapSource, new Rect(0, 0, bitMapSource.Width, bitMapSource.Height));
- var dpi = bitMapSource.DpiX;
- var resizeFactor = 96 / dpi;
- foreach(var faceRect in facesFound)
- {
- drwContext.DrawRectangle(Brushes.Transparent, new Pen(Brushes.Blue, 6), new Rect(faceRect.Left * resizeFactor, faceRect.Top * resizeFactor, faceRect.Width * resizeFactor, faceRect.Height * resizeFactor));
- }
- drwContext.Close();
- var renderToImageCtrl = new RenderTargetBitmap((int)(bitMapSource.PixelWidth * resizeFactor), (int)(bitMapSource.PixelHeight * resizeFactor), 96, 96, PixelFormats.Pbgra32);
- renderToImageCtrl.Render(drwVisual);
- FaceImage.Source = renderToImageCtrl;
OutputNow, run your application and upload an image; it can be a single picture or a group picture.
Azure Face Recognition API Output
Check the statistics in Azure portal
If everything goes fine, you will be able to see the statistics in your Azure portal as preceding.
Face API Metric In Azure Portal
To Do
If you would like to compare the images or identify the person in an image, please read the documentation here Face identifying and comparing with Azure Face API
Conclusion
Did I miss anything that you may think is needed? Did you find this post useful? I hope you liked this article. Please share your valuable suggestions and feedback with me.
Your turn. What do you think?
A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.