热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

人脸识别工具类

packagecom.yong.util;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.



package com.yong.util;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.face.AipFace;

import com.baidu.aip.face.FaceVerifyRequest;

import com.baidu.aip.face.MatchRequest;

import com.baidu.aip.util.Base64Util;

//import Decoder.BASE64Decoder;

public class FaceSpot {

//private static final BASE64Decoder decoder = new BASE64Decoder();

//百度API自己创建的应用的参数

private static final String AppID = “1799—”;

private static final String APIKey = “E1lQ78z----”;

private static final String SecretKey = “mTAZ-----”;

static AipFace client = null;
static {
client = new AipFace(AppID, APIKey, SecretKey);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
}
//String file1 = "F:/3.png";
//String file2 = "F:/2.png";
//byte[] img1 = FileToByte(new File(file1));
//byte[] img2 = FileToByte(new File(file1));
//两个人脸对比返回分数
//matchFace(img1, img2);
//增加一个用户
//addUser(img2,"BASE64","22","EasyBuy");
//获得此用户所有面部Facial ID
//getUserFaceList("22","EasyBuy");
//登录,支付时使用
//searchFace(img2, "EasyBuy","22");
public static void main(String[] args) throws IOException {
//BASE64Decoder decoder = new BASE64Decoder();
String file1 = "E:/login.jpg";
byte[] img2 = FileToByte(new File(file1));
System.out.println(addUser(img2,"BASE64","2","user_face"));

// String file1 = "E:/IMG.jpg";
//byte[] img2 = FileToByte(new File(file1));
// System.out.println(searchFace(img2,"user_face","1"));

}
/**
* 人脸检测
*
* @return
* @throws IOException
*/
public static String detectFace(File file, String max_face_num) {
try {
return detectFace(FileToByte(file), max_face_num);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 人脸检测
*
* @return
* @throws IOException
*/
public static String detectFace(byte[] arg0, String max_face_num) {
try {
HashMap optiOns= new HashMap();
options.put("face_field",
"age,beauty,expression,faceshape,gender,glasses,race,qualities");
options.put("max_face_num", "2");
options.put("face_type", "LIVE");
// 图片数据
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
JSONObject res = client.detect(imgStr, imageType, options);
// System.out.println(res.toString(2));
return res.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 人脸比对
*
* @param file1
* @param file2
* @return
*/
public static String matchFace(File file1, File file2) {
try {
return matchFace(FileToByte(file1), FileToByte(file2));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 人脸比对
*
* @param arg0
* 人脸1
* @param arg1
* 人脸2
* @return
*/
public static String matchFace(byte[] arg0, byte[] arg1) {
String imgStr1 = Base64Util.encode(arg0);
String imgStr2 = Base64Util.encode(arg1);
MatchRequest req1 = new MatchRequest(imgStr1, "BASE64");
MatchRequest req2 = new MatchRequest(imgStr2, "BASE64");
ArrayList requests = new ArrayList();
requests.add(req1);
requests.add(req2);
JSONObject res = client.match(requests);
return res.toString();
}
/**
* 人脸搜索
*
* @param file
* @param groupIdList
* @param userId
* @return
*/
public static String searchFace(File file, String groupIdList, String userId) {
try {
return searchFace(FileToByte(file), groupIdList, userId);
} catch (IOException e) {

e.printStackTrace();
}
return null;
}
/**
* 人脸搜索
*
* @param arg0
* @param groupIdList
* @return
*/
public static String searchFace(byte[] arg0, String groupIdList,
String userId) {
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
HashMap optiOns= new HashMap();
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
if (userId != null) {
options.put("user_id", userId);
}
options.put("max_user_num", "1");
JSONObject res = client.search(imgStr, imageType, groupIdList, options);
return res.toString(2);
}
//Base64参数
public static JSONObject searchFace(String imgStr, String groupIdList,
String userId) {
String imageType = "BASE64";
HashMap optiOns= new HashMap();
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
/*
* if (userId != null) { options.put("user_id", userId); }
*/
options.put("max_user_num", "1");
JSONObject res = client.search(imgStr, imageType, groupIdList, options);
System.out.println(res.toString(2));
return res;
}
/**
* 增加用户
*
* @param file
* @param userInfo
* @param userId
* @param groupId
* @return
*/
public static String addUser(File file, String userInfo, String userId,
String groupId) {
try {
return addUser(FileToByte(file), userInfo, userId, groupId);
} catch (IOException e) {

e.printStackTrace();
}
return null;
}
/**
* 增加用户
*
* @param arg0
* @param userInfo
* @param userId
* @param groupId
* @return
*/
public static String addUser(byte[] arg0, String userInfo, String userId,String groupId) {
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
HashMap optiOns= new HashMap();
options.put("user_info", userInfo);
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
JSONObject res = client.addUser(imgStr, imageType, groupId, userId,options);
return res.toString(2);
}
public static String updateUser(File file, String userInfo, String userId,
String groupId) {
try {
return updateUser(FileToByte(file), userInfo, userId, groupId);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 更新用户
*
* @param arg0
* @param userInfo
* @param userId
* @param groupId
* @return
*/
public static String updateUser(byte[] arg0, String userInfo,
String userId, String groupId) {
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
HashMap optiOns= new HashMap();
if (userInfo != null) {
options.put("user_info", userInfo);
}
options.put("quality_control", "NORMAL");
options.put("liveness_control", "LOW");
JSONObject res = client.updateUser(imgStr, imageType, groupId, userId,
options);
return res.toString(2);
}
/**
* 删除用户人脸信息
*
* @param userId
* @param groupId
* @param faceToken
* @return
*/
public static String deleteUserFace(String userId, String groupId,
String faceToken) {
HashMap optiOns= new HashMap();
// 人脸删除
JSONObject res = client.faceDelete(userId, groupId, faceToken, options);
return res.toString();
}
/**
* 查询用户信息
*
* @param userId
* @param groupId
* @return
*/
public static String searchUserInfo(String userId, String groupId) {
HashMap optiOns= new HashMap();
// 用户信息查询
JSONObject res = client.getUser(userId, groupId, options);
return res.toString(2);
}
/**
* 获取用户人脸列表
*
* @param userId
* @param groupId
* @return
*/
public static String getUserFaceList(String userId, String groupId) {
HashMap optiOns= new HashMap();
// 获取用户人脸列表
JSONObject res = client.faceGetlist(userId, groupId, options);
return res.toString(2);
}
/**
* 获取一组用户
*
* @param groupId
* @param returnNum
* @return
*/
public static String getGroupUsers(String groupId, String returnNum) {
HashMap optiOns= new HashMap();
options.put("start", "0");
if (returnNum != null) {
options.put("length", returnNum);
}
// 获取用户列表
JSONObject res = client.getGroupUsers(groupId, options);
return res.toString(2);
}
/**
* 组用户复制
*
* @param userId
* @param srcGroupId
* @param dstGroupId
* @return
*/
public static String userCopy(String userId, String srcGroupId,
String dstGroupId) {
HashMap optiOns= new HashMap();
options.put("src_group_id", srcGroupId);
options.put("dst_group_id", dstGroupId);
// 复制用户
JSONObject res = client.userCopy(userId, options);
return res.toString(2);
}
/**
* 删除用户
*
* @param userId
* @param groupId
* @return
*/
public static String deleteUser(String userId, String groupId) {
HashMap optiOns= new HashMap();
// 人脸删除
JSONObject res = client.deleteUser(groupId, userId, options);
return res.toString();
}
/**
* 增加组信息
*
* @param groupId
* @return
*/
public static String addGroup(String groupId) {
HashMap optiOns= new HashMap();
// 创建用户组
JSONObject res = client.groupAdd(groupId, options);
return res.toString();
}
/**
* 删除
*
* @param groupId
* @return
*/
public static String deleteGroup(String groupId) {
HashMap optiOns= new HashMap();
// 创建用户组
JSONObject res = client.groupDelete(groupId, options);
return res.toString();
}
/**
* 获取组列表
*
* @param length
* @return
*/
public static String getGroupList(String length) {
HashMap optiOns= new HashMap();
options.put("start", "0");
options.put("length", length);
// 组列表查询
JSONObject res = client.getGroupList(options);
return res.toString();
}
/**
* 活体检测
*
* @param arg0
* @return
*/
public static String faceverify(byte[] arg0) {
String imgStr = Base64Util.encode(arg0);
String imageType = "BASE64";
FaceVerifyRequest req = new FaceVerifyRequest(imgStr, imageType);
ArrayList list = new ArrayList();
list.add(req);
JSONObject res = client.faceverify(list);
return res.toString();
}
public static byte[] FileToByte(File file) throws IOException {
// 将数据转为流
@SuppressWarnings("resource")
InputStream cOntent= new FileInputStream(file);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = content.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
// 获得二进制数组
return swapStream.toByteArray();
}

}






  • 点赞



  • 收藏



  • 分享




    • 文章举报






Cheng_Yong
发布了4 篇原创文章 · 获赞 2 · 访问量 49
私信

关注

推荐阅读
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • Java中包装类的设计原因以及操作方法
    本文主要介绍了Java中设计包装类的原因以及操作方法。在Java中,除了对象类型,还有八大基本类型,为了将基本类型转换成对象,Java引入了包装类。文章通过介绍包装类的定义和实现,解答了为什么需要包装类的问题,并提供了简单易用的操作方法。通过本文的学习,读者可以更好地理解和应用Java中的包装类。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • (三)多表代码生成的实现方法
    本文介绍了一种实现多表代码生成的方法,使用了java代码和org.jeecg框架中的相关类和接口。通过设置主表配置,可以生成父子表的数据模型。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • Node.js学习笔记(一)package.json及cnpm
    本文介绍了Node.js中包的概念,以及如何使用包来统一管理具有相互依赖关系的模块。同时还介绍了NPM(Node Package Manager)的基本介绍和使用方法,以及如何通过NPM下载第三方模块。 ... [详细]
  • ejava,刘聪dejava
    本文目录一览:1、什么是Java?2、java ... [详细]
  • 本文介绍了如何使用go语言实现一个一对一的聊天服务器和客户端,包括服务器开启、等待客户端连接、关闭连接等操作。同时提供了一个相关的多人聊天的链接供参考。 ... [详细]
  • 【Python 爬虫】破解按照顺序点击验证码(非自动化浏览器)
    #请求到验证码base64编码json_img_datajson_raw.get(Vimage)#获取到验证码编码 #保存验证码图片到本地defbase64_to_img(bstr ... [详细]
  • 【实例简介】【实例截图】【核心代码】1.核心模块o1.1.介绍o1.2.__builtin__模块o1.3.exceptions模块o1.4.os模块o1.5.os.path模块o ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Mono为何能跨平台
    概念JIT编译(JITcompilation),运行时需要代码时,将Microsoft中间语言(MSIL)转换为机器码的编译。CLR(CommonLa ... [详细]
author-avatar
天空的鸟儿飞
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有