美文网首页
基于百度AI开放平台的人脸识别的注册登录(1)

基于百度AI开放平台的人脸识别的注册登录(1)

作者: itczt | 来源:发表于2019-03-20 19:20 被阅读0次

    基于其本人还是一个菜鸟,只能做傻瓜式的阐述。请见谅!
    1.打开百度ai开放平台,并注册登陆账号。
    http://ai.baidu.com/?track=cp:aipinzhuan|pf:pc|pp:AIpingtai|pu:title|ci:|kw:10005792

    百度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<String, String> options = new HashMap<String, String>();
            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<String, String> options = new HashMap<String, String>();
            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"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    恭喜您,使用人脸识别登陆系统成功
    </body>
    </html>
    

    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>人脸识别 • 在线版</title>
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/bface.css" rel="stylesheet">
    </head>
    
    <body>
        <!--整体内容区域-->
        <div class="container container-main">
            <!--人脸识别模态框-->
            <div class="modal fade" id="myModal" aria-hidden="true"
                data-backdrop="static" tabindex="-1" role="dialog"
                aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title" id="myModalLabel"
                                style="display: inline-block">BFace ▪ 人脸识别</h4>
                            <button type="button" class="close" data-dismiss="modal"
                                aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <div class="modal-body-title">
                                <!--头部提示文字-->
                                <p>请将头部放在视频区域内,匹配成功将会自动登入系统</p>
                                <p>
                                    如果视频内未出现识别框或长时间未响应 <a style="cursor: pointer">请单击此处</a>
                                </p>
                            </div>
                            <!--人脸框-->
                            <div class="modal-body-viode">
                                <canvas id="canvas" width="420" height="340"></canvas>
                                <video id="video" width="420" height="340" preload autoplay loop
                                    muted></video>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!--人脸识别开启模态框按钮-->
            <table style="margin: 200px auto;">
                <tr>
                    <td colspan="3"><img alt="" src="images/timg.jpg" /></td>
                </tr>
                <tr>
                    <td align="center">
                        <button class="btn btn-success" data-toggle="modal"
                            data-target="#myModal" onclick="showLogin()">登陆</button>
                    </td>
                    
                    <td align="center">
                        <button class="btn btn-success" data-toggle="modal"
                            data-target="#myModal" onclick="showReg()">注册</button>
                    </td>
                </tr>
            </table>
        </div>
    
        <!--JS导入-->
        <script type="text/javascript" src="js/tracking-min.js"></script>
        <script type="text/javascript" src="js/face-min.js"></script>
        <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <script type="text/javascript" src="js/bface.js"></script>
        <script type="text/javascript">
            function showLogin() {
                //调用人脸识别方法
                login("http://localhost:8080/faceDiscern/LoginServlet");
            }
            function showReg() {
                //调用人脸识别方法
                reg("http://localhost:8080/faceDiscern/RegServlet");
            }
        </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:基于百度AI开放平台的人脸识别的注册登录(1)

          本文链接:https://www.haomeiwen.com/subject/axhbpqtx.html