java实现人脸识别(使用百度云V3版本)

版权声明:文章可以随便转载,但是转载时带上原文地址来源,侵权必究 https://blog.csdn.net/qq_34137397/article/details/87820313

2017年,开发了第一个版本的人脸识别,当时费时有5天之久终于写出来了,但是只适用于火狐浏览器,别的浏览器都打不开摄像头。

2018年,将人脸识别重新完善,可以支持360、火狐、谷歌等主流浏览器,版本都是使用的百度云V2版,当时已经出来了V3版,只是一直没时间去看。

2019年,重新拿出来以前写的项目,将百度云V3版人脸识别写出来分享给大家,以下是源码分享(只是一个简单的demo,不如V2版本的全面):

V2版本的人脸识别:java实现人脸识别源码【含测试效果图】——前期准备工作及访问提示

V3版本的jar包下载:人脸识别百度API,V3的jar包

V3版本的人脸检测:

/**
     * 
    * @Description: 该方法的主要作用:
    * @Title: 人脸检测
    * @param  @param client 设定文件  
    * @return  返回类型:void   
    * @throws
    * 个人博客:https://blog.csdn.net/qq_34137397
     */
	public static void face_jiance(AipFace client) { 
	    // 传入可选参数调用接口
	    HashMap<String, String> options = new HashMap<String, String>();
	    options.put("face_field", "age");
	    options.put("max_face_num", "2");
	    options.put("face_type", "LIVE");
	    File directory = new File("");// 参数为空
        String courseFile="";
		try {
			courseFile = directory.getCanonicalPath();
			//获取当前项目的根路径
	        //System.out.println(courseFile);
		    String image = courseFile+"/WebRoot/picture/111.jpg";
		    String imageType = "BASE64";
		    //转换格式
		    String strImageToBase64 =ImageToBase64(image);
		    //输出base64图像数据
	        //System.out.println("本地图片转换Base64:"+strImageToBase64);
		    // 人脸检测
		    JSONObject res = client.detect(strImageToBase64,imageType, options);
		    System.out.println(res.toString(2));
		} catch (IOException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}
	}

V3版本的人脸注册:

/**
	 * 
	* @Description: 该方法的主要作用:人脸注册
	* @Title: face_reg
	* @param  @param client 设定文件  
	* @return  返回类型:void   
	* @throws
	* 个人博客:https://blog.csdn.net/qq_34137397
	 */
	public static void face_reg(AipFace client) {
	    // 传入可选参数调用接口
	    HashMap<String, String> options = new HashMap<String, String>();
	    options.put("user_info", "user's info");

	    String groupId = "group1";
	    String userId = "user1";

	    File directory = new File("");// 参数为空
        String courseFile="";
		try {
			courseFile = directory.getCanonicalPath();
			//获取当前项目的根路径
	        //System.out.println(courseFile);
			 String image = courseFile+"/WebRoot/picture/111.jpg";
			 String imageType = "BASE64";
		    //转换格式
		    String strImageToBase64 =ImageToBase64(image);
		    //输出base64图像数据
	        //System.out.println("本地图片转换Base64:"+strImageToBase64);
		    // 人脸注册
		    JSONObject res = client.addUser(strImageToBase64, imageType, groupId, userId, options);
		    System.out.println(res.toString(2));
	    
		} catch (IOException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}
	}

V3版本的人脸登陆:

/**
	 * 
	* @Description: 该方法的主要作用:人脸登陆
	* @Title: face_login
	* @param  @param client 设定文件  
	* @return  返回类型:void   
	* @throws
	* 个人博客:https://blog.csdn.net/qq_34137397
	 */
	public static void face_login(AipFace client) {
	    // 传入可选参数调用接口
	    HashMap<String, String> options = new HashMap<String, String>();
	    options.put("user_id", "user1");
	    File directory = new File("");// 参数为空
        String courseFile="";
        String groupIdList = "group1";
		try {
			courseFile = directory.getCanonicalPath();
			//获取当前项目的根路径
	        //System.out.println(courseFile);
			 String image = courseFile+"/WebRoot/picture/222.jpg";
			 String imageType = "BASE64";
		    //转换格式
		    String strImageToBase64 =ImageToBase64(image);
		    //输出base64图像数据
	        //System.out.println("本地图片转换Base64:"+strImageToBase64);
		    // 人脸搜索
		    JSONObject res = client.search(strImageToBase64, imageType, groupIdList, options);
		    System.out.println(res.toString(2));
	    
		} catch (IOException e) {
			// TODO 异常执行块!
			e.printStackTrace();
		}

	}

V3版本的本地图片转换成Base64图像格式的方法:

/**
	 * 
	* @Description: 该方法的主要作用:本地图片转换成Base64图像格式的方法
	* @Title: ImageToBase64
	* @param  @param imgPath
	* @param  @return 设定文件  
	* @return  返回类型:String   
	* @throws
	* 个人博客:https://blog.csdn.net/qq_34137397
	 */
	public static String ImageToBase64(String imgPath){
		InputStream inputStream = null;
		byte[] data = null;
		try {
			inputStream = new FileInputStream(imgPath);	
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		//进行编码
		BASE64Encoder encoder = new BASE64Encoder();
		//返回编码完成的base64图像数据
		return encoder.encode(data);
	}

另外附上其他人脸识别相关的博文:
1.关于人脸和指纹识别共同交流方案

2.人脸识别活体检测之眨眨眼和张张嘴

3.C#winforms实现windows窗体人脸识别

4.java实现人脸识别源码【含测试效果图】——前期准备工作及访问提示

5.多功能语音播放器上线啦~

6.纯js实现人脸识别眨眨眼张张嘴案例

7.人脸识别活体检测测试案例

8.青鸟IT汇微信公众号新增智能机器人

9.使用阿里云智能翻译接口案例

10.使用阿里云火车票查询接口案例

11.使用阿里云身份证扫描识别接口案例

12.调用阿里云接口实现短信消息的发送源码

猜你喜欢

转载自blog.csdn.net/qq_34137397/article/details/87820313