如何停止在FaceAPI人员组中再次添加同一人

时间:2019-02-07 10:36:06

标签: c# azure microsoft-cognitive face-recognition face-api

是否有办法停止在FaceAPI人员组中添加相同的人员? 问题是已经在人员组中的用户正在使用不同的名称添加自己。

下面是我写的方法。它返回正在添加到人员组的人员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;
}

1 个答案:

答案 0 :(得分:1)

你做了什么

根据您的代码,您正在执行以下操作:

  1. 通过PersonGroup获取PersonGroupId或创建Detect 现有
  2. 然后为方法参数中提供的每张照片:
    • 使用Face API中的Person方法检测人脸:如果发现0个或1个以上,则抛出异常
  3. 创建一个新的PersonGroup,添加到前一个PersonGroup
  4. 然后为方法参数中提供的每张照片:将面部添加到创建的Person
  5. 最后,训练Person

避免为现有人员创建新的Person

如果您想避免创建新的Identify(如果您已经拥有同一个人的脸部照片),则只需使用其中一张照片(其中任何一张)调用方法var person = await _faceServiceClient.CreatePersonAsync(personGroupId, Guid.NewGuid().ToString()); 如果您知道所有代码都来自同一个人,则如代码所示)。

此步骤应在以下行之前完成:

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;
}

在这里您可以执行以下操作(我使用了here中提供的最新的Faceface API Nuget软件包中的方法:

Ctrl-C

最后一件事:为什么要引发异常?您不能只跳过0张或多张1张脸的照片吗?