干货 | AI人脸识别之人脸搜索

本文档将利用京东云AI SDK来实践人脸识别中的人脸搜索功能,主要涉及到分组创建/删除、分组列表获取、人脸创建/删除、人脸搜索,本次实操的最终效果是: 创建一个人脸库,拿一张图片在人脸库中搜索出相似度最高的一张,实现1:N的人脸识别 ,操作示意图如下:

一、准备工作

1. 创建AK/SK

登陆京东云控制台:https://console.jdcloud.com,点击右上角账户 AccessKey 管理 ,接着如图点击 创建Access Key

2. 购买人脸搜索服务

人脸搜索通过 API 调用次数计费,目前人脸搜索功能有 0元免费试用 ,调用量限制为 13

我们登陆到京东云控制台,依次点击左侧导航 人工智能 - 人脸识别 - 人脸搜索 ,点击 立即购买 完成购买操作。

3.下载SDK

购买完成后,返回人脸搜索的控制台,如图点击 下载SDK 完成京东云AI SDK的下载操作

4.下载Eclipse并安装

Eclipse下载地址:https://www.eclipse.org/downloads/,Eclipse安装方法请自行百度。

二、实操开始

1.新建JAVA项目

打开Eclipse,依次点击 File - New - Java Project ,输入 Project name 如下图设置后,点击 Finish - Don't Create

右击JAVA项目中的 src 目录,依次点击 new - Package

接下来我们分别创建分组创建( faceGroupCreate )/删除( faceGroupDelete )、分组列表获取( getFaceGroupList )、人脸创建( faceCreate )/删除( faceDelete )、人脸搜索( faceSearch )相关的 (类)Class ,新建 Class 的方法如下:

全部Class创建完成后如下图:

2.装载京东云AI SDK

将我们下载好的京东云AI SDK解压,然后复制到我们新建的JAVA项目的根目录里

刷新Eclipse中的 Package Explorer 便可看到我们复制进来的京东云AI SDK文件,选中全部jar包文件,右击,依次点击 Build Path - Add to Build Path 来重新构建路径(Build Path)

重新构建路径完成后,我们在Eclipse的 Package Explorer 中可看到 Referenced Libraries ,里面包含我们重构路径的所有jar包

3.调试接口

创建分组(faceGroupCreate)

faceGroupCreate 类中输入如下内容作为调试代码

 1package facesearch;
 2
 3import com.jdcloud.apigateway.signature.JdcloudSDKClient;
 4import com.jdcloud.sdk.utils.BinaryUtils;
 5import com.google.api.client.http.HttpResponse;
 6import java.io.IOException;
 7import java.util.HashMap;
 8import java.util.Map;
 9
10import static com.jdcloud.sdk.http.Protocol.HTTP;
11
12//创建分组
13public class faceGroupCreate {
14    public static void main(String[] args) {
15        String accessKey = "请输入您的AK";
16        String secretKey = "请输入您的SK";
17        String endPoint = "aiapi.jdcloud.com";
18        String path = "/jdai/faceGroupCreate";
19        String method = "POST";
20        Map<String, String> headers = new HashMap<>();
21        Map<String, Object> queryMap = new HashMap<>();
22        //queryMap.put("groupId", "10");
23        queryMap.put("groupName", "请输入分组名称");
24        queryMap.put("groupInfo", "请输入分组描述");
25        String body = "\"\"";
26        try {
27            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
28                endPoint, path, method, headers, queryMap, body);
29            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
30        } catch (IOException e) {
31            System.out.println(e.getMessage());
32        }
33    }
34}

右击代码依次点击 Run as - 1 Java Application 运行代码

运行后发现报错信息如下( 这里虽然有报错,但我们定义的名称为 请输入分组名称 的组已成功创建 ):

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

由此我们可以看出,报出错误的地方主要是slf4j的jar包,而故障码中 Failed to load class "org.slf4j.impl.StaticLoggerBinder" 的意思则是 加载类文件org.slf4j.impl.StaticLoggerBinder时失败

我们下载 slf4j-nop.jar ,然后像添加AI SDK jar包一样添加到build path中即可解决问题, slf4j-nop.jar jar包我已打包上传到京东云对象存储,下载地址为:https://pocenv-hcc.s3.cn-north-1.jdcloud-oss.com/slf4j-nop-1.7.28.jar

将slf4j包下载后复制到京东云AI SDK文件夹里,然后在Eclipse内添加到构建路径里即可

接下来依次创建其他类文件

注意:

如下代码中涉及到 String body = "imageBase64="; 的部分

  • 需要先将图片转换为Base64,转换地址为:http://imgbase64.duoshitong.com/;

  • 然后将转换后的代码复制到 imageBase64= 之后(转换后的代码需去除掉“ data:image/jpeg;base64, ”后再复制)。

删除分组(`faceGroupDelete`)

 1package facesearch;
 2
 3import com.jdcloud.apigateway.signature.JdcloudSDKClient;
 4import com.jdcloud.sdk.utils.BinaryUtils;
 5import com.google.api.client.http.HttpResponse;
 6import java.io.IOException;
 7import java.util.HashMap;
 8import java.util.Map;
 9
10import static com.jdcloud.sdk.http.Protocol.HTTP;
11
12//删除分组
13public class faceGroupDelete {
14    public static void main(String[] args) {
15        String accessKey = "请输入您的AK";
16        String secretKey = "请输入您的SK";
17        String endPoint = "aiapi.jdcloud.com";
18        String path = "/jdai/faceGroupDelete";
19        String method = "POST";
20        Map<String, String> headers = new HashMap<>();
21        Map<String, Object> queryMap = new HashMap<>();
22        //queryMap.put("groupId", "10");
23        queryMap.put("groupName", "请输入分组名称");
24        String body = "{}";
25        try {
26            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
27                endPoint, path, method, headers, queryMap, body);
28            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
29        } catch (IOException e) {
30            System.out.println(e.getMessage());
31        }
32    }
33}

分组列表获取(`getFaceGroupList`)

 1package facesearch;
 2
 3import com.jdcloud.apigateway.signature.JdcloudSDKClient;
 4import com.jdcloud.sdk.utils.BinaryUtils;
 5import com.google.api.client.http.HttpResponse;
 6import java.io.IOException;
 7import java.util.HashMap;
 8import java.util.Map;
 9
10import static com.jdcloud.sdk.http.Protocol.HTTP;
11
12//获取分组列表
13public class getFaceGroupList {
14    public static void main(String[] args) {
15        String accessKey = "请输入您的AK";
16        String secretKey = "请输入您的SK";
17        String endPoint = "aiapi.jdcloud.com";
18        String path = "/jdai/getFaceGroupList";
19        String method = "POST";
20        Map<String, String> headers = new HashMap<>();
21        Map<String, Object> queryMap = new HashMap<>();
22        queryMap.put("start", "0");
23        queryMap.put("length", "5");
24        String body = "aaa";
25        try {
26            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
27                endPoint, path, method, headers, queryMap, body);
28            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
29        } catch (IOException e) {
30            System.out.println(e.getMessage());
31        }
32    }
33}

人脸创建(`faceCreate`)

 1package facesearch;
 2
 3import com.jdcloud.apigateway.signature.JdcloudSDKClient;
 4import com.jdcloud.sdk.utils.BinaryUtils;
 5import com.google.api.client.http.HttpResponse;
 6import java.io.IOException;
 7import java.util.HashMap;
 8import java.util.Map;
 9
10import static com.jdcloud.sdk.http.Protocol.HTTP;
11
12//创建人脸
13public class faceCreate {
14    public static void main(String[] args) {
15        String accessKey = "请输入您的AK";
16        String secretKey = "请输入您的SK";
17        String endPoint = "aiapi.jdcloud.com";
18        String path = "/jdai/faceCreate";
19        String method = "POST";
20        //创建
21        Map<String, String> dataMap = new HashMap<>();
22        //在线图片转base64:http://imgbase64.duoshitong.com/
23        dataMap.put("marin1", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
24        dataMap.put("marin2", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
25        dataMap.put("corona", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
26        dataMap.put("dog", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
27        Map<String, String> headers = new HashMap<>();
28        Map<String, Object> queryMap = new HashMap<>();
29        queryMap.put("groupName", "请输入分组名称");
30        String body;
31        for (Map.Entry<String, String> entry: dataMap.entrySet()){
32            queryMap.put("outerId", entry.getKey());
33            body = entry.getValue();
34            try {
35                HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
36                    endPoint, path, method, headers, queryMap, body);
37                System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
38            } catch (IOException e) {
39                System.out.println(e.getMessage());
40            }
41            queryMap.remove("outerId");
42        }
43    }
44}

人脸删除(`faceDelete`)

 1package facesearch;
 2
 3import com.jdcloud.apigateway.signature.JdcloudSDKClient;
 4import com.jdcloud.sdk.utils.BinaryUtils;
 5import com.google.api.client.http.HttpResponse;
 6import java.io.IOException;
 7import java.util.HashMap;
 8import java.util.Map;
 9
10import static com.jdcloud.sdk.http.Protocol.HTTP;
11
12//删除人脸
13public class faceDelete {
14    public static void main(String[] args) {
15        String accessKey = "请输入您的AK";
16        String secretKey = "请输入您的SK";
17        String endPoint = "aiapi.jdcloud.com";
18        String path = "/jdai/faceDelete";
19        String method = "POST";
20        Map<String, String> headers = new HashMap<>();
21        Map<String, Object> queryMap = new HashMap<>();
22        queryMap.put("groupName", "请输入分组名称");
23        queryMap.put("outerId", "marin1");
24        queryMap.put("outerId", "marin2");
25        queryMap.put("outerId", "corona");
26        queryMap.put("outerId", "dog");
27        String body = "{}";
28        try {
29            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
30                endPoint, path, method, headers, queryMap, body);
31            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
32        } catch (IOException e) {
33            System.out.println(e.getMessage());
34        }
35    }
36}

人脸搜索(`faceSearch`)

 1package facesearch;
 2
 3import com.jdcloud.apigateway.signature.JdcloudSDKClient;
 4import com.jdcloud.sdk.utils.BinaryUtils;
 5import com.google.api.client.http.HttpResponse;
 6import java.io.IOException;
 7import java.util.HashMap;
 8import java.util.Map;
 9
10import static com.jdcloud.sdk.http.Protocol.HTTP;
11
12//人脸搜索
13public class faceSearch {
14    public static void main(String[] args) {
15        String accessKey = "请输入您的AK";
16        String secretKey = "请输入您的SK";
17        String endPoint = "aiapi.jdcloud.com";
18        String path = "/jdai/faceSearch";
19        String method = "POST";
20        Map<String, String> headers = new HashMap<>();
21        Map<String, Object> queryMap = new HashMap<>();
22        queryMap.put("groupName", "请输入分组名称");
23        //如下填写同一人的第三张人脸Base64代码进行人脸搜索,这里用人脸marin.jpg
24        String body = "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)";
25        try {
26            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
27                endPoint, path, method, headers, queryMap, body);
28            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
29        } catch (IOException e) {
30            System.out.println(e.getMessage());
31        }
32    }
33}

4.进行演示

如下演示都通过右击对应的代码执行 Run as - 1 Java Application 来运行代码查看结果

创建分组

运行 faceGroupCreate.java ,结果如下:

获取分组列表

运行 getFaceGroupList.java ,结果如下:

创建人脸库

运行 faceCreate.java ,结果如下:

人脸搜索

运行 faceSearch.java ,结果如下:

删除人脸

运行 faceDelete.java ,结果如下:

删除分组

运行 faceGroupDelete.java ,结果如下:

如上,我们通过 marin1.jpgmarin2.jpgcorona.jpgdog.jpg 创建了人脸库,最后通过 marin.jpg 将相似度最高的 marin1.jpg 搜索了出来,至此,操作演示完毕~~

我来评几句
登录后评论

已发表评论数()

相关站点

热门文章