百度Api人脸识别的使用,入门级使用,通过调用接口向百度云中添加人脸信息,附源码,视频教程看参考文档

1、整体文件

在这里插入图片描述

拿到demo需要更改的地方

在这里插入图片描述

  • 图片路径小技巧,就不用傻傻地自己写了
    在这里插入图片描述

注册百度云人脸识别Api流程,目前新人而言,实名之后百度云有免费的时候,直接拿去玩

在这里插入图片描述
在这里插入图片描述

运行可能会遇到的错误

  • 成功截图
    在这里插入图片描述
    在这里插入图片描述

  • 失败截图,(图片不合格导致失败)
    图片报错,说left eye is occlusion,意思就是左眼阻塞,其实就是图片不合格,不能作为图片识别的几个重要特征
    在这里插入图片描述
    在这里插入图片描述

参考文档

源码

  • FaceTest文件
package cn.itcast.baidu;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;
import org.junit.Test;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;


public class FaceTest {


    @Test
    public void testFaceRegister() throws Exception{
        //1.创建java代码和百度云交互的client对象
        AipFace client=new AipFace("26144507","aajSQhrf543534tkMuB7Nsn5","5UguEfcCoTzzaf43242XGjspQA71r");
        //传入参数
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("quality_control", "NORMAL");       //图片质量 NONE LOW NORMAL HIGH
        options.put("liveness_control", "LOW");         //活体检测
        //构造图片,自己电脑上图片的绝对路径
        String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\001.jpg";
        //上传的图片  两种格式  :  url格式   Base64字符串格式
        byte[] bytes = Files.readAllBytes(Paths.get(path));
        String encode = Base64Util.encode(bytes);
        
        //4.调用api方法完成人脸注册
        /**
         *参数一:(图片的url或者图片的Base64字符串),
         * 参数二:图片形式(URL,BASE64)
         * 参数三:组ID(固定字符串)
         * 参数四:用户ID
         * 参数五:hashMap中的基本参数配置
         */
        String imageType = "BASE64";
        String groupId = "itcast";
        String userId = "1000";
        JSONObject res = client.addUser(encode, imageType, groupId, userId, options);

        System.out.println(res.toString(2));
    }

}

  • pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>face-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>7</source>
                    <target>7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.baidu.aip</groupId>
            <artifactId>java-sdk</artifactId>
            <version>4.16.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
<!--            <scope>test</scope>-->
        </dependency>
    </dependencies>


</project>
  • 增更查代码(增加了其他的方法,更全面些)
package cn.itcast.baidu;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;


public class FaceTest {

    private AipFace client;

    @Before
    public void init(){
        client=new AipFace("26134907","aajSQhrf8756543MuB7Nsn5","5UguEfcCoTz3122fiXGjspQA71r");
    }


    /**
     * @describe: 人脸识别注册
     * @param:
     * @author: lijunyun
     * @date 2022/5/2 16:40
     **/

    @Test
    public void testFaceRegister() throws Exception{
        //传入参数
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("quality_control", "NORMAL");       //图片质量 NONE LOW NORMAL HIGH
        options.put("liveness_control", "LOW");         //活体检测
        //构造图片
        String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\002.jpg";
        //上传的图片  两种格式  :  url格式   Base64字符串格式
        byte[] bytes = Files.readAllBytes(Paths.get(path));
        String encode = Base64Util.encode(bytes);
        
        //4.调用api方法完成人脸注册
        /**
         *参数一:(图片的url或者图片的Base64字符串),
         * 参数二:图片形式(URL,BASE64)
         * 参数三:组ID(固定字符串)
         * 参数四:用户ID
         * 参数五:hashMap中的基本参数配置
         */
        String imageType = "BASE64";
        String groupId = "itcast";
        String userId = "1000";
        JSONObject res = client.addUser(encode, imageType, groupId, userId, options);
        System.out.println(res.toString(2));
    }

    /**
     * @describe:人脸检测:判断图片中是否具有面具信息
     * @param:
     * @author: lijunyun
     * @date 2022/5/2 16:41
     **/
    @Test
    public void testFaceCheck() throws Exception{
        //构造图片
        String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\006.jpg";
        //上传的图片  两种格式  :  url格式   Base64字符串格式
        byte[] bytes = Files.readAllBytes(Paths.get(path));
        String encode = Base64Util.encode(bytes);

        //4.调用api进行人脸检测
        /**
         *参数一:(图片的url或者图片的Base64字符串),
         * 参数二:图片形式(URL,BASE64)
         * 参数三:hashMap中的基本参数配置(null,表示使用默认配置)
         */
        String imageType = "BASE64";
        JSONObject res = client.detect(encode,imageType,null);
        System.out.println(res.toString(2));
    }

    /**
     * @describe: 人脸搜索,找到相似度最高的图片(或几个)
     * 说明:返回值(数据,只需要一条,相似度最高的数据),score:相似度评分(80分以上可以认为是同一个人)
     * @param:
     * @author: lijunyun
     * @date 2022/5/2 17:00
     **/
    @Test
    public void testFaceSearch() throws Exception{
        //构造图片
        String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\001.jpg";
        //上传的图片  两种格式  :  url格式   Base64字符串格式
        byte[] bytes = Files.readAllBytes(Paths.get(path));
        String encode = Base64Util.encode(bytes);

        //4.调用api进行人脸检测
        /**
         *参数一:(图片的url或者图片的Base64字符串),
         * 参数二:图片形式(URL,BASE64)
         * 参数三:groupId为组ID
         * 参数四:hashMap中的基本参数配置(null,表示使用默认配置)
         */
        String imageType = "BASE64";
        String groupId = "itcast";
        JSONObject res = client.search(encode,imageType,groupId,null);
        System.out.println(res.toString(2));
    }

    /**
     * @describe:  人脸更新:更新人脸库中的照片
     * @param:
     * @author: lijunyun
     * @date 2022/5/2 17:08
     **/
    @Test
    public void testFaceUpdate() throws Exception{
        //传入参数
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("quality_control", "NORMAL");       //图片质量 NONE LOW NORMAL HIGH
        options.put("liveness_control", "LOW");         //活体检测
        //构造图片
        String path="C:\\Users\\xiexie\\Pictures\\Saved Pictures\\人脸识别\\003.jpg";
        //上传的图片  两种格式  :  url格式   Base64字符串格式
        byte[] bytes = Files.readAllBytes(Paths.get(path));
        String encode = Base64Util.encode(bytes);

        //4.调用api方法完成人脸注册
        /**
         *参数一:(图片的url或者图片的Base64字符串),
         * 参数二:图片形式(URL,BASE64)
         * 参数三:组ID(固定字符串)
         * 参数四:用户ID
         * 参数五:hashMap中的基本参数配置
         */
        String imageType = "BASE64";
        String groupId = "itcast";
        String userId = "1000";
        JSONObject res = client.updateUser(encode, imageType, groupId, userId, options);
        System.out.println(res.toString(2));
    }
}

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在百度AI开放平台注册账号并创建一个人脸识别应用,获取API Key和Secret Key。 然后,在微信小程序使用wx.cloud.downloadFile()方法下载云存储的文件,并获取到文件的临时路径。 接着,将文件的临时路径作为参数,调用百度人脸识别API人脸检测接口,获取到人脸位置信息。 最后,将人脸位置信息作为参数,调用百度人脸识别API人脸注册接口,获取到face_token。具体实现过程如下: 1. 下载云存储的文件: ```javascript wx.cloud.downloadFile({ fileID: 'your fileID', success: res => { const filePath = res.tempFilePath // 调用人脸识别API detectFace(filePath) }, fail: err => { console.error(err) } }) ``` 2. 调用人脸检测接口,获取人脸位置信息: ```javascript function detectFace(filePath) { wx.showLoading({ title: '正在检测...', mask: true }) wx.request({ url: 'https://aip.baidubce.com/rest/2.0/face/v3/detect', data: { image: wx.getFileSystemManager().readFileSync(filePath, 'base64'), image_type: 'BASE64', face_field: 'location' }, header: { 'Content-Type': 'application/json' }, method: 'POST', success: res => { wx.hideLoading() if (res.data.error_code) { wx.showToast({ title: res.data.error_msg, icon: 'none' }) return } const faceToken = res.data.result.face_list[0].face_token // 将faceToken保存到数据库或其他地方 }, fail: err => { wx.hideLoading() console.error(err) } }) } ``` 3. 调用人脸注册接口,获取face_token: ```javascript function registerFace(faceLocation) { wx.showLoading({ title: '正在注册...', mask: true }) wx.request({ url: 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add', data: { image: wx.getFileSystemManager().readFileSync(filePath, 'base64'), image_type: 'BASE64', group_id: 'your group_id', user_id: 'your user_id', face_token: faceToken, liveness_control: 'HIGH', action_type: 'REPLACE' }, header: { 'Content-Type': 'application/json' }, method: 'POST', success: res => { wx.hideLoading() if (res.data.error_code) { wx.showToast({ title: res.data.error_msg, icon: 'none' }) return } const faceToken = res.data.result.face_token // 将faceToken保存到数据库或其他地方 }, fail: err => { wx.hideLoading() console.error(err) } }) } ``` 注意:在调用人脸识别API时,需要将API Key和Secret Key加入到请求头,并且需要将图片转换成base64格式。同时,需要注意API各个参数的含义和使用方法,具体可以参考百度AI开放平台的文档

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值