人脸识别——基于百度智能云实现

百度智能云是百度旗下面向企业、开发者和政府机构的智能云计算服务商,致力于为各行业提供以ABC(人工智能、大数据、云计算)技术为一体的平台服务。

对于开发者而言,灵活利用其提供的大量模块化接口,可快速完成相关的开发,大大缩短开发周期。

本文将对其人脸识别的接口进行利用,并实现几个实例。对这些实例核心进行拓展将能实现更广泛的应用。基于百度深度学习能力的人脸识别技术,提供人脸检测与属性分析、人脸1:1对比、人脸搜索、活体检测等能力。灵活应用于金融、泛安防、零售等行业场景,满足身份核验、人脸考勤、闸机通行等业务需求。

4个实例:

faceDao.faceDetect(client);//检测是否是人,评测人像各类指标
        faceDao.faceMatch(client);//检测两个图片的人物匹配度
        faceDao.faceSerch(client);//检索一张人脸是否在人脸库中
        faceDao.faceUpdate(client);//更新人脸库

思路:

工程中建立4个类:

AiFaceMain类:主函数类,用于调用FaceDao类中实现的各种具体方法。

FaceDao:实现各种人脸识别的具体方法:人脸检测,人脸对比,库中检索特定人脸,对人脸库的操作。

ClientUtils:初始化类,初始化AipFace,获得AipFace client。登入AipFace。

ImageUtils:图片转化工具类,将本地图片转换为base64流,最后转为String提供给FaceDao中的各类方法使用。这里是因为百度API中要求图片要以Base64编码进行传输,请求的图片需经过Base64编码,图片的base64编码指将图片数据编码成一串字符串,使用该字符串代替图像地址。


上干货,代码:

工程结构:

AiFaceMain类:

package com.sevenh.Dao;

import java.io.IOException;

import com.baidu.aip.face.AipFace;
import com.sevenh.Util.ClientUtils;

public class AiFaceMain {
	public static void main(String[] args) {
		ClientUtils cl = new ClientUtils();
		AipFace client=cl.getClient();
		FaceDao faceDao = new FaceDao();
		try {
			
			//faceDao.faceDetect(client);//检测是否是人,评测人像各类指标
			  faceDao.faceMatch(client);//检测两个图片的人物匹配度
			//faceDao.faceSerch(client);//从人脸库中找到最匹配的一个
                        //faceDao.faceUpdate(client);//更新人脸信息
                        //faceDao.deletUserGroup(client);//删库
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

 

FaceDao类: 

public class FaceDao {
	/**
	 * 人脸检测
	 * @param client 
	 * ClientUtils cl = new ClientUtils();
			AipFace client=cl.getClient();
			FaceDao faceDao = new FaceDao();
			faceDao.faceDetect(client);
	 * @throws IOException
	 */
	public void faceDetect(AipFace client) throws IOException{
		 String src=ImageUtils.image2Base64("E:\\zm\\人脸识别\\勒布朗.png");
    	 String image = src;
         String imageType = "BASE64";
     
         // 人脸检测
      // 传入可选参数调用接口
         HashMap<String, String> options = new HashMap<String, String>();
         options.put("face_field", "age,emotion,gender");
         
         options.put("max_face_num", "2");
         options.put("face_type", "LIVE");
         options.put("liveness_control", "NONE");

         JSONObject res = client.detect(image, imageType, options);
         
         JSONObject jD=res.getJSONObject("result");
         JSONArray stus=jD.getJSONArray("face_list");
         JSONObject ty=stus.getJSONObject(0);
         
         System.out.println("年龄:"+ty.get("age"));
         System.out.println("情绪:"+ty.get("emotion"));
         System.out.println("性别:"+ty.get("gender"));
         System.out.println(res.toString(2));
	}
	/**
	 * 人脸对比
	 * @param client
	 * @throws IOException
	 */
	public void faceMatch(AipFace client) throws IOException{
		String image1;
		String image2;
		String imageType;
		image1 = ImageUtils.image2Base64("C:\\Users\\ayyk\\Desktop\\人脸识别\\勒布朗.jpg");
		image2 = ImageUtils.image2Base64("C:\\Users\\ayyk\\Desktop\\人脸识别\\勒布朗没胡子.jpg");
		imageType = "BASE64";
		
	    MatchRequest req1 = new MatchRequest(image1, imageType);
	    MatchRequest req2 = new MatchRequest(image2, imageType);
	    ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
	    requests.add(req1);
	    requests.add(req2);

	    JSONObject res = client.match(requests);
	    JSONObject jD=res.getJSONObject("result");
	    Double sc=(Double) jD.get("score");
	    System.out.println("相似度:"+sc);
	    if(sc>80) {
	    	System.out.println("是同一个人");
	    }else {System.out.println("不是同一个人");}
	    System.out.println(res.toString());
	}
	/**
	 * 在人脸库中搜索人脸
	 * @param client
	 * @throws IOException
	 */
	public void faceSerch(AipFace client) throws IOException {
		// 传入可选参数调用接口
	    HashMap<String, String> options = new HashMap<String, String>();//在哈希表(hash table)中缓存放入相关字段对应的值来进行参数设置。
	    options.put("max_face_num", "1");
	    options.put("match_threshold", "10");
	    options.put("quality_control", "NORMAL");
	    //options.put("liveness_control", "LOW");
	    //options.put("user_id", "233451");
	    options.put("max_user_num", "1");
	    
	    String image = ImageUtils.image2Base64("E:\\zm\\人脸识别\\人脸对比\\dd.jpg");
	    String imageType = "BASE64";
	    String groupIdList = "test";
	    
	    // 人脸搜索
	    JSONObject res = client.search(image, imageType, groupIdList, options);
	    System.out.println(res.toString(2));//这里的toString(2)是json格式缩进2个字符
	}
         /**
	 * 人脸更新
	 * @param client
	 * @throws IOException
	 */
	public void faceUpdate(AipFace client) throws IOException{
		// 传入可选参数调用接口
	    HashMap<String, String> options = new HashMap<String, String>();
	    //options.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
	    options.put("group_id", "test");
	    options.put("user_id", "LeBron");
	    options.put("user_info", "勒布朗·詹姆斯(LeBron James),1984年12月30日出生于美国俄亥俄州阿克伦(Akron, Ohio),美国职业篮球运动员,司职小前锋,效力于NBA洛杉矶湖人队。");
	    //options.put("liveness_control", "NORMAL");
	    //options.put("image_type", "FACE_TOKEN");
	    options.put("quality_control", "LOW");
	    
	    String image = ImageUtils.image2Base64("E:\\zm\\人脸识别\\勒布朗.png");
	    String imageType = "BASE64";
	    String groupIdList = "test";
	    String userId = "LeBron";
	    JSONObject res=client.updateUser(image, imageType, groupIdList, userId, options);
	    System.out.println("更新成功:"+res.toString(2));
	}

	/**
	 * 人脸库删除
	 * @param client
	 */
	public void deletUserGroup(AipFace client) throws IOException{
	    // 传入可选参数调用接口
	    HashMap<String, String> options = new HashMap<String, String>();
	    
	    String groupId = "sevenh";
	    
	    // 删除用户组
	    JSONObject res = client.groupDelete(groupId, options);
	    System.out.println(res.toString(2));

	}


}

ClientUtils类:

public class ClientUtils {
	 //设置APPID/AK/SK
    public static final String APP_ID = "16578793";
    public static final String API_KEY = "aRQYOMsXk3k7AUFDoZjVV2Td";
    public static final String SECRET_KEY = "wgPvo64xQo3mSQlLwzVbGEWGx19douwh";
    /**
     * 初始化AipFace,获得AipFace client。登入AipFace。
     * @return
     */
    public AipFace getClient() {
    	AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
    	client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
		return client;
    }
}

 ImageUtils类:

public class ImageUtils {
/**
 * 将本地图片转换为base64流,最后转为String。
 * @param imageFilePath 本地图片地址参数
 * @return 返回的是图片流的字符串。
 * @throws IOException
 */
    public static String image2Base64(String imageFilePath) throws IOException {

        FileInputStream fis = new FileInputStream(new File(imageFilePath));

        byte[] bytes = new byte[fis.available()];.available()这个方法可以在读写操作前先得知数据流里有多少个字节可以读取
        fis.read(bytes);// 从一个输入流中读取一定数量的字节,并将这些字节存储到其缓冲作用的数组bytes中。      
        //static BASE64Encoder encoder = new BASE64Encoder();这是以前的旧方法。jdk9后弃用。
        //String base64String = encoder.encode(bytes);        
        Encoder encoder = Base64.getEncoder();
        String base64String = encoder.encodeToString(bytes);
        System.out.println("base64String: " + base64String);//输出图片变成的字符串来看看
        return base64String;
    }

 


 运行结果:

1.人脸检测:

接口能力

  • 人脸检测:检测图片中的人脸并标记出位置信息;
  • 人脸关键点:展示人脸的核心关键点信息,及150个关键点信息。
  • 人脸属性值:展示人脸属性信息,如年龄、性别等。
  • 人脸质量信息:返回人脸各部分的遮挡、光照、模糊、完整度、置信度等信息。

 

2.人脸对比:

接口能力

  • 两张人脸图片相似度对比:比对两张图片中人脸的相似度,并返回相似度分值;
  • 多种图片类型:支持生活照证件照身份证芯片照带网纹照四种类型的人脸对比;
  • 活体检测控制:基于图片中的破绽分析,判断其中的人脸是否为二次翻拍(举例:如用户A用手机拍摄了一张包含人脸的图片一,用户B翻拍了图片一得到了图片二,并用图片二伪造成用户A去进行识别操作,这种情况普遍发生在金融开户、实名认证等环节。);
  • 质量检测控制:分析图片的中人脸的模糊度、角度、光照强度等特征,判断图片质量;

业务应用

用于比对多张图片中的人脸相似度并返回两两比对的得分,可用于判断两张脸是否是同一人的可能性大小。

典型应用场景:如人证合一验证用户认证等,可与现有的人脸库进行比对验证。

 

3.特定人脸在人脸库中检索

在人脸库(test)中查找是否有“猜猜我是谁.png”图片中的人物。

结果:

4.对人脸库的增删改查(以更新人脸信息为例)

 百度智能云平台当前已经开放了身份识别、公安验证、视频活体检测等底层API,根据实际业务情况可直接接入相关的API,方便快速集成开发使用,减少开发工作量,加快开发速度。

注:本文着重介绍的是工程结构和代码的具体实现,开发环境的建立请见另一篇文章。

 

  • 6
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值