带有本地图像的 C# Azure Face API。错误请求例外

jon*_*amm 1 c# azure face-api azure-cognitive-services

昨天我编辑了来自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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

这是第一个带有训练 Bad Exception 的代码示例,该代码可以工作,但是当我执行多个图像时,它不起作用。

            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)
            {

            }
Run Code Online (Sandbox Code Playgroud)

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

var idntifyResuluts = await client.Face.IdentifyAsync(sourceFaceIds, personGroupId);
Run Code Online (Sandbox Code Playgroud)

有人知道解决方案吗?你可以在github上找到漏洞代码

[更新]

我用多个图像修复了第一个例外。图片太大了。

例外情况

Chr*_*ler 5

我无法复制最初的问题,但重要的是要认识到 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);
}
Run Code Online (Sandbox Code Playgroud)

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

Operation returned an invalid status code 'BadRequest'
BadArgument
Only face attributes 'headpose,mask' are supported by detection_03.
Run Code Online (Sandbox Code Playgroud)

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

运行完 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
Run Code Online (Sandbox Code Playgroud)

对于可能出现的问题,请确保您的映像符合此处列出的操作要求: 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”相同
错误代码 错误信息说明
糟糕的争论 请求正文无效。
糟糕的争论 参数 maxNumOfCandidatesReturned 无效。范围为 [1,5]
糟糕的争论 参数confidenceThreshold 无效。范围为 [0, 1]
糟糕的争论 面容 ID 无效。
糟糕的争论 人员组 ID 无效。有效格式为由数字、小写英文字母、“-”、“_”组成的字符串,长度不超过64个字符。
糟糕的争论 大人组ID无效。有效格式为由数字、小写英文字母、“-”、“_”组成的字符串,长度不超过64个字符。
糟糕的争论 “识别模型”不兼容。
PersonGroupIdAndLargePersonGroupIdBothNotNull 大人员组 ID 和人员组 ID 均不为空。
PersonGroupIdAndLargePersonGroupIdBothNull 大人员组 ID 和人员组 ID 均为空。
未找到人员组 未找到人员组。
未找到大人组 未发现大型人群。
找不到人脸 找不到脸。
未受过训练的人员组 人员组未受过培训。
未受过训练的大型团体 大型团体未受过训练。
人员组培训未完成 人员组正在培训中。
大型团体训练未完成 大型团体正在训练中。