您好,登錄后才能下訂單哦!
本文檔將利用京東云AI SDK來實踐人臉識別中的人臉搜索功能,主要涉及到分組創建/刪除、分組列表獲取、人臉創建/刪除、人臉搜索,本次實操的最終效果是: 創建一個人臉庫,拿一張圖片在人臉庫中搜索出相似度最高的一張,實現1:N的人臉識別,操作示意圖如下:
人臉搜索通過 API 調用次數計費,目前人臉搜索功能有 0元免費試用,調用量限制為 13
右擊JAVA項目中的 src目錄,依次點擊 new- Package
接下來我們分別創建分組創建( faceGroupCreate)/刪除( faceGroupDelete)、分組列表獲取( getFaceGroupList)、人臉創建( faceCreate)/刪除( faceDelete)、人臉搜索( faceSearch)相關的 (類)Class,新建 Class的方法如下:
全部Class創建完成后如下圖:
刷新Eclipse中的 Package Explorer便可看到我們復制進來的京東云AI SDK文件,選中全部jar包文件,右擊,依次點擊 Build Path- Add to Build Path來重新構建路徑(Build Path)
重新構建路徑完成后,我們在Eclipse的 Package Explorer中可看到 Referenced Libraries,里面包含我們重構路徑的所有jar包
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的jar包,而故障碼中 Failed to load class "org.slf4j.impl.StaticLoggerBinder"的意思則是 加載類文件org.slf4j.impl.StaticLoggerBinder時失敗
接下來依次創建其他類文件
注意:如下代碼中涉及到 String body = "imageBase64=";的部分 需要先將圖片轉換為Base64,轉換地址為:http://imgbase64.duoshitong.com/; 然后將轉換后的代碼復制到 imageBase64=之后(轉換后的代碼需去除掉“ data:image/jpeg;base64,”后再復制)。
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}
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}
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}
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}
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}
如下演示都通過右擊對應的代碼執行 Run as- 1 Java Application來運行代碼查看結果
如上,我們通過 marin1.jpg、 marin2.jpg、 corona.jpg、 dog.jpg創建了人臉庫,最后通過 marin.jpg將相似度最高的 marin1.jpg搜索了出來,至此,操作演示完畢~~
點擊“
京東云
”了解京東云人臉對比
歡迎點擊“ 京東云 ”了解更多精彩內容
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。