- Visual Studio 2015
- MIT
- Microsoft Cognitive Services
- Microsoft Cognitive Services
- Microsoft Face Verification
- 08/30/2016
Another little post talking about Microsoft Cognitive Services. In my previous post I talked about Emotion Recognition
Now I’m taking about Face Verification.
This API allow you to detect the identity of a person represented in a picture between a previously instructed list of known person.
You need to request a trial license to use Microsoft API from this url
and use it to create your FaceServiceClient object.
In your Visual Studio Project, you must add reference to the following Nuget packages:
- Newtonsoft.Json: a popular high-performance JSON framework for .NET.
- Microsoft.ProjectOxford.Emotion: allows the use of Microsoft's state-of-the-art cloud-based algorithmms to recognize emotions.
A FaceServiceClient object is required to perform all the API requests.
C#
Edit|Remove
csharp
//FaceService Client static FaceServiceClient Clnt = new FaceServiceClient(Properties.Settings.Default.FaceKey);
The main object is PersonGroup that represent a set of known person. I can perform face verification only within a specific PersonGroup.
Group is identified by 2 main attributes:
- Group ID
- Group Name
First of all we need to get or create the Group Object:
C#
Edit|Remove
csharp
/// <summary> /// Get or create a PersonGroup /// </summary> /// <param name="groupId"></param> /// <param name="groupName"></param> /// <returns></returns> private static async Task<PersonGroup> createGroup(string groupId, string groupName) { Task<PersonGroup> result = null; //Trying to get group specified by groupId try { var tGet = Clnt.GetPersonGroupAsync(groupId); tGet.Wait(); result = tGet; } catch (Exception) { //If the group does not exist, I create IT Task tCreate = Clnt.CreatePersonGroupAsync(groupId, groupName); tCreate.Wait(); var tGet = Clnt.GetPersonGroupAsync(groupId); tGet.Wait(); result = tGet; } return result.Result; }
GroupId property must be a lowercase string. In my case I used a “tolower” GUID.
Now we have to add a person to a PersonGroup and then associate one or more images to this person
C#
Edit|Remove
csharp
/// <summary> /// Add a person and his training images to a specific group /// </summary> /// <param name="groupId"></param> /// <param name="personName"></param>9 /// <param name="imagesPath"></param> private static async void addPersonToGroup(string groupId, string personName, List<string> imagesPath) { try { Task<AddPersistedFaceResult>[] tAdds = new Task<AddPersistedFaceResult>[imagesPath.Count]; //Create a person var p = Clnt.CreatePersonAsync(groupId, personName, personName); p.Wait(); var p1 = p.Result; //Adding person images for (int i = 0; i < imagesPath.Count; i++) { System.IO.Stream ms = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(imagesPath[i])); tAdds[i] = Clnt.AddPersonFaceAsync(groupId, p1.PersonId, ms); } Task.WaitAll(tAdds); } catch (Exception ex) { throw; } }
Now we can call API to identify a person within a specific PeopleGroup, passing the groupId and the stream of the person image to be recognized.
C#
Edit|Remove
csharp
/// <summary> /// Identify a person by his image within a specific PersonGroup /// </summary> /// <param name="groupId"></param> /// <param name="fotoPath"></param> /// <returns></returns> private static async Task<IdentifyResult[]> identifyPersons(string groupId, string fotoPath) { try { Task<Face[]> tDetect = Clnt.DetectAsync(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(fotoPath))); tDetect.Wait(); List<IdentifyResult> result = new List<IdentifyResult>(); if (tDetect.Result != null && tDetect.Result.Length > 0) { List<Guid> faceIds = new List<Guid>(); tDetect.Result.ToList().ForEach(t => faceIds.Add(t.FaceId)); Task<IdentifyResult[]> tIdent = Clnt.IdentifyAsync(groupId, faceIds.ToArray()); tIdent.Wait(); if (tIdent.Result != null && tIdent.Result.Length > 0) { return tIdent.Result; } else throw new ApplicationException("Person not found"); } else throw new ApplicationException("No Faces"); } catch (Exception ex) { throw; } }
C#
Edit|Remove
csharp
/// <summary> /// Return a PersonObject by his ID and groupId /// </summary> /// <param name="groupId"></param> /// <param name="personId"></param> /// <returns></returns> private static async Task<Person> getPersonById(string groupId, Guid personId) { try { var t = Clnt.GetPersonAsync(groupId, personId); t.Wait(); return t.Result; } catch (Exception ex) { throw; } }