Pro-n-apps
Pro-n-apps

Reputation: 55

Cognitive Services Exception in Xamarin App

Hi I am using Microsoft Cognitive Services and it's giving me an exception for some unknown reason. This is the way I declared FaceServiceRestClient:

var faceServiceClient = new FaceServiceRestClient("MY_KEY");

when I run above code it throws an exception by saying "Your Subscription key is invalid". I checked 3-4 times Key is correct, I also regenerated key and used it still exception is there.

Now, I added second parameter, thats URL so not my declaration is as follows:

var faceServiceClient = new FaceServiceRestClient("MY_KEY", "https://australiaeast.api.cognitive.microsoft.com/face/v1.0" );

For above statement I get an exception as "Java.Lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=MY_KEY/detect"

This is how I am calling detect method (If you want to see)

Com.Microsoft.Projectoxford.Face.Contract.Face[] result = faceServiceClient.Detect(@params[0], true, false, null);

I do not understand exactly where to look at or which declaration is correct. & BTW this is Xamarin application and I used Xamarin.Microsoft.Cognitive.Face package. If you want to anything else in my code please Comment I will share code snippet. Can anyone please help? Thank you

Upvotes: 1

Views: 101

Answers (1)

SushiHangover
SushiHangover

Reputation: 74164

"Java.Lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=MY_KEY/detect"

That package wraps the Android/Java API and that API is different then the other platforms. In the case of FaceServiceRestClient, the Azure Face Endpoint is the first param, and your Face API key is the second.

Note: They did not even name the parameters in the binding library so you will see param names such as p0 p1 throughout the C# API :-( I ended up using Rekognition to work around the throughput limitations of Cognitive services, but that is a different story)

I stripped down a camera/photo tagger that I wrote to get you started.

Example:

await Task.Run(() =>
{
    var faceServiceClient = new FaceServiceRestClient(faceEndpoint, faceAPIKey);
    using (var imageFileStream = camera.SingleImageStream)
    {
        var faceAttributes = new FaceServiceClientFaceAttributeType[] { FaceServiceClientFaceAttributeType.Gender, FaceServiceClientFaceAttributeType.Age, FaceServiceClientFaceAttributeType.Smile, FaceServiceClientFaceAttributeType.Glasses, FaceServiceClientFaceAttributeType.FacialHair };
        var faces = faceServiceClient.Detect(imageFileStream, true, false, faceAttributes);
        foreach (var face in faces)
        {
            Log.Debug(TAG, $"{face.FaceRectangle.Left}:{face.FaceRectangle.Top}:{face.FaceRectangle.Width}:{face.FaceRectangle.Height}");
            DrawFaceRect(face.FaceRectangle);
            TagPhoto(face.FaceAttributes);
        }
    }
});

Upvotes: 1

Related Questions