Implementing Face Recognition Login with Baidu AI and SpringBoot
This article details a step‑by‑step guide to building a face‑recognition based login system using Baidu Cloud AI services, SpringBoot, Thymeleaf, and jQuery, covering API integration, Maven dependencies, code examples for registration, detection, search, and deployment considerations.
Introduction
Motivated by the desire to create a personal face‑recognition demo, the author evaluated free APIs from several providers. After an unsuccessful attempt with Face++ due to an INVALID_OUTER_ID error, Baidu Cloud AI was chosen and successfully used to implement registration and login via facial recognition.
Difficulty Analysis
Both Face++ and Baidu Cloud AI expose REST APIs, but Face++ requires developers to extract and wrap the provided code snippets, demanding solid I/O stream handling and increasing code redundancy. Baidu Cloud AI offers two development paths: a manual code‑extraction approach similar to Face++, or a Maven‑based dependency that allows direct API calls, making the latter considerably easier.
Project Review (Baidu Cloud AI)
Final Effect Demonstration
Technology Stack
SpringBoot
Bootstrap
Thymeleaf
Baidu Cloud AI / Face++
Project Requirement Analysis
The goal is to replace manual username/password login with a face‑recognition flow: jQuery captures a photo from the local camera, the image is sent to a facial‑recognition API (Baidu Cloud or Face++), and the returned face data is used for automatic registration and subsequent login.
Project Setup
1. Preliminary Preparation
① Access the Baidu Cloud Face Recognition console (sign up with a phone number if you lack an account).
② Create a face‑recognition application, select the required interfaces (face detection, registration, search), and obtain the App ID, API Key, and Secret Key.
2. Test Baidu Cloud API
① Add the Maven dependency:
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.9.0</version>
</dependency>② Test the core APIs (face registration, detection, search) with the following Java snippets.
Face Registration
// Face registration example
@Test
public void testFaceRegister() throws IOException {
AipFace client = new AipFace("AppId", "Api_key", "Api_secret");
HashMap
map = new HashMap<>();
map.put("quality_control","NORMAL");
map.put("liveness_control","LOW");
String path = "local_image_path";
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
JSONObject res = client.addUser(encode, "BASE64", "pdx", "1000", map);
System.out.println(res.toString());
}Face Detection
@Test
public void testFaceCheck() throws IOException {
AipFace client = new AipFace("AppId", "Api_key", "Api_secret");
String path = "local_image_path";
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
JSONObject res = client.detect(encode, "BASE64", null);
System.out.println(res.toString(2));
}Face Search
@Test
public void testFaceSearch() throws IOException {
AipFace client = new AipFace("AppId", "Api_key", "Api_secret");
String path = "local_image_path";
byte[] bytes = Files.readAllBytes(Paths.get(path));
String encode = Base64Util.encode(bytes);
JSONObject res = client.search(encode, "BASE64", "pdx", null);
System.out.println(res.toString(2));
}Project Construction (Thymeleaf Template Engine)
1. Create Maven Project
Include the necessary dependencies and set up the directory structure.
2. Configure properties
ai.appId="api_id"
ai.apiKey="api_key"
ai.secretKey="api_secret"
ai.imageType=BASE64
ai.groupId="custom_group"
server.max-http-header-size=1000KB
spring.thymeleaf.cache=false3. Wrap API Calls in a Utility Class
private AipFace client;
private HashMap
map = new HashMap<>();
public BaiduAiUtils(){
map.put("quality_control","NORMAL");
map.put("liveness_control","LOW");
}
@PostConstruct
public void init(){
client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
}
public Boolean faceRegister(String userId, String image){
JSONObject res = client.addUser(image, IMAGE_TYPE, groupId, userId, map);
return res.getInt("error_code") == 0;
}
public Boolean faceUpdate(String userId, String image){
JSONObject res = client.updateUser(image, IMAGE_TYPE, groupId, userId, map);
return res.getInt("error_code") == 0;
}4. Front‑end Controller
@RequestMapping("/")
public String toLogin(){
return "index";
}
/**
* Face login endpoint
*/
@RequestMapping("/face-login")
@ResponseBody
public String searchface(@RequestBody @RequestParam(name="imagebast64") StringBuffer imagebast64, HttpServletRequest request) throws Exception {
String userId = faceLoginService.loginByFace(imagebast64);
request.getSession().setAttribute("userId", userId);
request.getSession().setAttribute("username", "Patrick Star");
return userId;
}Important Notes Before Building
① If the QPS limit is reached, Baidu Cloud provides a free testing quota that can be claimed from the console.
② OAuth errors often stem from stray spaces in App ID, API Key, or Secret Key.
Project Summary
Using Baidu Cloud AI, the complete face‑recognition login system was assembled, achieving fast recognition and smooth user experience, though concurrent request handling could be improved. All source code (including the snippets above) is available in the public Gitee repository.
Repository: https://gitee.com/gao-wumao/ai-face-login
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.