At present, the popular face swiping, face check-in, face payment and so on in the market are all due to the products of artificial intelligence, but whether there will be a face recognition that can be "fooled" through with a photo, as everyone said. Recently, I plan to connect with a face login system, so I conducted research on Baidu AI's face recognition, Developers can directly call the interface to realize face upload, face detection, face recognition, etc. It's very convenient. Let's share it as a note.

1) First, you need to go to Baidu Smart Cloud AI to officially apply for an account

Click here to arrive directly

2) After logging in the account, select from the left navigation, activate face recognition, and create an application

 Java uses Baidu AI artificial intelligence face recognition. Is face recognition really easy to crack?

 Java uses Baidu AI artificial intelligence face recognition. Is face recognition really easy to crack?

3) Remember several key parameters of the application

 Java uses Baidu AI artificial intelligence face recognition. Is face recognition really easy to crack?

4) Write code and create the simplest Maven project. Import the following dependencies

 <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.8.0</version> </dependency>

5) Write test classes, test face upload, face recognition, face update, etc. For details, please refer to the official developer documentation

 public class FaceTest { private AipFace client; @Before public void init() { //1. Create a client object for interaction between java code and Baidu Cloud //Parameter 1: appid, Parameter 2: apiKey, Parameter 3: secretKey, self filled after Baidu Cloud AI application client = new AipFace("", "", ""); } /** *Face registration: add user face photos to Baidu's face database * * @throws Exception */ @Test public void testFaceRegister() throws Exception { //2. Parameter setting HashMap<String, String> options = new HashMap<>(); options.put("quality_control", "NORMAL");// Picture quality NONE LOW NORMAL, HIGH, more parameters can refer to the official documents https://ai.baidu.com/docs#/Face -Java-SDK/top options.put("liveness_control", "NONE");// Picture for live detection test NONE options.put("action_type", "REPLACE");// Operation mode APPEND: REPLACE is added //3. Structure picture String path = "G:\\IdeaProjects\\basic_code\\face-test\\facetestimg\\test1.jpg"; //The uploaded image has two formats: url address and Base64 string byte[] bytes = Files.readAllBytes(Paths.get(path)); String encode = Base64Util.encode(bytes); //4. Call the api method to complete face registration /** *Parameter 1: (image URL or image Base64 string), *Parameter 2: Picture format (URL, BASE64) *Parameter 3: Group ID (fixed string) can be used to distinguish different classifications in actual business *Parameter 4: User ID The user ID of the user is generally stored in the actual business *Parameter 5: Basic parameter configuration in hashMap */ JSONObject res = client.addUser(encode, "BASE64", "testgroup1", "100001", options); System.out.println(res.toString()); } /** *Face update: update the photos in the face database */ @Test public void testFaceUpdate() throws Exception { //2. Parameter setting HashMap<String, String> options = new HashMap<>(); options.put("quality_control", "NORMAL");// Picture quality NONE LOW NORMAL, HIGH options.put("liveness_control", "NONE");// In vivo detection options.put("action_type", "REPLACE"); //3. Structure picture String path = "G:\\IdeaProjects\\basic_code\\face-test\\facetestimg\\test2.jpg"; //The uploaded image has two formats: url address and Base64 string byte[] bytes = Files.readAllBytes(Paths.get(path)); String encode = Base64Util.encode(bytes); //4. Call the api method to complete face registration /** *Parameter 1: (image URL or image Base64 string), *Parameter 2: Picture format (URL, BASE64) *Parameter 3: Group ID (fixed string) *Parameter 4: User ID *Parameter 5: Basic parameter configuration in hashMap */ JSONObject res = client.updateUser(encode, "BASE64", "testgroup1", "100001", options); System.out.println(res.toString()); } /** *Face detection: judge whether there is facial information in the picture */ @Test public void testFaceCheck() throws Exception { //Construction picture String path = "G:\\IdeaProjects\\basic_code\\face-test\\facetestimg\\test3.jpg"; //The uploaded image has two formats: url address and Base64 string byte[] bytes = Files.readAllBytes(Paths.get(path)); String image = Base64Util.encode(bytes); //Call api method for face detection //Parameter 1: (image URL or image Base64 string), //Parameter 2: Picture format (URL, BASE64) //Parameter 3: Basic parameter configuration in hashMap (null: use default configuration) JSONObject res = client.detect(image, "BASE64", null); System.out.println(res.toString(2)); } /** *Face search: compare the pictures uploaded by users with all faces in the specified face database, *Get the score of the one or several with the highest similarity * *Description: Return value (data, only the first one is needed, the data with the highest similarity) *Score: similarity score (more than 80 points can be considered as the same person) */ @Test public void testFaceSearch() throws Exception { //Construction picture String path = "G:\\IdeaProjects\\basic_code\\face-test\\facetestimg\\test1.jpg"; byte[] bytes = Files.readAllBytes(Paths.get(path)); String image = Base64Util.encode(bytes); //Face Search JSONObject res = client.search(image, "BASE64", "testgroup1", null); System.out.println(res.toString(2)); } }

6) Finally, it is sorted into tool classes, as well as commonly used face login generation QR code tool classes

Baidu AI face tool BaiduAiUtil:

 @Component public class BaiduAiUtil { @Value("${ai.appId}") private String APP_ID; @Value("${ai.apiKey}") private String API_KEY; @Value("${ai.secretKey}") private String SECRET_KEY; @Value("${ai.imageType}") private String IMAGE_TYPE; @Value("${ai.groupId}") private String groupId; private AipFace client; private HashMap<String, String> options = new HashMap<String, String>(); public BaiduAiUtil() { options.put("quality_control", "NORMAL");  // More parameters can be added by yourself options.put("liveness_control", "LOW"); } @PostConstruct public void init() { client = new AipFace(APP_ID, API_KEY, SECRET_KEY); } /** *Face registration: save user photos in the face database */ public Boolean faceRegister(String userId, String image) { //Face registration JSONObject res = client.addUser(image, IMAGE_TYPE, groupId, userId, options); Integer errorCode = res.getInt("error_code"); return errorCode == 0 ?  true : false; } /** *Face update: update user photos in face database */ public Boolean faceUpdate(String userId, String image) { //Face update JSONObject res = client.updateUser(image, IMAGE_TYPE, groupId, userId, options); Integer errorCode = res.getInt("error_code"); return errorCode == 0 ?  true : false; } /** *Face detection: judge whether there is a face portrait in the uploaded picture */ public Boolean faceCheck(String image) { JSONObject res = client.detect(image, IMAGE_TYPE, options); if (res.has("error_code") && res.getInt("error_code") == 0) { JSONObject resultObject = res.getJSONObject("result"); Integer faceNum = resultObject.getInt("face_num"); return faceNum == 1? true:false; }else{ return false; } } /** *Face search: find the most similar face in the face database and return data *Processing: If the matching score of a user is greater than 80, it can be regarded as the same user */ public String faceSearch(String image) { JSONObject res = client.search(image, IMAGE_TYPE, groupId, options); if (res.has("error_code") && res.getInt("error_code") == 0) { JSONObject result = res.getJSONObject("result"); JSONArray userList = result.getJSONArray("user_list"); if (userList.length() > 0) { JSONObject user = userList.getJSONObject(0); double score = user.getDouble("score"); if(score > 80) { return user.getString("user_id"); } } } return null; } }

QR code generation tool class QRCodeUtil:

 @Component public class QRCodeUtil { /** *Generate Base64 QR code */ public String crateQRCode(String content) throws IOException { System.out.println(content); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { QRCodeWriter writer = new QRCodeWriter(); BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, 200, 200); BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix); ImageIO.write(bufferedImage, "png", os); //Add picture logo return new String("data:image/png;base64," + Base64.encode(os.toByteArray())); } catch (Exception e) { e.printStackTrace(); } finally { os.close(); } return null; } }

7) Usage
Here we use SpringBoot to connect a set of face recognition:

  • Lead in coordinates
 <!--         Baidu Cloud AI --> <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.8.0</version> </dependency> <!--         Google QR code generation --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.2.1</version> </dependency>
  • Profile Add
 ai: appId: ? apiKey: ? secretKey: ? imageType: BASE64 groupId: lcryface

8) Implementation process of face login and face sign in
Internet web project: user -->QR code display -->scan QR code -->jump to landing page (mobile camera or notebook camera) -->face recognition -->call Baidu AI verification -->login successfully

Entity project: user -->need to submit additional information (amount, etc.) -->hardware camera collects face information -->face recognition -->call Baidu AI for verification -->success

Summary:

In conclusion, it is not feasible to 'perfunctory' the system's face recognition with photos. Unless there is no live detection, so I can upload multiple photos for comparison only when I do not detect the live detection in the test. And in the comparison score, the score of the human face comparison score must be high enough, which is basically impossible to forge. Of course, I have also asked people who do this. At present, twins need to be improved. In fact, they are relatively safe. Face check-in can be used if it does not involve money. Payment can be temporarily ignored. It varies from person to person

Article Contents