Shreya Bhadwal
Shreya Bhadwal

Reputation: 11

Azure Face Service Invalid Request API Error On Running Quickstart Code

I have been trying to use the Azure face service through Python and have copy pasted the code from the official site. (https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/identity-client-library?tabs=visual-studio&pivots=programming-language-python). I have a free student benefits account.

I have updated the key and endpoint and let the image links remain as it is. However, I am getting the following error when I run the code.

Person group: 61ec255a-06db-45dd-ac7e-90c9dfac98b0
Traceback (most recent call last):
  File "f:\College\SEM 6\IOT\azure code\file.py", line 43, in <module>
    face_client.person_group.create(person_group_id=PERSON_GROUP_ID, name=PERSON_GROUP_ID, recognition_model='recognition_04')
  File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\azure\cognitiveservices\vision\face\operations\_person_group_operations.py", line 121, in create
    raise models.APIErrorException(self._deserialize, response)
azure.cognitiveservices.vision.face.models._models_py3.APIErrorException: (InvalidRequest) Invalid request has been sent.

I don't know where I am going wrong. Can someone please point out what's happening here?

Upvotes: 1

Views: 2103

Answers (4)

hasManyStupidQuestions
hasManyStupidQuestions

Reputation: 290

Because of updates to Azure's Responsible AI policy, many features of the Face API are locked down except for Limited Access customers. This includes returning the Face ID that the face service creates and uses internally. See this Microsoft thread.

It's not very clear in the documentation, but the default value of the return_face_id parameter (something that is now Limited Access / locked down) of both the detect_with_stream and detect_with_url methods of the FaceOperations class (which is the face attribute of instances of FaceClient) is True, i.e. by default the SDK sends API calls that try to do restricted operations.

If you change return_face_id to False, you will be able to use the API (provided you don't try to use other locked down / Limited Access features).

TL;DR: Instead of

from azure.cognitiveservices.vision.face import FaceClient
...
face_client = FaceClient(endpoint, credentials) 
detected_faces = face_client.face.detect_with_stream(...)

do (the difference is return_face_id=False)

from azure.cognitiveservices.vision.face import FaceClient
...
face_client = FaceClient(endpoint, credentials) 
detected_faces = face_client.face.detect_with_stream(..., return_face_id=False)

The same also applies for detect_with_url instead of detect_with_stream.

Note: I had the same issue, and copy-pasted above the same answer I gave to a related question (and also this one) that resolved the issue.

Upvotes: 1

holger
holger

Reputation: 975

Like baptistin pointed out, the issue is with the limited access to certain Face API features. While the Cognitive Services SDK does not indicate this in detail, you can use plain REST API to do the same query and get to a more detailed error message. I used PowerShell to invoke the REST API but you could also use Postman, curl - whichever you prefer.

$cognitiveServicesName  = 'yourCognitiveServiceAccountName'
$resourceGroupName      = 'yourResourceGroupName'

$csUri      = (Get-AzCognitiveServicesAccount -Name $cognitiveServicesName -ResourceGroupName $resourceGroupName).Endpoint+'/face/v1.0/detect?returnFaceId=true'
$accessKey  = (Get-AzCognitiveServicesAccountKey -Name $cognitiveServicesName -ResourceGroupName $resourceGroupName).Key1


$headers = @{
    "Content-Type"              = "application/json"
    "Ocp-Apim-Subscription-Key" = $accessKey
}

$body = @{
    url = 'urlToYourImage'
} | ConvertTo-Json

Invoke-RestMethod -Method Post -Uri $csUri -Headers $headers -Body $body | ConvertTo-Json

The response is a little more detailed than what you get when using the SDK:

{
  "error": {
    "code": "InvalidRequest",
    "message": "Invalid request has been sent.",
    "innererror": {
      "code": "UnsupportedFeature",
      "message": "Feature is not supported. Please apply for access at https://aka.ms/facerecognition"
    }
  }
}

Upvotes: 0

baptistin
baptistin

Reputation: 1

Microsoft has limited access to certain features (including the Face API in order to respect their principles of responsible AI) to people who are accredited After filling out their form which you can find on their website.

Upvotes: 0

SureshBabu
SureshBabu

Reputation: 474

  • We tried to reproduce the issue from our end by following the document provided we are able to run the application successfully as shown below:

enter image description here

Verify the below checklist:

Upvotes: 0

Related Questions