Java uses Baidu AI artificial intelligence face recognition. Is face recognition really easy to crack?
<dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.8.0</version> </dependency>
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)); } }
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; } }
-
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
Summary: