Search code examples
c#visual-studioexceptionazure-cognitive-servicesface-api

Microsoft ProjectOxford Face API Error: System.UriFormatException


I was following this documentation for using the Microsoft Face API to identify faces in an image in Visual Studio when I got the following error printed in the Console:

Error adding Person to Group Exception of type 'Microsoft.ProjectOxford.Face.FaceAPIException' was thrown.

The exception is printed when the following function to add a person to an existing person group is called:

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

This is how I am calling AddPersonToGroup in the main method:

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

I tried searching up this error in Google and came across this SO question, but that answer did not work for me. (Their answer was to pass in the subscription key and endpoint for the FaceServiceClient constructor.)

Would anyone be able to provide any insight into why this error is occurring? I have been unable to figure out what is causing it, but I believe it may have to do with await faceServiceClient.GetPersonGroupAsync(personGroupId);. I also read that it may be due to the Cognitive Services pricing plan I have chosen. However, the free one which I am using allows for 20 transactions per minute and I am only trying to add 9 pictures for 3 different people.


Solution

  • I was able to find a solution to the problem by using the new function below which creates a Person Group, adds people to it, and enters images:

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

    The following code above has worked for me in creating the Person Group, creating the People, and adding images for the people. I believe that there may be two causes of the initial error:

    1. await faceServiceClient.GetPersonGroupAsync(personGroupId); could have been a possible issue. Now that I am not using it the code works with faceServiceClient.CreatePersonGroupAsync.
    2. Too many calls per minute. I am currently using the free plan with 20 transactions per minute. What I did not realize was that each time I ran the code, I was making multiple calls with the API because I was calling the functions for creating the Person Group, adding People, adding images for the People, training the Person Group, and then identifying a Person. I believe that this is the main error.