Azure计算机视觉API人脸分析未返回年龄,始终显示0?
Azure图像分析API人脸年龄返回0的解决思路
你遇到的问题是因为Azure计算机视觉的AnalyzeImageAsync接口中,Faces视觉特征在新版API(v3.1及以后)里不再返回年龄、性别等人脸属性,仅会返回人脸的边界框信息,所以所有测试图片的年龄字段都显示为0。
解决思路如下:
- 改用Azure人脸API(Face API):这是专门用于人脸属性分析的服务,支持年龄、性别、表情等多种属性识别,是当前推荐的方案。
- (不推荐)如果坚持使用计算机视觉API,需切换到旧版本(如v2.0),但旧版本已进入弃用周期,后续可能停止服务。
人脸API代码示例
首先安装Microsoft.Azure.CognitiveServices.Vision.Face NuGet包,然后参考以下代码:
using Microsoft.Azure.CognitiveServices.Vision.Face; using Microsoft.Azure.CognitiveServices.Vision.Face.Models; // 初始化Face客户端 FaceClient faceClient = new FaceClient(new ApiKeyServiceClientCredentials(faceApiKey)) { Endpoint = faceEndpoint }; // 指定需要分析的人脸属性 List<FaceAttributeType> targetAttributes = new List<FaceAttributeType> { FaceAttributeType.Age, FaceAttributeType.Gender, FaceAttributeType.Emotion }; // 通过URL分析人脸 IList<DetectedFace> detectedFaces = await faceClient.Face.DetectWithUrlAsync(img, returnFaceAttributes: targetAttributes); // 提取年龄信息 foreach (var face in detectedFaces) { Console.WriteLine($"检测到的人脸年龄:{face.FaceAttributes.Age}"); }
注意事项
- 需单独创建Face API资源,它和计算机视觉资源是独立的,拥有各自的密钥和端点。
- 确保图片中人脸清晰、完整,模糊或遮挡严重的图片可能影响识别精度,但不会返回0值。
内容的提问来源于stack exchange,提问作者Wallace B. McClure
相关产品推荐
相关产品推荐

