我通过它的API使用MS认知服务来分析图像,并注意到当图像处于横向模式(“面部未垂直对齐”)时,它将返回空结果。我很困惑,想知道我是在做错什么,还是MS服务是如何工作的。这是重现该问题的简单示例。
您应该具有MS Face订阅密钥才能使用该服务!
import requests
# If you are using a Jupyter notebook, uncomment the following line.
#%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from matplotlib import patches
from io import BytesIO
# send request to MS, use YOUR subscription key
subscription_key = "982374kwhXXXxxxx"
assert subscription_key
face_api_url ='https://westus.api.cognitive.microsoft.com/face/v1.0/detect'
header = {'Ocp-Apim-Subscription-Key': face_api_url }
headers = {'Ocp-Apim-Subscription-Key': subscription_key, "Content-Type": "application/octet-stream" }
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
}
# get a random image and convert to numpy array
image_url = 'https://how-old.net/Images/faces2/main007.jpg'
image = Image.open(BytesIO(requests.get(image_url).content))
image_np = np.array(image)
这是图片:
然后运行MS Face识别分析:
# save image into a buffer
buf = io.BytesIO()
plt.imsave(buf, image_np, format='jpg')
response = requests.post(face_api_url, params=params, headers=headers, data=buf.getvalue())
faces = response.json()
print(faces)
输出正确:
[{'faceId': '56afb612-f737-44da-8b62-511070527e18', 'faceRectangle': {'top': 209, 'left': 229, 'width': 91, 'height': 91}, 'faceAttributes': {'smile': 1.0, 'headPose': {'pitch': 0.0, 'roll':
3.7, 'yaw': 1.2}, 'gender': 'female', 'age': 29.0, 'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0}, 'glasses': 'NoGlasses', 'emotion': {'anger': 0.0,
'contempt': 0.0,
'disgust': 0.0,
'fear': 0.0,
'happiness': 1.0,
'neutral': 0.0,
'sadness': 0.0,
'surprise': 0.0}, 'blur': {'blurLevel': 'low', 'value': 0.02}, 'exposure': {'exposureLevel': 'goodExposure', 'value': 0.65}, 'noise': {'noiseLevel': 'medium', 'value': 0.31}, 'makeup': {'eyeMakeup': True, 'lipMakeup': True}, 'accessories': [], 'occlusion': {'foreheadOccluded': False,
'eyeOccluded': False,
'mouthOccluded': False}, 'hair': {'bald': 0.05,
'invisible': False,
'hairColor': [{'color': 'brown', 'confidence': 1.0},
{'color': 'blond', 'confidence': 0.48},
{'color': 'black', 'confidence': 0.35},
{'color': 'red', 'confidence': 0.27},
{'color': 'gray', 'confidence': 0.19},
{'color': 'other', 'confidence': 0.03}]}}}, {'faceId': 'dfb667ba-3fe7-42fc-b9ba-06a86619e94d', 'faceRectangle': {'top': 110, 'left': 125, 'width': 76, 'height': 76}, 'faceAttributes': {'smile': 1.0, 'headPose': {'pitch': 0.0, 'roll': 2.3, 'yaw': 2.2}, 'gender': 'male', 'age': 32.0, 'facialHair': {'moustache': 0.4, 'beard': 0.4, 'sideburns': 0.1}, 'glasses': 'NoGlasses', 'emotion': {'anger': 0.0,
'contempt': 0.0,
'disgust': 0.0,
'fear': 0.0,
'happiness': 1.0,
'neutral': 0.0,
'sadness': 0.0,
'surprise': 0.0}, 'blur': {'blurLevel': 'low', 'value': 0.02}, 'exposure': {'exposureLevel': 'goodExposure', 'value': 0.72}, 'noise': {'noiseLevel': 'low', 'value': 0.01}, 'makeup': {'eyeMakeup': False, 'lipMakeup': True}, 'accessories': [], 'occlusion': {'foreheadOccluded': False,
'eyeOccluded': False,
'mouthOccluded': False}, 'hair': {'bald': 0.02,
'invisible': False,
'hairColor': [{'color': 'brown', 'confidence': 1.0},
{'color': 'blond', 'confidence': 0.92},
{'color': 'red', 'confidence': 0.66},
{'color': 'gray', 'confidence': 0.25},
{'color': 'other', 'confidence': 0.02},
{'color': 'black', 'confidence': 0.01}]}}}]
但是,如果我交换轴(对图像进行转置),结果将为零(“空”)
# save image into a buffer
buf = io.BytesIO()
plt.imsave(buf, np.swapaxes(image_np, 0,1), format='jpg')
response = requests.post(face_api_url, params=params, headers=headers, data=buf.getvalue())
faces = response.json()
print(faces)
结果将是一个空列表:
[]
我想知道是否有人遇到了这个问题,以及如何使MS服务正常工作,而不管图像的旋转或位置如何。
答案 0 :(得分:2)
它实际上指出here,在某些情况下可能无法检测到面部,包括错误的图像方向。
面部检测器更喜欢正面和近正面。有情况 可能无法检测到脸部极大的脸角 (头位)或被遮挡,或图像方向错误。
您可以改为检测侧面的方向,然后根据需要旋转图片,然后发送!