英文:
Microsoft Azure Cognitive Services Face API error 403
问题
我有一个Python函数,它接受一个图像路径作为输入。然后上传图像并找到一个人脸,然后仅复制人脸到一个新图像并返回。我有一个包含525张图片的Google Drive文件夹。我计划使用这个函数自动裁剪每张图像并将其重新保存到Google Drive的不同位置。然而,该函数使用OpenCV和Microsoft Azure Cognitive Services Face API来检测和裁剪人脸。
所以昨晚我创建了一个Microsoft Azure账户,并注册了按使用量付费的服务。我创建了一个使用按使用量付费订阅的Face API。无论如何,当我尝试在Python中使用该函数时,我收到了以下错误:
"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"
我访问了该网站,但它问的问题与我和我的应用程序无关。我并不打算在部署的应用程序中使用Azure服务;我只是想自动编辑525张图像以节省时间。
我在代码中做错了什么吗?
def detect_face(image_path):
# 加载图像使用OpenCV
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 将图像转换为二进制数据
_, img_encoded = cv2.imencode('.jpg', image)
# 设置API端点和订阅密钥
url = private_API_endpoint_value
subscription_key = private_key_value
# 为API调用设置标头和参数
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',
}
# 使用图像数据发送API调用
response = requests.post(url, headers=headers, params=params, data=img_encoded.tobytes())
if response.status_code != 200:
print(f"错误:{response.status_code} - {response.text}")
# 检查API调用是否成功
if response.status_code == 200:
# 解析响应并获取人脸矩形坐标
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']
# 裁剪图像只包含人脸并保存为新文件
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 face_path
# 如果API调用不成功,则返回None
return None
英文:
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
得分: 1
请将**'returnFaceId': 'false'**设置为仅使用人脸检测功能而不是人脸识别功能。
以下属性已受到限制 - 人脸 API:识别和验证功能,人脸 ID 属性。
根据资格和使用标准,人脸服务的访问受到限制,以支持我们的负责任人工智能原则。人脸服务仅提供给Microsoft托管的客户和合作伙伴。请使用人脸识别接入表格申请访问权限。有关更多信息,请参阅人脸有限访问页面 - https://learn.microsoft.com/en-us/legal/cognitive-services/computer-vision/limited-access-identity?context=%2Fazure%2Fcognitive-services%2Fcomputer-vision%2Fcontext%2Fcontext
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论