1. 建立AK/SKhtml
登錄京東雲控制檯:console.jdcloud.com,點擊右上角帳戶AccessKey 管理,接着如圖點擊建立Access Keyjava
2. 購買人臉搜索服務人臉搜索經過 API 調用次數計費,目前人臉搜索功能有0元免費試用,調用量限制爲13api
咱們登錄到京東雲控制檯,依次點擊左側導航人工智能-人臉識別-人臉搜索,點擊當即購買完成購買操做。bash
3.下載SDK購買完成後,返回人臉搜索的控制檯,如圖點擊下載SDK完成京東雲AI SDK的下載操做eclipse
4.下載Eclipse並安裝Eclipse下載地址:www.eclipse.org/downloads/,…ui
1.新建JAVA項目google
打開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項目的根目錄裏spa
刷新Eclipse中的Package Explorer即可看到咱們複製進來的京東雲AI SDK文件,選中所有jar包文件,右擊,依次點擊Build Path-Add to Build Path來從新構建路徑(Build Path) 從新構建路徑完成後,咱們在Eclipse的Package Explorer中可看到Referenced Libraries,裏面包含咱們重構路徑的全部jar包 3.調試接口建立分組(faceGroupCreate)3d
在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());
}
}
}
複製代碼
右擊代碼依次點擊Run as-1 Java Application運行代碼
運行後發現報錯信息以下(
這裏雖然有報錯,但咱們定義的名稱爲請輸入分組名稱的組已成功建立):
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See www.slf4j.org/codes.html#… 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包我已打包上傳到京東雲對象存儲,下載地址爲:pocenv-hcc.s3.cn-north-1.jdcloud-oss.com/slf4j-nop-1…
將slf4j包下載後複製到京東雲AI SDK文件夾裏,而後在Eclipse內添加到構建路徑裏便可
接下來依次建立其餘類文件注意: 以下代碼中涉及到String body = "imageBase64=";的部分
- 須要先將圖片轉換爲Base64,轉換地址爲: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,結果以下:
獲取分組列表運行getFaceGroupList.java,結果以下:
建立人臉庫運行faceCreate.java,結果以下:
人臉搜索運行faceSearch.java,結果以下:
刪除人臉運行faceDelete.java,結果以下:
刪除分組運行faceGroupDelete.java,結果以下:
如上,咱們經過marin1.jpg、marin2.jpg、corona.jpg、dog.jpg建立了人臉庫,最後經過marin.jpg將類似度最高的marin1.jpg搜索了出來,至此,操做演示完畢~~點擊「京東雲」瞭解京東雲人臉對比