人脸识别注册及登录

功能测试网址https://www.nevergiveu.com/face/index.jsp
html

		<dl class="admin_login">
			<dt>
				 </br><strong>请把脸对准摄像头</strong>
			</dt>
				<div id="webcam"></div>
				<canvas id="canvas" width="300" height="400"></canvas>
			<dd>
				<input type="button" id="snapBtn" value="立即登录" class="submit_btn" />
			</dd>
			<dd>
				<input type="button" id="snapBtn1" value="立即注册" class="submit_btn" />
			</dd>
		</dl>

js

	window.onload= function () {

		//获得video摄像头区域
		var video = document.getElementById("video");
		var constraints = {
			video: {width: 500, height: 500,facingMode: "user"},
			audio: true
		};
		var promise =navigator.mediaDevices.getUserMedia(constraints);
		//这里介绍新的方法,返回一个 Promise对象
		// 这个Promise对象返回成功后的回调函数带一个 MediaStream 对象作为其参数
		// then()是Promise对象里的方法
		// then()方法是异步执行,当then()前的方法执行完后再执行then()内部的程序
		// 避免数据没有获取到

		promise.then(function (MediaStream) {
			video.srcObject = MediaStream;
			video.play();
		});
	}
          var pos = 0,
              ctx = null,
              image = [],base64,imgData1,username;

              function mycheck() {
              	var video = document.getElementById("video");
			var canvas = document.getElementById("canvas");
			var ctx = canvas.getContext('2d');
			ctx.drawImage(video, 0, 0, 500, 500);
			// 将图片显示到canvas中
			//ctx.putImageData(img, 0, 0);
			// 取得图片的base64码
			 base64 = canvas.toDataURL("image/png");
			//获取图像在前端截取22位以后的字符串作为图像数据
			 imgData1 = base64.substring(22);
			 username = "14";
			$.ajax({
				type: "post",
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				url: "face?action=login",
				data: {"img":imgData1,"username":username},
				dataType:"json",
				success: function(data){
					if(data.result == "请把脸放上!!"){
						alert("登录失败")
					}else{
						alert('登陆成功,欢迎'+data.result.user_list[0].user_info);
						var url = "test.jsp?name="+data.result.user_list[0].user_info;

						$(window).attr("location",encodeURI(url));
					}
				},error:function(msg){
					alert("面容识别失败,请继续验证");
				}
			});
		}
	$('#snapBtn1').on('click', function() {
		$(window).attr("location","index.jsp");
	});

在百度创建人脸库
在这里插入图片描述
java

public class FaceServlet extends HttpServlet{
	//这三个属性跟你你在百度人脸库的信息填写即可
	final String APP_ID = "";
	final String API_KEY = "";
	final String SECRET_KEY = "";
	final AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
	
    @Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String action = req.getParameter("action");
		if(action.equals("reg")){
			this.face_reg(req,resp,client);
		}else if(action.equals("login")){
			this.face_login(req,resp,client);
		}else if(action.equals("jiance")){
			this.face_jiance(req,resp,client);
		}
	}

	//注册
	public boolean face_reg(HttpServletRequest req, HttpServletResponse resp,AipFace client) {
		String img	= req.getParameter("img");			//图像数据
		String username = req.getParameter("username");	//用户名
		System.out.println(img);
		System.out.println(username);
		boolean tag = false;

		// 传入可选参数调用接口
		HashMap<String, String> options1 = new HashMap<String, String>();
		options1.put("user_id", "14");
		String groupIdList = "face";//填你在百度创建的人脸库
		try {
			String imageType1 = "BASE64";
			//转换格式
			String strImageToBase641 = img;
			//输出base64图像数据
			//先检测用户是否已注册,不检测也可以,百度人脸库支持一个用户拥有多张照片
			// 人脸搜索
			JSONObject res1 = client.search(strImageToBase641, imageType1, groupIdList, options1);
			System.out.println(res1.toString(2));
			req.setCharacterEncoding("utf-8");  //这里不设置编码会有乱码
			resp.setContentType("text/html;charset=utf-8");
			resp.setHeader("Cache-Control", "no-cache");
			PrintWriter out = resp.getWriter();
			if (res1.get("error_code").toString().equals("0")) {
				out.print(res1);//检查是否已注册
			} else {
				// 传入可选参数调用接口
				HashMap<String, String> options = new HashMap<String, String>();
				options.put("user_info", username);

				String groupId = "face";
				String userId = "14";

				try {
					String imageType = "BASE64";
					//转换格式
					String strImageToBase64 = img;
					//输出base64图像数据				
					// 人脸注册
					JSONObject res = client.addUser(strImageToBase64, imageType, groupId, userId, options);
					System.out.println(res.toString(2));

					req.setCharacterEncoding("utf-8");  //这里不设置编码会有乱码
					resp.setContentType("text/html;charset=utf-8");
					resp.setHeader("Cache-Control", "no-cache");
					if (res.get("error_code").toString().equals("0")) {
						out.print("{\"result\": \"注册成功!!\"}");  //注册成功
					} else {
						out.print("{\"result\": \"请把脸放上!!\"}");//请把脸放上
					}
				} catch (IOException e) {
					// TODO 异常执行块!
					e.printStackTrace();
				}
			}
		}catch (IOException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}
		return tag;
	}

	//登录
	public boolean face_login(HttpServletRequest req, HttpServletResponse resp,AipFace client) {
		String img	= req.getParameter("img");			//图像数据
		String username = req.getParameter("username");	//用户名
		System.out.println(img);
		System.out.println(username);
		boolean tag = false;
		// 传入可选参数调用接口
	    HashMap<String, String> options = new HashMap<String, String>();
	    options.put("user_id", "14");//user_id自定义就行
        String groupIdList = "face";//填你在百度的人脸库
		try {

			 String imageType = "BASE64";
		    //转换格式
		    String strImageToBase64 =img;
		    //输出base64图像数据
		    // 人脸搜索
		    JSONObject res = client.search(strImageToBase64, imageType, groupIdList, options);
		    System.out.println(res.toString(2));
		    req.setCharacterEncoding("utf-8");  //这里不设置编码会有乱码
	        resp.setContentType("text/html;charset=utf-8");
	        resp.setHeader("Cache-Control", "no-cache"); 
		    PrintWriter out = resp.getWriter();
		    String str = res.toString(2);
		    if(res.get("error_code").toString().equals("0")){
		    	out.print(res.toString(2));  //登录成功
		    }else{
		    	out.print("{\"result\":\"请把脸放上!!\"}");//请把脸放上
		    }
		} catch (IOException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}
		return tag;
	}
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值