有没有办法停止在 FaceAPI 人员组中添加同一个人?问题是已经在 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;
}