public async Task <ObservableCollection <FaceClientResultModel> > Detection(Func <Stream> stream) { ObservableCollection <FaceClientResultModel> result = new ObservableCollection <FaceClientResultModel>(); IList <FaceAttributeType> faceAttributes = new FaceAttributeType[] { FaceAttributeType.Gender, FaceAttributeType.Age, FaceAttributeType.Smile, FaceAttributeType.Emotion, FaceAttributeType.Glasses, FaceAttributeType.Hair }; IList <DetectedFace> faceList; IList <IdentifyResult> identifyResult = new List <IdentifyResult>(); try { faceList = await faceClient.Face.DetectWithStreamAsync(stream(), true, false, recognitionModel : recognitionModel, detectionModel : detectionModel); var faceIds = faceList.Where(face => face.FaceId.HasValue).Select(face => face.FaceId.Value).ToList(); var groupId = await EnsurePersonGroup(personGroupId); if (!string.IsNullOrEmpty(groupId)) { //一次最大10人 for (int i = 0; i < faceIds.Count; i += 10) { var rs = await faceClient.Face.IdentifyAsync(faceIds.Skip(i).Take(10).ToList(), personGroupId); rs.ForEach(r => identifyResult.Add(r)); } } } catch (Exception ex) { throw ex; } foreach (var face in faceList) { var item = new FaceClientResultModel() { FaceID = face.FaceId.ToString(), Name = await GetPersonNameAsync(identifyResult, face.FaceId), ImageData = CaptureImage( stream(), face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height) }; item.Image = ImageSource.FromStream(() => item.ImageData.AsStream()); result.Add(item); } return(result); }
public async Task Training(FaceClientResultModel input) { // TODO:这里先不考虑超出调用限制的问题 //https://docs.azure.cn/zh-cn/cognitive-services/face/face-api-how-to-topics/how-to-add-faces //这是个临时处理 var personList = await faceClient.PersonGroupPerson.ListAsync(personGroupId); Person person = personList.FirstOrDefault(p => p.Name == input.Name); //添加人员,这里需要考虑当前用户是否已经添加用用户了,如果已添加过则在当前用户下添加照片 if (person == null) { person = await faceClient.PersonGroupPerson.CreateAsync(personGroupId, input.Name); } await faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId, input.ImageData.AsStream(), detectionModel : detectionModel); //必须要训练 await faceClient.PersonGroup.TrainAsync(personGroupId); }