java播放v3录音_java实现人脸识别(使用百度云V3版本)

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

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

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

V3版本的人脸检测:

/**

*

* @Description: 该方法的主要作用:

* @Title: 人脸检测

* @param @param client 设定文件

* @return 返回类型:void

* @throws

* 个人博客:https://blog.csdn.net/qq_34137397

*/

public static void face_jiance(AipFace client) {

// 传入可选参数调用接口

HashMap options = new HashMap();

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 options = new HashMap();

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 options = new HashMap();

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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值