Microsoft Azure Cognitive Services Face API error 403

Brandon Boone 31 Reputation points
2023-03-07T00:02:05.8266667+00:00

I have a python function that takes in a path to an image. It then uploads the image and finds a human face, then copy only the human face to a new image that it returns. I have a google drive folder with 525 Images. I planned to use this function to automatically crop each image and resave it to a different location in my Google Drive. However, the function uses OpenCV and the Microsoft Azure Cognitive Services Face API to detect and crop human faces.

So I made an Microsoft Azure account last night, and I signed up for pay-as-you-go. I made a Face API that uses a pay as you go subscription I made. Anyhow, when I try and use the function in Python I get this error:

"code": "InvalidRequest",

"message": "Invalid request has been sent.",

"innererror":

"code": "UnsupportedFeature",

"message": "Feature is not supported, missing approval for one or more of the following features: Identification, Verification. Please apply for access at https://aka.ms/facerecognition"

I went to the site, but it is asking questions that are not relevant to me nor my application. I am not trying to use the azure service in an application that I will be deploying; I merely want to automatically edit 525 images to save time.

I am doing something wrong in the code ?

def detect_face(image_path):
# Load image using OpenCV
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Convert image to binary data
_, img_encoded = cv2.imencode('.jpg', image)

# Set API endpoint and subscription key
url = private_API_endpoint_value 
subscription_key = private_key_value

# Set headers and parameters for API call
headers = {
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': subscription_key
}
params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,emotion,facialHair,glasses,hair,makeup,occlusion,smile',

}

# Send API call with image data
response = requests.post(url, headers=headers, params=params, data=img_encoded.tobytes())
if response.status_code != 200:
  print(f"Error: {response.status_code} - {response.text}")

# Check if API call was successful
if response.status_code == 200:
    # Parse response and get face rectangle coordinates
    data = json.loads(response.text)
    if data:
        face_rect = data[0]['faceRectangle']
        x, y, w, h = face_rect['left'], face_rect['top'], face_rect['width'], face_rect['height']

        # Crop image to just the face and save as new file
        face_image = image[y:y+h, x:x+w]
        face_path = os.path.splitext(image_path)[0] + "_face.jpg"
        cv2.imwrite(face_path, face_image)

        # Return path to new face image
        return face_path

# If API call was unsuccessful, return None
return None

I think my application is fairly simple, so I really shouldn't have to request access from Microsoft. I mean this is like a one-time use application.

So I feel like I'm setting up something wrong are doing something incorrectly.

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,619 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 48,911 Reputation points Microsoft Employee Moderator
    2023-03-08T06:47:48.23+00:00

    Brandon Boone I don't think the issue here is with the code that is being used, it is rather the access to Face API needs to be approved since you are requesting sensitive data to be returned in your response. MS as part of responsible AI policy rolled requests all new users to submit the form to gain access to the face API for scenarios where it deems the data might be sensitive. If you were an existing user of face API prior to June 2022, the access would have been available for a year i.e. June 2023 and for continued access existing users should also submit their form for continued access. Please see the announcement of the same on this page for more details. I hope this helps!!

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.