> 文章列表 > 百度云人脸检测

百度云人脸检测

百度云人脸检测

百度云人脸检测

    • 接口能力
    • 业务应用
    • 质量检测
    • Code demonstration

接口能力

  • 人脸检测:检测图片的人脸并标记出位置信息。
  • 人脸关键点:展示人脸的核心关键点信息,及150个关键点信息。
  • 人脸属性值:展示人脸属性信息,如年龄、性别等。
  • 人脸质量信息:返回人脸各部分的遮挡、光照、模糊、完整度、置信度等信息。

百度云智能官方API文档

业务应用

典型应用场景:如人脸属性分析,基于人脸关键点的加工分析,人脸营销活动等。

质量检测

百度云人脸检测

Code demonstration

仅供参考

/*** 人脸检测* @param imageType 图片类型* @param image 图片* @throws Exception 异常*/
public static void detect(ImageType imageType, String image) throws Exception {AipFace client = new AipFace(KDZS_APP_ID, KDZS_APP_KEY, KDZS_APP_SECRET);// 传入可选参数调用接口HashMap<String, Object> options = new HashMap<String, Object>();options.put("max_face_num", "1");options.put("face_field", "glasses,quality,eye_status,face_type");/*** 人脸的类型* LIVE表示生活照:通常为手机、相机拍摄的人像图片、或从网络获取的人像图片等* IDCARD表示身份证芯片照:二代身份证内置芯片中的人像照片* WATERMARK表示带水印证件照:一般为带水印的小图,如公安网小图* CERT表示证件照片:如拍摄的身份证、工卡、护照、学生证等证件图片* 默认LIVE*/options.put("face_type", "LIVE");options.put("liveness_control", "LOW");// 人脸检测JSONObject ret = client.detect(image, imageType.toString(), options);log.info("人脸检测结果: {}", ret.toString(2));// 解析结果int errorCode = ret.getInt("error_code");if (errorCode == 222202) {throw new BusinessException("照片未找到人脸");} else if (errorCode != 0) {throw new BusinessException(ret.getString("error_msg"));}JSONObject result = ret.getJSONObject("result");// 人脸个数int faceNum = result.getInt("face_num");DomainUtils.throwException(faceNum > 1, "检测出多个人脸");DomainUtils.throwException(faceNum < 1, "未检测到人脸信息");// 获取人脸result = result.getJSONArray("face_list").getJSONObject(0);// 真实人脸/卡通人脸 human: 真实人脸 cartoon: 卡通人脸DomainUtils.throwException("cartoon".equals(result.getJSONObject("face_type").getString("type")), "未检测到真实人脸");// 人脸置信度,范围【0~1】,代表这是一张人脸的概率,0最小、1最大double faceProbability = result.getDouble("face_probability");DomainUtils.throwException(faceProbability < 0.8, "人脸置信度较低");// 人脸旋转角度参数JSONObject angle = result.getJSONObject("angle");double roll = angle.getDouble("roll");double pitch = angle.getDouble("pitch");double yaw = angle.getDouble("yaw");DomainUtils.throwException(roll > 20 || pitch > 20 || yaw > 20, "姿态角度不正确, 请调正拍摄角度");// 是否带眼镜 none:无眼镜,common:普通眼镜,sun:墨镜DomainUtils.throwException("sun".equals(result.getJSONObject("glasses").getString("type")), "请摘掉墨镜拍照");//  双眼状态JSONObject eyeStatus = result.getJSONObject("eye_status");double leftEye = eyeStatus.getDouble("left_eye");double rightEye = eyeStatus.getDouble("right_eye");DomainUtils.throwException(leftEye < 0.2 || rightEye < 0.2, "眼睛睁开度较小");// 遮挡JSONObject quality = result.getJSONObject("quality");JSONObject occlusion = quality.getJSONObject("occlusion");DomainUtils.throwException(occlusion.getDouble("left_eye") > 0.2, "左眼被遮挡");DomainUtils.throwException(occlusion.getDouble("right_eye") > 0.2, "右眼被遮挡");DomainUtils.throwException(occlusion.getDouble("nose") > 0.2, "鼻子被遮挡");DomainUtils.throwException(occlusion.getDouble("mouth") > 0.2, "嘴巴被遮挡");DomainUtils.throwException(occlusion.getDouble("left_cheek") > 0.2, "左脸颊被遮挡");DomainUtils.throwException(occlusion.getDouble("right_cheek") > 0.2, "右脸颊被遮挡");DomainUtils.throwException(occlusion.getDouble("chin_contour") > 0.2, "下巴被遮挡");// 人脸模糊程度DomainUtils.throwException(quality.getDouble("blur") > 0.7, "照片比较模糊");// 脸部区域的光照程度DomainUtils.throwException(quality.getDouble("illumination") < 40, "光线较弱, 请选择好的光线位置进行拍摄");// 人脸完整度DomainUtils.throwException(quality.getInt("completeness") == 0, "人脸超出照片");
}/*** 图片类型*/
public enum ImageType {URL,BASE64;
}