有没有办法阻止面部API人组中添加相同的人吗?问题是谁已经在persongroup用户与不同的名字将他们自己。
下面是我写的方法。它返回将被加入到人团的人GUID。
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;
}
基于您的代码,你正在做以下几点:
PersonGroup
或创建PersonGroupId
如果不存在Detect
方法从人脸API面:如果为0或大于1发现,抛出异常Person
,添加到前面PersonGroup
PersonGroup
Person
如果你想避免创建一个新的Person
如果您已经有同一人的脸的照片,你可以简单地调用该方法Identify
与照片中的一个(其中任何一个,如果你知道这一切都是来自同一人,因为你的代码似乎暗示)。
这一步应该在以下行之前完成:
var person = await _faceServiceClient.CreatePersonAsync(personGroupId, Guid.NewGuid().ToString());
在这里,你可以做这样的事情(我使用最新的NuGet包的人脸API,可here方法:
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;
}
最后一件事:你为什么要抛出异常?你就不能跳过照片以0或大于1周的脸?