如何停止的人脸API人组重新添加同一人

问题描述 投票:0回答:1

有没有办法阻止面部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;
}
c# azure microsoft-cognitive face-recognition face-api
1个回答
1
投票

你做了什么

基于您的代码,你正在做以下几点:

  1. 获取其PersonGroup或创建PersonGroupId如果不存在
  2. 然后在该方法中提供的参数的每个照片: 检测是否存在使用Detect方法从人脸API面:如果为0或大于1发现,抛出异常
  3. 创建一个新的Person,添加到前面PersonGroup
  4. 然后,在方法的参数提供的每个照片:面对加入到创建人
  5. 最后,培养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周的脸?

© www.soinside.com 2019 - 2024. All rights reserved.