How to detect image Faces and make Blur using Cognitive Face API with .NET Core

To know about Cognitive Services Face API, you must read these articles first.

 Getting Started .NET Core

  • Start Visual Studio 2015
  • From the file menu, select New, then Project
  • Select Web template
  • Select .NET Framework 4.6.1
  • Select ASP.NET Core Web Application (.NET Core)
  • Enter Name
  • Browse save location
  • Click OK


Select Web Application and do select Microsoft Azure if you want to host on azure server and click OK.


Copy the Face Preview API key.


Now add a new key in appsettings.json and paste key.


Now right click on project and click on Manage Nuget package manager and add these two packages.

     1. Microsoft.projectOxford.face

     2. ImageProcessorCore


Now let’s write some code on Program.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.AspNetCore.Hosting;  
  7. using ImageProcessorCore;  
  8. using Microsoft.Extensions.Configuration;  
  9. using Microsoft.ProjectOxford.Face;  
  10. using Microsoft.ProjectOxford.Face.Contract;  
  11. namespace Detect_Blur_Faces_NETCore_FaceAPI  
  12. {  
  13. public class Program  
  14. {  
  15. public static void Main(string[] args)  
  16. {  
  17. const string realImage = "myImage.jpg";  
  18. const string bluredImage = "blurFacesImage.jpg";  
  19. var configuration = BuildConfiguration();  
  20. var faceAPIKey = configuration["FaceAPIKey"];  
  21. TrackFaces(realImage, configuration["FaceAPIKey"])  
  22. .ContinueWith((task) =>  
  23. {  
  24. var faceRects = task.Result;  
  25. Console.WriteLine($"Detected {faceRects.Length} faces");  
  26. TrackBlurFaces(faceRects, realImage, bluredImage);  
  27. Console.WriteLine($"Done!!!");  
  28. });  
  29. Console.ReadLine();  
  30. }  
  31. private static IConfigurationRoot BuildConfiguration()  
  32. {  
  33. var builder = new ConfigurationBuilder();  
  34. builder.SetBasePath(Directory.GetCurrentDirectory());  
  35. builder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);  
  36. #if DEBUG  
  37. builder.AddUserSecrets("Detect-Blur-Faces-NETCore-FaceAPI");  
  38. #endif  
  39. return builder.Build();  
  40. }  
  41. private static void TrackBlurFaces(FaceRectangle[] faceRects, string sourceImage, string destinationImage)  
  42. {  
  43. if (File.Exists(destinationImage))  
  44. {  
  45. File.Delete(destinationImage);  
  46. }  
  47. if (faceRects.Length > 0)  
  48. {  
  49. using (FileStream stream = File.OpenRead("myImage.jpg"))  
  50. using (FileStream output = File.OpenWrite(destinationImage))  
  51. {  
  52. var image = new Image<Color, uint>(stream);  
  53. foreach (var faceRect in faceRects)  
  54. {  
  55. var rectangle = new Rectangle(  
  56. faceRect.Left,  
  57. faceRect.Top,  
  58. faceRect.Width,  
  59. faceRect.Height);  
  60. image = image.BoxBlur(20, rectangle);  
  61. }  
  62. image.SaveAsJpeg(output);  
  63. }  
  64. }  
  65. }  
  66. private static async Task<FaceRectangle[]> TrackFaces(string imageFilePath, string apiKey)  
  67. {  
  68. var faceServiceClient = new FaceServiceClient(apiKey);  
  69. try  
  70. {  
  71. using (Stream imageFileStream = File.OpenRead(imageFilePath))  
  72. {  
  73. var faces = await faceServiceClient.DetectAsync(imageFileStream);  
  74. var faceRects = faces.Select(face => face.FaceRectangle);  
  75. return faceRects.ToArray();  
  76. }  
  77. }  
  78. catch (Exception)  
  79. {  
  80. return new FaceRectangle[0];  
  81. }  
  82. }  
  83. }  
  84. }  

This is my real image which we have to track faces and make them blur.


Now let’s run the project and see the output.


As you can see the window says 10 faces detected. Now let’s check new image is saved in your project on the name of blurFacesImage.jpg.


Conclusion

In this article, we have learned how to detect total faces in image and make them blur using Cognitive Face API in .NET Core. If you have any question or comments, please download attached sample or send me message in C# Corner comments section.

Up Next
    Ebook Download
    View all
    Learn
    View all