C# 如何停止在FaceAPI persongroup中再次添加同一个人
有没有办法停止在FaceAPI persongroup中添加同一个人? 问题是已经在persongroup中的用户正在使用不同的名称添加自己 下面是我写的方法。它返回要添加到人员组的人员GUIDC# 如何停止在FaceAPI persongroup中再次添加同一个人,c#,azure,microsoft-cognitive,face-recognition,face-api,C#,Azure,Microsoft Cognitive,Face Recognition,Face Api,有没有办法停止在FaceAPI persongroup中添加同一个人? 问题是已经在persongroup中的用户正在使用不同的名称添加自己 下面是我写的方法。它返回要添加到人员组的人员GUID public async Task<Guid> Register(IEnumerable<MediaFile> photos) { var personGroupId = "XYXNXNX" var allPersonGroups = await _faceSe
public async Task<Guid> Register(IEnumerable<MediaFile> photos)
{
var personGroupId = "XYXNXNX"
var allPersonGroups = await _faceServiceClient.ListPersonGroupsAsync();
if (allPersonGroups?.Any(x => x.PersonGroupId == personGroupId) == false)
{
await _faceServiceClient.CreatePersonGroupAsync(personGroupId, "HFFGFGFD"); // creating a new person group if not exits.
}
foreach (var photo in photos)
{
using (var stream = photo.GetStream())
{
var faces = await _faceServiceClient.DetectAsync(stream);
if (faces?.Length == 0)
{
throw new CustomException(_translatorService.NoFaceFound);
}
if (faces?.Length > 1)
{
throw new CustomException(_translatorService.MultipleFacesFound);
}
}
}
var person = await _faceServiceClient.CreatePersonAsync(personGroupId, Guid.NewGuid().ToString());
foreach (var photo in photos)
{
await _faceServiceClient.AddPersonFaceInPersonGroupAsync(personGroupId, person.PersonId, photo.GetStream());
}
await _faceServiceClient.TrainPersonGroupAsync(personGroupId);
return person.PersonId;
}
公共异步任务寄存器(IEnumerable照片)
{
var personGroupId=“xyxnx”
var allPersonGroups=await _faceServiceClient.ListPersonGroupsAsync();
if(allPersonGroups?.Any(x=>x.PersonGroupId==PersonGroupId)==false)
{
wait _faceServiceClient.CreatePersonGroupAsync(personGroupId,“hffgffd”);//如果不存在,则创建一个新的人员组。
}
foreach(照片中的var照片)
{
使用(var stream=photo.GetStream())
{
var faces=wait\u faceServiceClient.DetectAsync(流);
如果(面?.Length==0)
{
抛出新的CustomException(_translatorService.NoFaceFound);
}
如果(面?长度>1)
{
抛出新的CustomException(_translatorService.MultipleFacesFound);
}
}
}
var person=wait_faceServiceClient.createPersonalsync(personGroupId,Guid.NewGuid().ToString());
foreach(照片中的var照片)
{
wait _faceServiceClient.AddPersonFaceInPersonGroupAsync(personGroupId,person.PersonId,photo.GetStream());
}
wait_faceServiceClient.TrainPersonGroupAsync(personGroupId);
返回person.PersonId;
}
你做了什么
根据您的代码,您正在执行以下操作:
PersonGroupId
获取一个PersonGroupId
,否则创建
现存的- 使用Face API中的
方法检测是否存在面:如果发现0个或多个面,则引发异常Detect
人员
,添加到以前的人员组
人员组
人员
如果您想避免创建一个新的人物
,如果您已经有了同一个人的面部照片,您可以简单地调用方法identification
,使用其中一张照片(如果您知道所有照片都来自同一个人,就像您的代码所建议的那样)
此步骤应在以下行之前完成:
var person = await _faceServiceClient.CreatePersonAsync(personGroupId, Guid.NewGuid().ToString());
在这里,您可以执行类似的操作(我使用了最新的Nuget Face API包中的方法,该包提供:
公共异步任务寄存器(IEnumerable照片)
{
var personGroupId=“xyxnx”;
var allPersonGroups=await_faceClient.persongroups.ListAsync();
if(allPersonGroups?.Any(x=>x.PersonGroupId==PersonGroupId)==false)
{
等待_faceClient.PersonGroup.CreateAsync(personGroupId,“hffgffd”);//如果不退出,则创建一个新的人员组。
}
var facesIdFromPhotos=新列表();
foreach(照片中的var照片)
{
使用(var stream=photo.GetStream())
{
var faces=await\u faceClient.Face.DetectWithStreamAsync(流);
如果(面?.Length==0)
{
抛出新异常(“NoFaceFound”);
}
如果(面?长度>1)
{
抛出新异常(“MultipleFacesFound”);
}
facesIdFromPhotos.Add(((Microsoft.Azure.CognitiveServices.Vision.Face.Models.DetectedFace)faces[0]).FaceId);
}
}
//检查相似性,从以前检测到的人脸中选择一个人脸
var similarityPerson=wait _faceClient.Face.identifAsync(facesIdFromPhotos.Take(1.ToList(),personGroupId);
Guid targetPersonId;
如果(similarityPerson[0]。候选人?.Count>0)
{
targetPersonId=similarityPerson[0]。候选人[0]。PersonId;
}
其他的
{
var createdPerson=await _faceClient.PersonGroupPerson.CreateAsync(personGroupId,Guid.NewGuid().ToString());
targetPersonId=createdPerson.PersonId;
}
//将面添加到人物(已存在或不存在)
foreach(照片中的var照片)
{
wait _faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId,targetPersonId,photo.GetStream());
}
wait_faceClient.PersonGroup.trainanc(personGroupId);
返回targetPersonId;
}
最后一件事:你为什么要抛出异常?你不能跳过0张或多张脸的照片吗?使用这种方法,我无法添加与已注册的人相似的人。IdentifySync在我尝试注册相似的人时返回已注册的人。当然,你可以使用
identification
result中的置信度得分。如果新人相似但不是同一个人,您只需使用足够高的置信度得分,您将低于该值
public async Task<Guid> Register(IEnumerable<MediaFile> photos)
{
var personGroupId = "XYXNXNX";
var allPersonGroups = await _faceClient.PersonGroup.ListAsync();
if (allPersonGroups?.Any(x => x.PersonGroupId == personGroupId) == false)
{
await _faceClient.PersonGroup.CreateAsync(personGroupId, "HFFGFGFD"); // creating a new person group if not exits.
}
var facesIdFromPhotos = new List<Guid>();
foreach (var photo in photos)
{
using (var stream = photo.GetStream())
{
var faces = await _faceClient.Face.DetectWithStreamAsync(stream);
if (faces?.Length == 0)
{
throw new Exception("NoFaceFound");
}
if (faces?.Length > 1)
{
throw new Exception("MultipleFacesFound");
}
facesIdFromPhotos.Add(((Microsoft.Azure.CognitiveServices.Vision.Face.Models.DetectedFace)faces[0]).FaceId);
}
}
// Check similarity, with 1 face from the previous detected faces
var similarityPerson = await _faceClient.Face.IdentifyAsync(facesIdFromPhotos.Take(1).ToList(), personGroupId);
Guid targetPersonId;
if (similarityPerson[0].Candidates?.Count > 0)
{
targetPersonId = similarityPerson[0].Candidates[0].PersonId;
}
else
{
var createdPerson = await _faceClient.PersonGroupPerson.CreateAsync(personGroupId, Guid.NewGuid().ToString());
targetPersonId = createdPerson.PersonId;
}
// Add faces to Person (already existing or not)
foreach (var photo in photos)
{
await _faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, targetPersonId, photo.GetStream());
}
await _faceClient.PersonGroup.TrainAsync(personGroupId);
return targetPersonId;
}