PC人脸识别登录,出乎意料的简单
接下来我们开始对图片进行比对,百度云提供了一个在线的人脸库,用户登录我们先在人脸库查询人像是否存在,存在则表示登录成功,如果不存在则注册到人脸库。每个图片有一个唯一标识face_token。 百度人脸识别 API 实现比较简单,需要特别注意参数image_type,它有三种类型 BASE64:图片的base64值,base64编码后的图片数据,编码后的图片大小不超过2M; URL:图片的 URL地址( 可能由于网络等原因导致下载图片时间过长); FACE_TOKEN:人脸图片的唯一标识,调用人脸检测接口时,会为每个人脸图片赋予一个唯一的 FACE_TOKEN,同一张图片多次检测得到的FACE_TOKEN是同一个。 而我们这里使用的是图片BASE64文件,所以image_type要设置成BASE64。 @Override public BaiDuFaceSearchResult faceSearch(String file) { try { byte[] decode = Base64.decode(Base64Util.base64Process(file)); String faceFile = Base64Util.encode(decode); Map<String, Object> map = new HashMap<>(); map.put("image", faceFile); map.put("liveness_control", "NORMAL"); map.put("group_id_list", "user"); map.put("image_type", "BASE64"); map.put("quality_control", "LOW"); String param = GsonUtils.toJson(map); String result = HttpUtil.post(faceSearchUrl, this.getAccessToken(), "application/json", param); BaiDuFaceSearchResult searchResult = JSONObject.parseObject(result, BaiDuFaceSearchResult.class); log.info(" faceSearch: {}", JSON.toJSONString(searchResult)); return searchResult; } catch (Exception e) { log.error("get faceSearch error {}", e.getStackTrace()); e.getStackTrace(); } return null; } @Override public BaiDuFaceDetectResult faceDetect(String file) { try { byte[] decode = Base64.decode(Base64Util.base64Process(file)); String faceFile = Base64Util.encode(decode); Map<String, Object> map = new HashMap<>(); map.put("image", faceFile); map.put("face_field", "faceshape,facetype"); map.put("image_type", "BASE64"); String param = GsonUtils.toJson(map); String result = HttpUtil.post(faceDetectUrl, this.getAccessToken(), "application/json", param); BaiDuFaceDetectResult detectResult = JSONObject.parseObject(result, BaiDuFaceDetectResult.class); log.info(" detectResult: {}", JSON.toJSONString(detectResult)); return detectResult; } catch (Exception e) { log.error("get faceDetect error {}", e.getStackTrace()); e.getStackTrace(); } return null; } @Override public BaiDuFaceAddResult addFace(String file, UserFaceInfo userFaceInfo) { try { byte[] decode = Base64.decode(Base64Util.base64Process(file)); String faceFile = Base64Util.encode(decode); Map<String, Object> map = new HashMap<>(); map.put("image", faceFile); map.put("group_id", "user"); map.put("user_id", userFaceInfo.getUserId()); map.put("user_info", JSON.toJSONString(userFaceInfo)); map.put("liveness_control", "NORMAL"); map.put("image_type", "BASE64"); map.put("quality_control", "LOW"); String param = GsonUtils.toJson(map); String result = HttpUtil.post(addfaceUrl, this.getAccessToken(), "application/json", param); BaiDuFaceAddResult addResult = JSONObject.parseObject(result, BaiDuFaceAddResult.class); log.info("addResult: {}", JSON.toJSONString(addResult)); return addResult; } catch (Exception e) { log.error("get addFace error {}", e.getStackTrace()); e.getStackTrace(); } return null; } 项目是前后端分离的,但为了大家学习方便,我把人脸识别页面整合到了后端项目。 最后 run FireControllerApplication 访问地址:http://localhost:8082/face 即可。
(编辑:应用网_阳江站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |