乾貨 | AI人臉識別之人臉搜索

本文檔將利用京東雲AI SDK來實踐人臉識別中的人臉搜索功能,主要涉及到分組建立/刪除、分組列表獲取、人臉建立/刪除、人臉搜索,本次實操的最終效果是:建立一我的臉庫,拿一張圖片在人臉庫中搜索出類似度最高的一張,實現1:N的人臉識別,操做示意圖以下:html

1、準備工做

1. 建立AK/SK

登錄京東雲控制檯:https://console.jdcloud.com,點擊右上角帳戶AccessKey 管理,接着如圖點擊建立Access Keyjava

2. 購買人臉搜索服務

人臉搜索經過 API 調用次數計費,目前人臉搜索功能有 0元免費試用,調用量限制爲 13

咱們登錄到京東雲控制檯,依次點擊左側導航人工智能-人臉識別-人臉搜索,點擊當即購買完成購買操做。api

3.下載SDK

購買完成後,返回人臉搜索的控制檯,如圖點擊下載SDK完成京東雲AI SDK的下載操做eclipse

4.下載Eclipse並安裝

Eclipse下載地址:https://www.eclipse.org/downloads/,Eclipse安裝方法請自行百度。ui

2、實操開始

1.新建JAVA項目

打開Eclipse,依次點擊File-New-Java Project,輸入Project name以下圖設置後,點擊Finish-Don't Creategoogle

右擊JAVA項目中的src目錄,依次點擊new-Package人工智能

接下來咱們分別建立分組建立(faceGroupCreate)/刪除(faceGroupDelete)、分組列表獲取(getFaceGroupList)、人臉建立(faceCreate)/刪除(faceDelete)、人臉搜索(faceSearch)相關的(類)Class,新建Class的方法以下:spa

所有Class建立完成後以下圖:3d

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.jarjar包我已打包上傳到京東雲對象存儲,下載地址爲: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搜索了出來,至此,操做演示完畢~~

點擊「京東雲」瞭解京東雲人臉對比

歡迎點擊「連接」瞭解京東雲更多精彩內容

相關文章
相關標籤/搜索