【问题标题】:C# Azure Face API with local images. Bad Request exeption带有本地图像的 C# Azure Face API。错误请求异常
【发布时间】:2021-09-28 13:15:54
【问题描述】:

昨天我编辑了来自Microsoft Azure Face API Quickstart的代码,我可以在本地图像中识别人。

当我用多个图像训练一个组时,我得到了一个错误的请求异常,并且在检查图像之前我得到了一个错误的请求

 Dictionary<string, string[]> personDictionary = new Dictionary<string, string[]>
         {
             { "Jonas", new[] {"jonasTrain2.jpg"}},
             {"Thomas", new [] {"thomasTrain1.jpg"}}
         };
        string sourceImageFilePath = @"C:\Users\jonas\Desktop\FAces\Test\jonasTest1.jpg";

        Console.WriteLine($"Create a person group ({personGroupId}).");
        await client.PersonGroup.CreateAsync(personGroupId, personGroupId, recognitionModel: recognitionModel);

        foreach (var groupedFace in personDictionary.Keys)
        {
            await Task.Delay(250);
            Person person = await client.PersonGroupPerson.CreateAsync(personGroupId: personGroupId, name: groupedFace);
            Console.WriteLine($"Create a person group person '{groupedFace}'");

            foreach (var similarImage in personDictionary[groupedFace])
            {
                Console.WriteLine($"Add face to the person group person ({groupedFace}) from image `{similarImage}`");
                FileStream fs = File.OpenRead(TRAIN_PATH + @"\" + similarImage);
                PersistedFace face = await client.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId,
                    fs, similarImage);
            }
        }

这是第一个带有训练 Bad Exeption 的代码示例,此代码可以工作,但是当我处理多个图像时,它就不起作用了。

            Console.WriteLine();
        List<Guid> sourceFaceIds = new List<Guid>();

        List<DetectedFace> detectedFaces = await DetectFaceRecognize(client, sourceImageFilePath, recognitionModel);

        foreach (var detectedFace in detectedFaces) sourceFaceIds.Add(detectedFace.FaceId.Value);

        var idntifyResuluts = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId);

        foreach (var identifyResult in idntifyResuluts)
        {
            try
            {
                Person person = await client.PersonGroupPerson.GetAsync(personGroupId, identifyResult.Candidates[0].PersonId);
                Console.WriteLine($"Person '{person.Name}' is identified for face in: {Path.GetFileName(sourceImageFilePath)} - {identifyResult.FaceId}," +
                    $" confidence: {identifyResult.Candidates[0].Confidence}");
            }
            catch (Exception)
            {

            }

这是我在这一行中得到异常的第二个代码示例:

var idntifyResuluts = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId);

有人知道解决方案吗? 您可以在github找到孔代码

[更新]

我用多个图像修复了第一个异常。图片太大了。

Exeption

【问题讨论】:

  • 这里有很多代码,但很难准确理解你卡在哪里。哪一行代码失败了,您是否捕获了发送的原始有效负载,以便您可以通过 PostMan 等工具进行验证?
  • @ChrisSchaller 更新我解决了第一个问题(Azure 不喜欢 4k 图片)我在第 86 行 var idntifyResuluts = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId);
  • @ChrisSchaller 试图做邮递员的事情,但我没有找到关于它的教程
  • 您应该捕获异常并将详细信息粘贴到您的帖子的更新中。 Azure 服务和 SDK 通常会在响应中提供足够的详细信息,帮助您确定解决方案
  • @ChrisSchaller 谢谢你的意见我做了那个更新

标签: c# azure azure-cognitive-services face-api


【解决方案1】:

我无法复制原始问题,但重要的是要认识到 API 返回嵌入在响应正文中的丰富信息。下面的 try-catch 块演示了如何解析异常:

try
{
    await DetectFaceRecognize(client, Path.Join(TRAIN_PATH, "jack.jpg"), RECOGNITION_MODEL4);
    await IdentifyPersonGroup(client, RECOGNITION_MODEL4);
}
catch(Microsoft.Azure.CognitiveServices.Vision.Face.Models.APIErrorException appX)
{
    Console.WriteLine(appX.Message);
    Console.WriteLine(appX.Body.Error.Code);
    Console.WriteLine(appX.Body.Error.Message);
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

当我使用无效参数运行它时,它捕获了以下消息:

Operation returned an invalid status code 'BadRequest'
BadArgument
Only face attributes 'headpose,mask' are supported by detection_03.

您提到您发现问题是文件大小,嵌入的错误消息将包含此信息。

在运行了GitHub上发布的代码后,我无法用自己的本地图像复制错误情况,这表明可能是问题所在,代码本身是API使用的简单演示。

使用 MS 库存图片后,我收到了 4 口之家的以下输出:

Person 'dad' is identified for face in: family.jpg - 2f4a8d5b-416e-4985-9c94-cd2ae07dce91, confidence: 0.96725
Person 'mum' is identified for face in: family.jpg - 11ccd00a-a1af-4dfb-a803-359b6bd1df8e, confidence: 0.96921
Person 'daughter' is identified for face in: family.jpg - 62e6d513-4f8a-4634-a1d1-8dfd68b45c8c, confidence: 0.90712
Person 'son' is identified for face in: family.jpg - 078abaae-501d-496c-85b9-3a6dc26d1a41, confidence: 0.92886

对于可能出现的问题,请确保您的图片符合此处列出的操作要求:https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

人脸 - 识别

一对多识别,以从人员组或大型人员组中找到与特定查询人脸最接近的匹配项。

对于 faceIds 数组中的每个人脸,人脸识别将计算查询人脸与人组(由 personGroupId 给出)或大型人组(由 largePersonGroupId 给出)中的所有人脸之间的相似度,并返回候选人对于按相似度置信度排序的那张脸。应培训人员组/大型人员组,使其准备好进行识别。在 PersonGroup - Train 和 LargePersonGroup - Train 中查看更多信息。

备注:

  • 该算法允许在同一个请求中独立识别多个人脸,但不能超过 10 个人脸。
  • 人物组/大人物组中的每个人都可以有多个面孔,但不能超过 248 个面孔。
  • 更高的人脸图像质量意味着更好的识别精度。请考虑高质量的人脸:正面、清晰,人脸大小为 200x200 像素(两眼之间 100 像素)或更大。
  • 返回的候选人数受 maxNumOfCandidatesReturned 和 confidenceThreshold 限制。如果没有识别出任何人,则返回的候选人将是一个空数组。
  • 当您需要从面孔列表/大型面孔列表而不是人员组/大型人员组中查找相似面孔时,请尝试面孔 - 查找相似面孔。
  • 与查询人脸的 faceId 关联的“recognitionModel”应与目标人群或大型人群使用的“recognitionModel”相同
Error Code Error Message Description
BadArgument Invalid request body.
BadArgument The argument maxNumOfCandidatesReturned is not valid. Range is [1,5]
BadArgument The argument confidenceThreshold is not valid. Range is [0, 1]
BadArgument Face ID is invalid.
BadArgument Person group ID is invalid. Valid format should be a string composed by numbers, English letters in lower case, '-', '_', and no longer than 64 characters.
BadArgument Large person group ID is invalid. Valid format should be a string composed by numbers, English letters in lower case, '-', '_', and no longer than 64 characters.
BadArgument 'recognitionModel' is incompatible.
PersonGroupIdAndLargePersonGroupIdBothNotNull Large person group ID and person group ID are both not null.
PersonGroupIdAndLargePersonGroupIdBothNull Large person group ID and person group ID are both null.
PersonGroupNotFound Person group is not found.
LargePersonGroupNotFound Large person group is not found.
FaceNotFound Face is not found.
PersonGroupNotTrained Person group not trained.
LargePersonGroupNotTrained Large person group not trained.
PersonGroupTrainingNotFinished Person group is under training.
LargePersonGroupTrainingNotFinished Large person group is under training.

【讨论】:

    【解决方案2】:

    我找到了解决所有问题的方法。我可以使用验证代替识别它的代码有点多,但更容易

    public static async Task Verify(IFaceClient client, string recognitonModel)
        {
            Console.WriteLine("====VERIFY====\n");
    
            List<string> targetImageFileNames = new List<string> { "jonasTrain5.jpg" };
            string sourceImageFileName1 = "identification1.jpg";
    
            List<Guid> targetFaceIds = new List<Guid>();
            foreach (var imageFileName in targetImageFileNames)
            {
                List<DetectedFace> detectedFaces = await DetectFaceRecognize(client, trainingPath + imageFileName, recognitonModel);
                targetFaceIds.Add(detectedFaces[0].FaceId.Value);
            }
    
            List<DetectedFace> detectedFaces1 = await DetectFaceRecognize(client, testingPath + sourceImageFileName1, recognitonModel);
            Guid sourceFaceId1 = detectedFaces1[0].FaceId.Value;
            foreach (var detectedFace in detectedFaces1)
            {
                VerifyResult verifyResult1 = await client.Face.VerifyFaceToFaceAsync((Guid)detectedFace.FaceId, targetFaceIds[0]);
                Console.WriteLine(
                    verifyResult1.IsIdentical
                        ? $"Faces from {sourceImageFileName1} & {targetImageFileNames[0]} are of the same (Positive) person, similarity confidence: {verifyResult1.Confidence}."
                        : $"Faces from {sourceImageFileName1} & {targetImageFileNames[0]} are of different (Negative) persons, similarity confidence: {verifyResult1.Confidence}.");
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 2020-04-16
      • 2017-10-02
      • 1970-01-01
      相关资源
      最近更新 更多