人脸识别功能


前言

人脸识别是一种利用人脸特征进行身份识别的生物识别技术,它具有方便、快速、准确等优点,已经广泛应用于各个领域,如安防、支付、娱乐等。人脸识别的基本流程包括人脸检测、人脸对齐、人脸表征和人脸匹配四个步骤,其中人脸表征是提取人脸图像中的身份信息的关键环节。本篇文章主要介绍的是腾讯云人脸识别接口在项目中的使用。


一、服务开通

进入腾讯云官方网站,登录后找到人脸识别产品,点击开通.
https://cloud.tencent.com/
在这里插入图片描述

二、使用步骤

1. 新建人员库

在这里插入图片描述

2. 创建人脸识别功能组件

人脸识别请求支持HTTP请求

  • POST(推荐)
  • GET

我们需要三个接口完成人脸识别功能

  • 创建人员
  • 人员验证
  • 人员信息查询

3. 引入腾讯云sdk

<dependency>
	<groupId>com.tencentcloudapi</groupId>
	<artifactId>tencentcloud-sdk-java</artifactId>
	<version>3.1.548</version>
</dependency>

4. 创建 FaceComponent 人脸识别功能组件

@Data
@Component
public class FaceComponent {
	@Value("${teams.tencentcloud.secretId}")
	private String secretId;
	@Value("${teams.tencentcloud.secretKey}")
	private String secretKey;
	@Value("${teams.tencentcloud.groupId}")
	private String groupId;
}

5. 在 application.yml 文件中添加 secretId 和 secretKey 配置

secretId 和 secretKey在头像位置下拉框中的访问管理==>访问密钥中

teams:
	tencentcloud:
		secretId: xxxxxxxxxxx
		secretKey: xxxxxxxxxxxxx
		groupId: xxx

6. 编写创建人员接口方法

 //创建人员
    public Boolean createUser( String username, String userId, MultipartFile file){
        try{
            // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            CreatePersonRequest req = new CreatePersonRequest();
            req.setGroupId(groupId);
            req.setPersonName(username);
            req.setPersonId(userId);

            //设置图片base64编码
            BASE64Encoder encode = new BASE64Encoder();
            String imgStr = encode.encode(file.getBytes());
            req.setImage(imgStr);
            // 返回的resp是一个CreatePersonResponse的实例,与请求对象对应
            CreatePersonResponse resp = client.CreatePerson(req);
            String faceId = resp.getFaceId();
            if(StrUtil.isBlank(faceId)){
                return false;
            }else {
                return true;
            }
        } catch (TencentCloudSDKException e) {
            throw new TeamsException(e.getMessage());
        } catch (IOException e) {
            throw new TeamsException("图片IO错误");
        }
    }

7. 编写人员信息查询方法

//人员查询信息
    public Boolean getUserInfo(String userId){
        try{
            // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GetPersonBaseInfoRequest req = new GetPersonBaseInfoRequest();
            req.setPersonId(userId);
            // 返回的resp是一个GetPers
            // onBaseInfoResponse的实例,与请求对象对应
            GetPersonBaseInfoResponse resp = client.GetPersonBaseInfo(req);
            // 输出json格式的字符串回包
            String personName = resp.getPersonName();
            if(StrUtil.isBlank(personName)){
                return false;
            }else {
                return true;
            }
        } catch (TencentCloudSDKException e) {
            throw new TeamsException(e.getMessage());
        }
    }

8. 编写人脸验证方法

//人员验证
    public VerifyPersonResponse verifyUser(String userId, MultipartFile file) {
        try{
            // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("iai.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            IaiClient client = new IaiClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            VerifyPersonRequest req = new VerifyPersonRequest();
            //设置图片base64编码
            BASE64Encoder encode = new BASE64Encoder();
            String imgStr = encode.encode(file.getBytes());
            req.setImage(imgStr);
            req.setPersonId(userId);
            // 返回的resp是一个VerifyPersonResponse的实例,与请求对象对应
            // 输出json格式的字符串回包
            return  client.VerifyPerson(req);
        } catch (TencentCloudSDKException e) {
            throw new TeamsException(e.getMessage());
        } catch (IOException e) {
            throw new TeamsException("图片IO错误");
        }
    }

9. 编写模拟人脸识别签到方法

 public void checkin(HashMap hashMap) {
  		int userId = (Integer) hashMap.get("userId");
        Boolean userInfo = faceComponent.getUserInfo(userId + "");
        if (userInfo == null) {
            	System.out.println("不存在人脸模型");
        } else {
            VerifyPersonResponse resp = faceComponent.verifyUser(userId + "", (MultipartFile) hashMap.get("file"));
            Boolean isMatch = resp.getIsMatch();

            if (!isMatch) {
             	System.out.println("签到无效,非本人签到");
            } else {
               System.out.println("签到成功");
            }
        }
 }

如果不存在人脸模型则需要调用以下方法创建

 @Override
    public void createFaceModel(int userId, MultipartFile file) {
        TbUser tbUser = userMapper.selectById(userId);

        Boolean user = faceComponent.createUser(tbUser.getNickname(), userId + "", file);
    }

总结

本文章主要介绍了腾讯人脸识别接口在项目中的基本使用,更多详细信息可以参考官方文档。人脸识别接口是一套提供人脸检测、分析、识别等服务的API,它可以帮助开发者实现多种人脸相关的功能,如人脸核身、人脸融合、人像变换等。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值