• 人脸识别活体检测之张张嘴和眨眨眼——FaceServlet


    package org.web;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.entity.Landmark;
    import org.json.JSONObject;
    import org.util.Json_Parsing;
    
    import com.baidu.aip.face.AipFace;
    
    public class FaceServlet extends HttpServlet {
    
    
    
        /**
        * @Fields serialVersionUID :该字段的意思:
        * 个人博客:https://blog.csdn.net/qq_34137397
        */
    
        private static final long serialVersionUID = 7753313994078255746L;
        //百度云信息
        private String APP_ID = "你的APP_ID ";
        private String API_KEY = "你的API_KEY ";
        private String SECRET_KEY = "你的SECRET_KEY ";
        private AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);  
        private Map<Integer, Object> dataMap = new ConcurrentHashMap<Integer,Object>();
        /**
         * (非 Javadoc)
        * <p>Description(描述):调用dopost的方法 </p>
        * <p>Title: doGet</p>
        * @param request
        * @param response
        * @throws ServletException
        * @throws IOException
        * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
        * 个人博客:https://blog.csdn.net/qq_34137397
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
    
        /**
         * (非 Javadoc)
        * <p>Description(描述):dopost方法 </p>
        * <p>Title: doPost</p>
        * @param request
        * @param response
        * @throws ServletException
        * @throws IOException
        * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
        * 个人博客:https://blog.csdn.net/qq_34137397
         */
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            // 防止乱码
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            String tag = request.getParameter("tag");           //区分操作类型的值
            String img = request.getParameter("img");           // 图像数据
    
            if(tag.equals("mouse")){
                //张嘴
                face_mouse(img,response,request);
            }else if(tag.equals("eye")){
                //眨眼比较
                face_eye(img,response,request);
            }
    
        }
        //C#winforms实现windows窗体人脸识别
    
    
    
        /**
         * 
        * @Description: 该方法的主要作用:张张嘴验证
        * @Title: face_jiance
        * @param  @param img
        * @param  @param response
        * @param  @param request 设定文件  
        * @return  返回类型:void   
        * @throws
        * 个人博客:https://blog.csdn.net/qq_34137397
         */
        private void face_mouse(String img, HttpServletResponse response,
                HttpServletRequest request) {
    
            /*if (dataMap.get(1) == null) {
                // 第一次请求
                landmark = face_jiance(img);
                System.out.println("第一次的上嘴唇:"+landmark.getMouse__top().get(0));
                System.out.println("第一次的嘴唇中心:"+landmark.getMouse_zhongxin().get(0));
                System.out.println("第一次的下嘴唇:"+landmark.getMouse__bottom().get(0));
                dataMap.put(1, landmark);
            } else {*/
                // 不是第一次请求
                Landmark landmark_next = face_jiance(img);
                // 和前一次的数据进行比较
                //Landmark landmark_pre = (Landmark) dataMap.get(1);
                // 嘴唇上面的位置相对于中心点对比
                PrintWriter writer;
                if ((landmark_next.getMouse_zhongxin().get(0) - landmark_next
                        .getMouse__top().get(0)) > 40&&(landmark_next.getMouse__bottom().get(0) - landmark_next
                                .getMouse_zhongxin().get(0)) > 30) {
    
                        try {
                            writer = response.getWriter();
                            writer.print("1");
                        } catch (IOException e) {
                            // TODO 异常执行块!
                            e.printStackTrace();
                    }
                }else{
                    try {
                        writer = response.getWriter();
                        writer.print("0");
                    } catch (IOException e) {
                        // TODO 异常执行块!
                        e.printStackTrace();
                }
                }
                //dataMap.put(1,landmark_next);         //放进去  方便下次进行比较
    
            }
    
        /*}*/
    
        /**
         * 
        * @Description: 该方法的主要作用:眨眼对比
        * @Title: face_eye
        * @param  @param img
        * @param  @param response
        * @param  @param request 设定文件  
        * @return  返回类型:void   
        * @throws
        * 个人博客:https://blog.csdn.net/qq_34137397
         */
        private void face_eye(String img, HttpServletResponse response,
                HttpServletRequest request) {
            Landmark landmark_next = face_jiance(img);
             System.out.println("闭着眼的左眼上边与中间的值:"+((landmark_next.getLeft_eye_zhongxin().get(0))-(landmark_next.getLeft_eye_top().get(0))));
             System.out.println("闭着眼的左眼下边与中间的值:"+((landmark_next.getLeft_eye_bottom().get(0))-(landmark_next.getLeft_eye_zhongxin().get(0))));
             System.out.println("闭着眼的右眼上边与中间的值:"+((landmark_next.getRight_eye_zhongxin().get(0))-(landmark_next.getRight_eye_top().get(0))));
             System.out.println("闭着眼的右眼下边与中间的值:"+((landmark_next.getRight_eye_bottom().get(0))-(landmark_next.getRight_eye_zhongxin().get(0))));
            System.out.println("__________________________________________________");
             PrintWriter writer;
            if ((landmark_next.getLeft_eye_zhongxin().get(0))
                    - (landmark_next.getLeft_eye_top().get(0)) < 6
                    && (landmark_next.getRight_eye_zhongxin().get(0))
                            - (landmark_next.getRight_eye_top().get(0)) < 6) {
                System.out.println("进来了上边验证成功");
                // 继续判断下边的
                if ((landmark_next.getLeft_eye_bottom().get(0))
                        - (landmark_next.getLeft_eye_zhongxin().get(0)) < 6.6
                        && (landmark_next.getRight_eye_bottom().get(0))
                                - (landmark_next.getRight_eye_zhongxin().get(0)) < 6.6) {
                    System.out.println("进来了下边验证成功");
                    try {
                        writer = response.getWriter();
                        writer.print("1");
                    } catch (IOException e) {
                        // TODO 异常执行块!
                        e.printStackTrace();
                    }
                } else {
                    try {
                        writer = response.getWriter();
                        writer.print("0");
                    } catch (IOException e) {
                        // TODO 异常执行块!
                        e.printStackTrace();
                    }
                }
            }
        }
    
    
        /**
         * 
        * @Description: 该方法的主要作用:活体检测
        * @Title: face_jiance
        * @param  @param img
        * @param  @return 设定文件  
        * @return  返回类型:Landmark   
        * @throws
        * 个人博客:https://blog.csdn.net/qq_34137397
         */
        private Landmark face_jiance(String img){
             HashMap<String, String> options = new HashMap<String, String>();
                options.put("face_field", "landmark");
                options.put("face_type", "LIVE");
                String image = img;
                String imageType = "BASE64";
                // 人脸检测
                JSONObject res = null;
                try {
                    res = client.detect(image, imageType, options);
                } catch (Exception e) {
                    // TODO 异常执行块!
                    e.printStackTrace();
    
                }
               return Json_Parsing.parsingFaceJson(res);
        }
    
    
    }
    
  • 相关阅读:
    linux 常用函数
    现在什么都不是浮云
    laosao
    2012年
    工作任务
    代码整理
    如何做一个男人
    很重要的2点
    录像预录的设计及实现
    弄了一个小网站
  • 原文地址:https://www.cnblogs.com/a1111/p/12816020.html
Copyright © 2020-2023  润新知