Microsoft Azure Cognitive Services Face API error 403

1.2k Views Asked by At

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.

1

There are 1 best solutions below

0
Yutong Tie On

Please set 'returnFaceId': 'false' to use only face detection function not face identify function.

The below properties have been limited - Face API: Identify and Verify features, face ID property

Face service access is limited based on eligibility and usage criteria in order to support our Responsible AI principles. Face service is only available to Microsoft managed customers and partners. Use the Face Recognition intake form to apply for access. For more information, see the Face limited access page - https://learn.microsoft.com/en-us/legal/cognitive-services/computer-vision/limited-access-identity?context=%2Fazure%2Fcognitive-services%2Fcomputer-vision%2Fcontext%2Fcontext