【问题标题】:Microsoft ProjectOxford Face API Error: System.UriFormatExceptionMicrosoft ProjectOxford 人脸 API 错误:System.UriFormatException
【发布时间】:2019-05-23 22:40:06
【问题描述】:

当我在控制台中打印以下错误时,我正在关注 this documentation 在 Visual Studio 中使用 Microsoft Face API 来识别图像中的人脸:

将 Person 添加到“Microsoft.ProjectOxford.Face.FaceAPIException”类型的组异常时出错。

调用以下将人员添加到现有人员组的函数时会打印异常:

public async void AddPersonToGroup(string personGroupId, string name, string pathImage){
    try{
        await faceServiceClient.GetPersonGroupAsync(personGroupId);
        CreatePersonResult person = await faceServiceClient.CreatePersonAsync(personGroupId, name);

        foreach (var imgPath in Directory.GetFiles(pathImage, "*.jpg")) {
            using (Stream s = File.OpenRead(imgPath)) {
                await faceServiceClient.AddPersonFaceAsync(personGroupId, person.PersonId, s);
            }
        }
    } catch (Exception ex){
        //Below is where the error was printed.
        Console.WriteLine("Error adding Person to Group " + ex.Message);
    }
}

这就是我在 main 方法中调用 AddPersonToGroup 的方式:

new Program().AddPersonToGroup("actor", "Tom Cruise", @"C:\Users\ishaa\Documents\Face_Pictures\Tom_Cruise\");

我尝试在 Google 中搜索此错误并遇到 this SO question,但该答案对我不起作用。 (他们的答案是为FaceServiceClient 构造函数传递订阅密钥和端点。)

谁能提供有关此错误发生原因的任何见解?我一直无法弄清楚是什么原因造成的,但我相信它可能与 await faceServiceClient.GetPersonGroupAsync(personGroupId);。我还读到这可能是由于我选择了认知服务定价计划。然而,我正在使用的免费的允许每分钟 20 次交易,我只是想为 3 个不同的人添加 9 张图片。

【问题讨论】:

  • 检查您尝试创建的内容是否已经存在
  • 感谢尼科西的反馈。这是我第一次使用 Microsoft Azure。有没有机会,您知道我如何在 Azure 门户中查看现有的人员组吗?我创建了一个名为 Facial Recognition 的资源,我用它来获取密钥。是否有一个部分可以让我查看该信息,因为Activity log 似乎不包含它?
  • 为了帮助你,我们需要知道抛出了什么样的异常。当您捕获异常并获取更多详细信息时,您可以看看吗?放一个断点,看看你的ex对象
  • @NicolasR 感谢其他问题的帮助。我能够通过对函数进行一些更改来使其工作。我也意识到可能是我打了太多的电话,但现在我终于明白了如何有效地使用每个功能。

标签: c# visual-studio exception microsoft-cognitive face-api


【解决方案1】:

我能够通过使用下面的新功能找到问题的解决方案,该功能创建一个人组,向其中添加人员,然后输入图像:

public async void AddPersonToGroup(string personGroupId, string name, string pathImage){
    //Create a Person Group called actors.
    await faceServiceClient.CreatePersonGroupAsync("actors, "Famous Actors");

    //Create a person and assign them to a Person Group called "actors"
    CreatePersonResult friend1 = await faceServiceClient.CreatePersonAsync("actors", "Tom Cruise");

    //Get the directory with all the images of the person.
    const string friend1ImageDir = @"C:\Users\ishaa\Documents\Face_Recognition_Pictures\Tom_Cruise\";
    foreach (string imagePath in Directory.GetFiles(friend1ImageDir, "*.jpg")){
        using (Stream s = File.OpenRead(imagePath)){
           try{
               //Add the faces for the person.
               await faceServiceClient.AddPersonFaceAsync("actors", friend1.PersonId, s);
           } catch (Exception e){
               Console.WriteLine(e.Message);
           }
        }
    }
}

上面的以下代码在创建人员组、创建人员和为人员添加图像方面对我有用。我认为最初的错误可能有两个原因:

  1. await faceServiceClient.GetPersonGroupAsync(personGroupId); 可能是一个问题。现在我没有使用它,代码适用于faceServiceClient.CreatePersonGroupAsync
  2. 每分钟通话次数过多。我目前正在使用每分钟 20 笔交易的免费计划。我没有意识到的是,每次运行代码时,我都会使用 API 进行多次调用,因为我正在调用用于创建人员组、添加人员、为人员添加图像、训练人员组的函数,然后识别一个人。我认为这是主要错误。

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2017-08-10
    • 1970-01-01
    • 2018-01-06
    • 2016-08-09
    • 2018-06-01
    • 2018-12-31
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多