Bootstrap

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

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

Alt

一、准备工作

1. 创建AK/SK

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

Alt
2. 购买人脸搜索服务

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

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

Alt
3.下载SDK

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

Alt
Alt
4.下载Eclipse并安装

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

二、实操开始

1.新建JAVA项目

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

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

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

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

Alt
2.装载京东云AI SDK

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

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

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

Alt
3.调试接口

创建分组(faceGroupCreate)

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

 package facesearch;
 
 import com.jdcloud.apigateway.signature.JdcloudSDKClient;
 import com.jdcloud.sdk.utils.BinaryUtils;
 import com.google.api.client.http.HttpResponse;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
import static com.jdcloud.sdk.http.Protocol.HTTP;

//创建分组
public class faceGroupCreate {
    public static void main(String[] args) {
        String accessKey = "请输入您的AK";
        String secretKey = "请输入您的SK";
        String endPoint = "aiapi.jdcloud.com";
        String path = "/jdai/faceGroupCreate";
        String method = "POST";
        Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        //queryMap.put("groupId", "10");
        queryMap.put("groupName", "请输入分组名称");
        queryMap.put("groupInfo", "请输入分组描述");
        String body = "\"\"";
        try {
            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
                endPoint, path, method, headers, queryMap, body);
            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

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

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

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.

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

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

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

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

注意: 如下代码中涉及到String body = “imageBase64=”;的部分

  • 需要先将图片转换为Base64,转换地址为:http://imgbase64.duoshitong.com/;
  • 然后将转换后的代码复制到imageBase64=之后(转换后的代码需去除掉“data:image/jpeg;base64,”后再复制)。

删除分组(faceGroupDelete

package facesearch;

import com.jdcloud.apigateway.signature.JdcloudSDKClient;
import com.jdcloud.sdk.utils.BinaryUtils;
import com.google.api.client.http.HttpResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import static com.jdcloud.sdk.http.Protocol.HTTP;

//删除分组
public class faceGroupDelete {
    public static void main(String[] args) {
        String accessKey = "请输入您的AK";
        String secretKey = "请输入您的SK";
        String endPoint = "aiapi.jdcloud.com";
        String path = "/jdai/faceGroupDelete";
        String method = "POST";
        Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        //queryMap.put("groupId", "10");
        queryMap.put("groupName", "请输入分组名称");
        String body = "{}";
        try {
            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
                endPoint, path, method, headers, queryMap, body);
            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
        } catch (IOException e) {
           System.out.println(e.getMessage());
        }
    }
}

分组列表获取(getFaceGroupList

package facesearch;

import com.jdcloud.apigateway.signature.JdcloudSDKClient;
import com.jdcloud.sdk.utils.BinaryUtils;
import com.google.api.client.http.HttpResponse;
 import java.io.IOException;
import java.util.HashMap;
 import java.util.Map;

import static com.jdcloud.sdk.http.Protocol.HTTP;

//获取分组列表
public class getFaceGroupList {
    public static void main(String[] args) {
        String accessKey = "请输入您的AK";
       String secretKey = "请输入您的SK";
        String endPoint = "aiapi.jdcloud.com";
        String path = "/jdai/getFaceGroupList";
        String method = "POST";
      Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("start", "0");
        queryMap.put("length", "5");
        String body = "aaa";
        try {
            HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
                endPoint, path, method, headers, queryMap, body);
            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

人脸创建(faceCreate

package facesearch;

import com.jdcloud.apigateway.signature.JdcloudSDKClient;
import com.jdcloud.sdk.utils.BinaryUtils;
import com.google.api.client.http.HttpResponse;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
import static com.jdcloud.sdk.http.Protocol.HTTP;

//创建人脸
public class faceCreate {
    public static void main(String[] args) {
        String accessKey = "请输入您的AK";
        String secretKey = "请输入您的SK";
        String endPoint = "aiapi.jdcloud.com";
        String path = "/jdai/faceCreate";
        String method = "POST";
        //创建
        Map<String, String> dataMap = new HashMap<>();
        //在线图片转base64:http://imgbase64.duoshitong.com/
       dataMap.put("marin1", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
        dataMap.put("marin2", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
        dataMap.put("corona", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
        dataMap.put("dog", "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)");
        Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("groupName", "请输入分组名称");
        String body;
        for (Map.Entry<String, String> entry: dataMap.entrySet()){
            queryMap.put("outerId", entry.getKey());
            body = entry.getValue();
            try {
                HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
                    endPoint, path, method, headers, queryMap, body);
                System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            queryMap.remove("outerId");
        }
    }
}

人脸删除(faceDelete

package facesearch;

import com.jdcloud.apigateway.signature.JdcloudSDKClient;
import com.jdcloud.sdk.utils.BinaryUtils;
import com.google.api.client.http.HttpResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.jdcloud.sdk.http.Protocol.HTTP;

//删除人脸
public class faceDelete {
   public static void main(String[] args) {
        String accessKey = "请输入您的AK";
        String secretKey = "请输入您的SK";
        String endPoint = "aiapi.jdcloud.com";
        String path = "/jdai/faceDelete";
        String method = "POST";
       Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("groupName", "请输入分组名称");
        queryMap.put("outerId", "marin1");
        queryMap.put("outerId", "marin2");
        queryMap.put("outerId", "corona");
       queryMap.put("outerId", "dog");
        String body = "{}";
        try {
           HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
                endPoint, path, method, headers, queryMap, body);
            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
        } catch (IOException e) {
            System.out.println(e.getMessage());
       }
    }
}

人脸搜索(faceSearch

package facesearch;

import com.jdcloud.apigateway.signature.JdcloudSDKClient;
import com.jdcloud.sdk.utils.BinaryUtils;
import com.google.api.client.http.HttpResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.jdcloud.sdk.http.Protocol.HTTP;

//人脸搜索
public class faceSearch {
    public static void main(String[] args) {
        String accessKey = "请输入您的AK";
        String secretKey = "请输入您的SK";
        String endPoint = "aiapi.jdcloud.com";
        String path = "/jdai/faceSearch";
        String method = "POST";
       Map<String, String> headers = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("groupName", "请输入分组名称");
        //如下填写同一人的第三张人脸Base64代码进行人脸搜索,这里用人脸marin.jpg
        String body = "imageBase64=图片转换为Base64后的代码(去掉前面的data:image/jpeg;base64,)";
        try {
           HttpResponse response = JdcloudSDKClient.execute(accessKey, secretKey, HTTP,
                endPoint, path, method, headers, queryMap, body);
            System.out.println(new String(BinaryUtils.toByteArray(response.getContent())));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

4.进行演示

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

创建分组

运行faceGroupCreate.java,结果如下:

Alt
获取分组列表

运行getFaceGroupList.java,结果如下:

Alt
创建人脸库

运行faceCreate.java,结果如下:

Alt
人脸搜索

运行faceSearch.java,结果如下:

Alt
删除人脸

运行faceDelete.java,结果如下:

Alt
删除分组

运行faceGroupDelete.java,结果如下:

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

点击“京东云”了解京东云人脸对比

欢迎点击“京东云”了解更多精彩内容。
Alt
Alt

;