基于其本人还是一个菜鸟,只能做傻瓜式的阐述。请见谅!
1.打开百度ai开放平台,并注册登陆账号。
http://ai.baidu.com/?track=cp:aipinzhuan|pf:pc|pp:AIpingtai|pu:title|ci:|kw:10005792
2.选择产品服务,人脸与人体识别,人脸识别选项。打开后如图所示
3.点击立即使用
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
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
恭喜您,使用人脸识别登陆系统成功
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
人脸识别 • 在线版
以下为js和css源码
ajax.js
var Ajax = {
get: function (url, fn) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200 || xhr.status == 304) {
fn.call(this, xhr.responseText);
}
};
xhr.send();
},
post: function (url, data, fn) {
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304)) {
fn.call(this, xhr.responseText);
}
};
xhr.send(data);
}
}
Ajax.post("", {
"base": canvas.toDataURL()
}, function (result) {
if (result.result[0].scores > 90) {
alert("OK");
} else {
alert("NO");
}
}, "json");
Ajavx.get("", function (result) {
});
7.运行效果