Springboot to achieve Baidu AI face recognition comparison
Step one is the front end
Because we use the framework of front and background separation, so, the foreground as long as the realization of vUE video photography, and then take photos to obtain, base64-bit form can be passed to the background interface front-end code can refer to blog.csdn.net/weixin_4364…
The second step is background implementation
First introduce baidu API SDK
<dependency> <groupId>com.baidu.aip</groupId> <artifactId> Java - SDK </artifactId> <version>4.15.1</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> </exclusion> </exclusions> </dependency>Copy the code
Utility class
Obtain the picture data and compare it with the pictures used in the database first (input the user’s photo data in advance and take photos for comparison later)
/ * * * * FileUtil, face recognition Base64Util, HttpUtil, GsonUtils from * * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72, please https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3 Download * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3 * * / @ PostMapping ("/faceMatch ") public RestResponse faceMatch(@RequestBody Sign sign) { HashMap map=new HashMap(); If (stringutils.isblank (sign.getCode())) {return restResponse.fail (2," id number cannot be empty "); } Sign sign1=userService.selectSign(sign.getCode()); If (sign1==null) {return restResponse. fail(2," You have not checked in today, please check in before test "); } User user=userService.getUserByCode(sign.getCode()); If (user==null) {return restResponse. fail(2," You are not in the test system, please join the class again "); } Face oface=new Face(); oface.setFace_type("LIVE"); oface.setImage_type("BASE64"); oface.setQuality_control("LOW"); oface.setLiveness_control("HIGH"); oface.setImage(sign1.getTavar()); // Face nface =new Face(); nface.setImage(sign.getImg()); nface.setFace_type("LIVE"); nface.setImage_type("BASE64"); nface.setQuality_control("LOW"); nface.setLiveness_control("HIGH"); MatchRequest req1 = new MatchRequest(oface.getImage(), oface.getImage_type()); MatchRequest req2 = new MatchRequest(nface.getImage(), nface.getImage_type()); ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>(); requests.add(req1); requests.add(req2); JSONObject res = FaceUtil.getClient().match(requests); String flg=res.getString("error_msg"); if(flg.equals("SUCCESS")) { JSONObject r=res.getJSONObject("result"); double score=r.getDouble("score"); if(score>70) { return RestResponse.ok(user); }else {return restResponse. fail(2," face recognition failure "); }}else {return restResponse. fail(2," face recognition failure "); }}Copy the code
The above is the basic realization of Baidu AI face recognition comparison