[Azure] C# + Azure Face API 建立 Person 進行辨識

2021-11-02

前幾篇寫了相關於 Azure Face API ,今天這一篇應該是暫時後最後一篇,畢竟手上也有事情再忙

我也就是順手把手上測試過的東西紀錄一下,官方案例算是清楚,不過他比較著重於非同步的做法,可能是因為會有失敗排成的關係

這邊我案例都是同步的做法,畢竟我只是要測試而已,這一篇主要案例就是建立 PersonGroup ,PersonGroup 裡面可以放 Person ,

之後你會到某個人的 PersonId ,之後你可以對 Person 餵圖進去讓他建立起模型,之後你再拿其他人的圖片,取到 FaceId 後

就可以問該 FaceId 跟 PersonId 相似度多少( 0-1 之間) 。



0.事前準備,安裝套件申請 Azure 服務請參考這一篇  [Azure] C# + Azure Face API 作臉部辨識簡單上手


1. 建立 PersonGroup 在這,我的案例是建立一個 PersonGroup , Id : group1 , Name: GIRLS ,請注意 Id 要全小寫

再次強調,我這邊都是同步的寫法,關於一些非同步的部分,建議看官方


var personGroupId = "group1"; var personGroupName = "GIRLS"; var faceClient = Authenticate(ENDPOINT, SUBSCRIPTION_KEY); // 檢查 PersonGroup 是不是存在&#65292;如果不存在就建立 var personGroupInfo = faceClient.PersonGroup.ListAsync().Result; if (personGroupInfo.Any(x => x.PersonGroupId == personGroupId)) { Result += personGroupId + " Already Existed !<br>"; } else { faceClient.PersonGroup.CreateAsync(personGroupId, personGroupName).GetAwaiter().GetResult(); Result += personGroupId + " Created.<br>"; }


2. 建立 PersonGroup 之後,就要建立一個人,在這裡我建立的這個人叫 Akane,介紹一下,這麻豆的圖片在這 

https://www.pakutaso.com/person/woman/akanesaya/ ,日本免費的圖庫,我下載三張照片,送進去 Person 中,讓他記憶




建立 Person(Akane) to PersonGroup (group1) ,並上傳三張圖片上去

//建立一個用戶 Akane var person1 = faceClient.PersonGroupPerson.CreateAsync(personGroupId, "Akane").Result; // 列出所有 Person var allGroupPeople = faceClient.PersonGroupPerson.ListAsync(personGroupId).Result; Result += "All People : " + JsonConvert.SerializeObject(allGroupPeople) + "<br>"; //把 Akane 得照片加入至 Person foreach (var person in allGroupPeople) { //Add image to Akane if (person.Name == "Akane") { using (System.IO.Stream stream = System.IO.File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "sample" + System.IO.Path.DirectorySeparatorChar + "as1.jpg")) { var uploadResult = faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId, stream).Result; Result += "as1.jpg - PersistedFaceId :" + uploadResult.PersistedFaceId + "<br>"; } using (System.IO.Stream stream = System.IO.File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "sample" + System.IO.Path.DirectorySeparatorChar + "as2.jpg")) { var uploadResult = faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId, stream).Result; Result += "as2.jpg - PersistedFaceId :" + uploadResult.PersistedFaceId + "<br>"; } using (System.IO.Stream stream = System.IO.File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "sample" + System.IO.Path.DirectorySeparatorChar + "as3.jpg")) { var uploadResult = faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId, stream).Result; Result += "as3.jpg - PersistedFaceId :" + uploadResult.PersistedFaceId + "<br>"; } } }


3. 最後我就是拿其他人的照片讓他進行辨識判斷

//辨識 var verifyImagePath = AppDomain.CurrentDomain.BaseDirectory + "sample" + System.IO.Path.DirectorySeparatorChar + "pw3.jpg"; System.IO.MemoryStream ms = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(verifyImagePath)); var verifyImageFaceIds = faceClient.Face.DetectWithStreamAsync(ms, detectionModel: DetectionModel.Detection01).Result; if (verifyImageFaceIds.Count > 0) { var res = faceClient.Face.VerifyFaceToPersonAsync(verifyImageFaceIds[0].FaceId.Value, akane.PersonId, personGroupId: personGroupId).Result; Result += "verify result:" + res.Confidence + "<br>"; } else { Result += "verify iamge no face detected<br>"; } System.IO.File.WriteAllBytes(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "result.jpg"), ms.ToArray()); Result += "<img src="result.jpg" style="max-width:50%;max-height:300px" />";

這邊測試幾張

跟 Akane 很像 但是不是本人


不是Akane 


是 Akane 本人但是側臉



是 Akane 本人,但是其他裝扮


reference:

https://blog.alantsai.net/posts/2018/10/cognitive-service-face-api-08-understand-persongroup-person-and-face-to-create-person-model

https://dotblogs.com.tw/maduka/Tags?qq=%E4%BA%BA%E8%87%89%E8%AD%98%E5%88%A5

https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/how-to-add-faces


當麻許的超技八 2014 | Donma Hsu Design.