百度ai开放平台首页
2.选择产品服务,人脸与人体识别,人脸识别选项。打开后如图所示
人脸识别
3.点击立即使用
3.jpg
4.点击创捷应用
创建应用
这一页大家按自己需求填写即可
5.创建完成后点击管理应用会出现如下图所示
管里应用
这里我们需要的就是AppID、API Key和Secret Key这三项
6.接下来就开始用eclipse来写Java代码了
主要目录
核心代码
LoginServlet.java
package servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import util.AipFaceHelper;
import util.StringUtil;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); // 设置防止提交的中文数据乱码
response.setContentType("text/html;charset=UTF-8"); // 设置响应的信息不乱码
PrintWriter out = response.getWriter();// 获取一个能够向客户端显示信息的对象
// 传入可选参数调用接口
HashMap options = new HashMap();
options.put("quality_control", "LOW");// 图片质量控制
options.put("liveness_control", "LOW");// 活体检测控制
options.put("user_id", "no1");
options.put("max_user_num", "1"); // 查找后返回的用户数量。返回相似度最高的几个用户
String image = request.getParameter("base");
image = StringUtil.base64SubString(image);
String imageType = "BASE64";
String groupIdList = "test01"; // 从指定的group中进行查找 用逗号分隔,上限20个
// 人脸搜索
JSONObject res = AipFaceHelper.getInstance().search(image, imageType, groupIdList, options);
System.out.println(res.toString(2));
out.print(res.toString(2));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
RegServlet.java
package servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import util.AipFaceHelper;
import util.StringUtil;
/**
* Servlet implementation class RegServlet
*/
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); // 设置防止提交的中文数据乱码
response.setContentType("text/html;charset=UTF-8"); // 设置响应的信息不乱码
PrintWriter out = response.getWriter();// 获取一个能够向客户端显示信息的对象
HashMap options = new HashMap();
options.put("user_info", "user's info");// 用户资料,长度限制256B
options.put("quality_control", "LOW");// 图片质量控制
options.put("liveness_control", "LOW");// 活体检测控制
// 取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串
String image = request.getParameter("base");
image = StringUtil.base64SubString(image);
String imageType = "BASE64";
String groupId = "test01";
String userId = "no1";
// 人脸注册
JSONObject res = AipFaceHelper.getInstance().addUser(image, imageType, groupId, userId, options);
System.out.println(res.toString(2));
out.print(res.toString(2));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
AipFaceHelper.java
package util;
import com.baidu.aip.face.AipFace;
public class AipFaceHelper {
// 设置APPID/AK/SK
private static final String APP_ID = "15769139";
private static final String API_KEY = "3wmj05WUe5HyVK1amYpN8Ym6";
private static final String SECRET_KEY = "mMlsBOSugeaBZrMn14q5g44M5eBRsHmV";
private static AipFace client = null;
private AipFaceHelper() {
}
public static AipFace getInstance() {
if (client == null) {
client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
//设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
}
return client;
}
}
StringUtil.java
package util;
public class StringUtil {
public static String base64SubString(String base) {
return base.substring(22);
}
}
welcome.jsp
pageEncoding="UTF-8"%>
Insert title here恭喜您,使用人脸识别登陆系统成功
index.jsp
pageEncoding="UTF-8"%>
人脸识别 • 在线版data-backdrop="static" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel">
![]() | ||
data-target="#myModal" οnclick="showLogin()">登陆 | data-target="#myModal" οnclick="showReg()">注册 |
function showLogin() {
//调用人脸识别方法
login("http://localhost:8080/faceDiscern/LoginServlet");
}
function showReg() {
//调用人脸识别方法
reg("http://localhost:8080/faceDiscern/RegServlet");
}