Face search for AI face recognition

Posted by Bennettman on Fri, 18 Oct 2019 06:50:14 +0200

This document will use the JD cloud AI SDK to practice the face search function in face recognition, mainly involving group creation / deletion, group list acquisition, face creation / deletion, and face search. The final effect of this operation is: create a face database, take a picture to search the most similar one in the face database, and realize 1: N face recognition. The operation diagram is as follows :

I. preparations

1. Create AK/SK

Log in to JD cloud console: https://console.jdcloud.com Click the account AccessKey management in the upper right corner, and then click create Access Key as shown in the figure

2. Purchase face search service

Face search is charged by the number of API calls. At present, face search functions include 0 yuan free trial, call limit is 13

We log in to the JD cloud console, click the left navigation Ai - face recognition - face search, and click buy now to complete the purchase operation.

3. download SDK

After purchase, return to the console of face search, as shown in the figure, click download SDK to complete the download operation of JD cloud AI SDK

4. Download Eclipse and install

Eclipse download address: https://www.eclipse.org/downloads/ , please install Eclipse by yourself.

II. Practical operation

1. New JAVA project

Open Eclipse, click file new Java project, enter Project name as shown in the figure below, and then click Finish don't create.

Right click the src directory in the JAVA project, and then click new package.

Next, we will create (Class) classes related to faceGroupCreate / delete, getFaceGroupList, faceCreate / delete, and faceSearch respectively. The method of creating a new Class is as follows:

After all classes are created, see the following figure:

2. Load JD cloud AI SDK

Unzip the JD cloud AI SDK we downloaded and copy it to the root directory of our new JAVA project

Refresh the Package Explorer in Eclipse to see the JD cloud AI SDK files we copied in. Select all jar package files, right-click, and then click Build Path add to Build Path to rebuild the path.

After the reconstruction path is completed, we can see the Referenced Libraries in the Package Explorer of Eclipse, which contains all jar packages of our reconstruction path.

3. Debugging interface

Create group (faceGroupCreate)

Enter the following as debugging code in the faceGroupCreate class

 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//Create grouping
13public class faceGroupCreate {
14    public static void main(String[] args) {
15        String accessKey = "Please enter your AK";
16        String secretKey = "Please enter your 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", "Please enter group name");
24        queryMap.put("groupInfo", "Please enter group description");
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}

Right click the code and click Run as-1 Java Application to run the code.

After running, the error message is found as follows (although there is an error message here, the group with the name of "please enter group name" defined by us has been created successfully):

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.

From this we can see that the main error reported is the slf4j jar package, while the Failed to load class "org.slf4j.impl.StaticLoggerBinder" in the fault code means that the class file org.slf4j.impl.StaticLoggerBinder failed to load.

We download slf4j-nop.jar, and then add it to the build path as we add the AI SDK jar package to solve the problem. I have packed and uploaded the slf4j-nop.jar package to the JD cloud object storage. The download address is: https://pocenv-hcc.s3.cn-north-1.jdcloud-oss.com/slf4j-nop-1.7.28.jar

After downloading the slf4j package, copy it to the JD cloud AI SDK folder, and then add it to the build path in Eclipse.

Next, create other class files in turn

Be careful:
The following code refers to Part of String body = "imageBase64 =";
You need to convert the picture to Base64 at the following address: http://imgbase64.duoshitong.com/
Then copy the converted code to imageBase64 = after (the converted code needs to be removed“ data:image/jpeg;base64.

Delete group (` 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//Delete grouping
13public class faceGroupDelete {
14    public static void main(String[] args) {
15        String accessKey = "Please enter your AK";
16        String secretKey = "Please enter your 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", "Please enter group name");
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}

Get group list (` 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//Get group list
13public class getFaceGroupList {
14    public static void main(String[] args) {
15        String accessKey = "Please enter your AK";
16        String secretKey = "Please enter your 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}

Face creation (` 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//Create face
13public class faceCreate {
14    public static void main(String[] args) {
15        String accessKey = "Please enter your AK";
16        String secretKey = "Please enter your SK";
17        String endPoint = "aiapi.jdcloud.com";
18        String path = "/jdai/faceCreate";
19        String method = "POST";
20        //Establish
21        Map<String, String> dataMap = new HashMap<>();
22        //Online picture to base64: http://imgbase64.duoshitong.com/
23        dataMap.put("marin1", "imageBase64=Picture to Base64 After the code (remove the previous data:image/jpeg;base64,)");
24        dataMap.put("marin2", "imageBase64=Picture to Base64 After the code (remove the previous data:image/jpeg;base64,)");
25        dataMap.put("corona", "imageBase64=Picture to Base64 After the code (remove the previous data:image/jpeg;base64,)");
26        dataMap.put("dog", "imageBase64=Picture to Base64 After the code (remove the previous data:image/jpeg;base64,)");
27        Map<String, String> headers = new HashMap<>();
28        Map<String, Object> queryMap = new HashMap<>();
29        queryMap.put("groupName", "Please enter group name");
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}

Face delete (` 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//Delete face
13public class faceDelete {
14    public static void main(String[] args) {
15        String accessKey = "Please enter your AK";
16        String secretKey = "Please enter your 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", "Please enter group name");
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}

Face search (` 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//Face search
13public class faceSearch {
14    public static void main(String[] args) {
15        String accessKey = "Please enter your AK";
16        String secretKey = "Please enter your 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", "Please enter group name");
23        //Fill in the code Base64 of the third face of the same person for face search as follows. Here, use face marin.jpg
24        String body = "imageBase64=Picture to Base64 After the code (remove the previous 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. Demonstrate

The following demonstrations are executed by right clicking the corresponding code Run as- 1 Java Application to run the code to view the results

Create grouping

Run faceGroupCreate.java, and the result is as follows:

Get group list

Run getFaceGroupList.java, and the result is as follows:

Create face database

Run faceCreate.java, and the result is as follows:

Face search

Run faceSearch.java, and the result is as follows:

Delete face

Run faceDelete.java, and the result is as follows:

Delete grouping

Run faceGroupDelete.java, and the result is as follows:

As mentioned above, we created a face database through marin1.jpg, marin2.jpg, corona.jpg and dog.jpg. Finally, we searched out marin1.jpg with the highest similarity through marin.jpg. So far, the operation demonstration is completed.~~

Click " Jingdong cloud ”Learn about face comparison of JD cloud

 

Topics: Java SDK Eclipse Google