一、配置:
1.人脸识别官网:http://ai.baidu.com/
2.API文档地址:http://ai.baidu.com/docs#/Face-Java-SDK/top
二、项目:
1.后端接口:
1.1 pom.xml文件 添加jar包
<!-- Face Java SDK -->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.1.0</version>
</dependency>
1.2 Controller
@Controller
@RequestMapping("/FaceRecognitionApi/facedetect")
public class FacedetectApiController {
@Autowired
private IUserInformationService userInformationService;
// @Autowired
// private ICommonService commonService;
@Autowired
private IConventionService conventionService;
/**
* 人脸检测+注册
* @param file
* @return
* @throws IOException
*/
@RequestMapping(value="/picdetect",method = RequestMethod.POST)
@ResponseBody
public ResResult facedetect(@RequestParam(value = "file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response,FacedetectAddForm form,
UserInfo userInfo,ConventionUser conventionUser) throws IOException{
ResResult res = new ResResult();
if(userInfo == null || StringUtils.isBlank(userInfo.getWechatNum())){
res.setCode(-1);
res.setMsg("微信号不能为空");
res.setRe(new ArrayList<String[]>());
return res;
}else if(form == null || StringUtils.isBlank(form.getUserInfo())){
res.setCode(-1);
res.setMsg("用户资料不能为空");
res.setRe(new ArrayList<String[]>());
return res;
}
else {
UserInfoForm userInfoForm = new UserInfoForm();
userInfoForm.setWechatNum(userInfo.getWechatNum());
UserInfoVO vo = userInformationService.detailsUser(userInfoForm);
if(vo != null ){
res.setCode(-1);
res.setMsg("该用户已注册");
res.setRe(new ArrayList<String[]>());
return res;
}
}
if (file.isEmpty()) {
res.setCode(0);
res.setMsg("上传文件为空");
res.setRe(new ArrayList<String[]>());
return res;
}
ResResult res1 = PicUtil.savePic(request, file);//上传照片到本地
if(res1.getCode()==1){
/**
* 人脸检测
*/
AipFace client = new AipFace(APIConstants.APPID, APIConstants.API_KEY, APIConstants.SERCET_KEY);
HashMap<String, String> options = new HashMap<String, String>();
options.put("max_face_num", "1");//图片人数,默认为1
options.put("face_fields", "qualities");
JSONObject jsonObject = client.detect(res1.getRe().toString(), options);
System.out.println("---------------人脸检测Return:"+jsonObject.toString());
FacedetectReturnVO vo = JsonUtil.parse(jsonObject.toString(), FacedetectReturnVO