1. 建立AK/SKhtml
登录京东云控制台:console.jdcloud.com,点击右上角帐户AccessKey 管理,接着如图点击建立Access Keyjava
人脸搜索经过 API 调用次数计费,目前人脸搜索功能有0元免费试用,调用量限制为13api
咱们登录到京东云控制台,依次点击左侧导航人工智能-人脸识别-人脸搜索,点击当即购买完成购买操做。bash
购买完成后,返回人脸搜索的控制台,如图点击下载SDK完成京东云AI SDK的下载操做eclipse
Eclipse下载地址:www.eclipse.org/downloads/,…ui
1.新建JAVA项目google
打开Eclipse,依次点击File-New-Java Project,输入Project name以下图设置后,点击Finish-Don't Create人工智能
将咱们下载好的京东云AI SDK解压,而后复制到咱们新建的JAVA项目的根目录里spa
建立分组(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());
}
}
}
复制代码
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-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,结果以下:
点击“京东云”了解京东云人脸对比