您好,登录后才能下订单哦!
本文档将利用京东云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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。