|
@@ -1,18 +1,41 @@
|
1
|
1
|
package com.huiju.welcome.utils;
|
2
|
2
|
|
|
3
|
+import com.alibaba.fastjson.JSON;
|
|
4
|
+import com.alibaba.fastjson.JSONArray;
|
3
|
5
|
import com.alibaba.fastjson.JSONObject;
|
4
|
6
|
import com.aliyuncs.CommonRequest;
|
5
|
7
|
import com.aliyuncs.CommonResponse;
|
6
|
8
|
import com.aliyuncs.DefaultAcsClient;
|
|
9
|
+import com.aliyuncs.IAcsClient;
|
|
10
|
+import com.aliyuncs.exceptions.ClientException;
|
|
11
|
+import com.aliyuncs.exceptions.ServerException;
|
|
12
|
+import com.aliyuncs.green.model.v20180509.AddFacesRequest;
|
|
13
|
+import com.aliyuncs.green.model.v20180509.AddPersonRequest;
|
|
14
|
+import com.aliyuncs.green.model.v20180509.GetPersonRequest;
|
|
15
|
+import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
|
|
16
|
+import com.aliyuncs.http.FormatType;
|
|
17
|
+import com.aliyuncs.http.HttpResponse;
|
7
|
18
|
import com.aliyuncs.http.MethodType;
|
8
|
19
|
import com.aliyuncs.profile.DefaultProfile;
|
9
|
20
|
import com.huiju.welcome.config.AliProperties;
|
|
21
|
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
22
|
+import com.tencentcloudapi.iai.v20180301.models.GetPersonBaseInfoResponse;
|
10
|
23
|
import lombok.extern.slf4j.Slf4j;
|
11
|
24
|
import org.apache.http.HttpStatus;
|
12
|
25
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
26
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
14
|
27
|
import org.springframework.context.annotation.Configuration;
|
15
|
28
|
|
|
29
|
+import java.io.UnsupportedEncodingException;
|
|
30
|
+import java.util.ArrayList;
|
|
31
|
+import java.util.Arrays;
|
|
32
|
+import java.util.Date;
|
|
33
|
+import java.util.HashMap;
|
|
34
|
+import java.util.LinkedHashMap;
|
|
35
|
+import java.util.List;
|
|
36
|
+import java.util.Map;
|
|
37
|
+import java.util.UUID;
|
|
38
|
+
|
16
|
39
|
@Slf4j
|
17
|
40
|
@Configuration
|
18
|
41
|
@EnableConfigurationProperties(AliProperties.class)
|
|
@@ -23,11 +46,253 @@ public class AliFaceUtils {
|
23
|
46
|
|
24
|
47
|
DefaultProfile profile;
|
25
|
48
|
DefaultAcsClient client;
|
26
|
|
-
|
|
49
|
+ IAcsClient recognitionClient;
|
|
50
|
+
|
|
51
|
+
|
27
|
52
|
@Autowired
|
28
|
53
|
public AliFaceUtils(AliProperties p) {
|
29
|
54
|
profile = DefaultProfile.getProfile(AliProperties.face.getRegionId(), AliProperties.accessKeyId, AliProperties.accessKeySecret);
|
30
|
55
|
client = new DefaultAcsClient(profile);
|
|
56
|
+ recognitionClient = new DefaultAcsClient(profile);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ public JSONObject getPersonBaseInfo(String personId) throws UnsupportedEncodingException {
|
|
60
|
+ GetPersonRequest getPersonRequest = new GetPersonRequest();
|
|
61
|
+ getPersonRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
|
|
62
|
+ getPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
|
|
63
|
+ getPersonRequest.setEncoding("utf-8");
|
|
64
|
+
|
|
65
|
+ JSONObject data = new JSONObject();
|
|
66
|
+ /**
|
|
67
|
+ * personId: 用户自定义个体Id,必填
|
|
68
|
+ */
|
|
69
|
+ data.put("personId", "personId_test_3");
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+ getPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
|
|
73
|
+
|
|
74
|
+ /**
|
|
75
|
+ * 请务必设置超时时间
|
|
76
|
+ */
|
|
77
|
+ getPersonRequest.setConnectTimeout(3000);
|
|
78
|
+ getPersonRequest.setReadTimeout(6000);
|
|
79
|
+
|
|
80
|
+ try {
|
|
81
|
+ HttpResponse httpResponse = recognitionClient.doAction(getPersonRequest);
|
|
82
|
+
|
|
83
|
+ if (httpResponse.isSuccess()) {
|
|
84
|
+ JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
|
|
85
|
+ System.out.println(JSON.toJSONString(scrResponse, true));
|
|
86
|
+ if (200 == scrResponse.getInteger("code")) {
|
|
87
|
+ JSONObject resultObject = scrResponse.getJSONObject("data");
|
|
88
|
+ if (200 == resultObject.getInteger("code")) {
|
|
89
|
+ System.out.println(resultObject.getString("personId"));
|
|
90
|
+ } else {
|
|
91
|
+ System.out.println("task process fail:" + resultObject.getInteger("code"));
|
|
92
|
+ }
|
|
93
|
+ } else {
|
|
94
|
+ System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
|
|
95
|
+ }
|
|
96
|
+ } else {
|
|
97
|
+ System.out.println("response not success. status:" + httpResponse.getStatus());
|
|
98
|
+ }
|
|
99
|
+ } catch (ServerException e) {
|
|
100
|
+ e.printStackTrace();
|
|
101
|
+ } catch (ClientException e) {
|
|
102
|
+ e.printStackTrace();
|
|
103
|
+ } catch (Exception e) {
|
|
104
|
+ e.printStackTrace();
|
|
105
|
+ }
|
|
106
|
+ return data;
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ public void createPerson(String personId, String personName, String url, String ...images) throws TencentCloudSDKException, UnsupportedEncodingException {
|
|
110
|
+ // 已经存在的人员, 不重复创建
|
|
111
|
+ try {
|
|
112
|
+ getPersonBaseInfo(personId);
|
|
113
|
+ return;
|
|
114
|
+ } catch (Exception e) {
|
|
115
|
+ e.printStackTrace();
|
|
116
|
+ }
|
|
117
|
+ AddPersonRequest addPersonRequest = new AddPersonRequest();
|
|
118
|
+ addPersonRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
|
|
119
|
+ addPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
|
|
120
|
+ addPersonRequest.setEncoding("utf-8");
|
|
121
|
+
|
|
122
|
+ JSONObject data = new JSONObject();
|
|
123
|
+ /**
|
|
124
|
+ * personId: 用户自定义个体Id,必填
|
|
125
|
+ * groupIds: 用户自定义组Id列表,必填
|
|
126
|
+ * name: 用户名称,非必填
|
|
127
|
+ * note: 备注信息,非必填
|
|
128
|
+ */
|
|
129
|
+ data.put("personId", "personId_test_3");
|
|
130
|
+ data.put("groupIds", Arrays.asList("java_sdk_test_group"));
|
|
131
|
+ data.put("name", "测试");
|
|
132
|
+ data.put("note", "备注信息");
|
|
133
|
+
|
|
134
|
+ addPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
|
|
135
|
+
|
|
136
|
+ /**
|
|
137
|
+ * 请务必设置超时时间
|
|
138
|
+ */
|
|
139
|
+ addPersonRequest.setConnectTimeout(3000);
|
|
140
|
+ addPersonRequest.setReadTimeout(6000);
|
|
141
|
+
|
|
142
|
+ try {
|
|
143
|
+ HttpResponse httpResponse = recognitionClient.doAction(addPersonRequest);
|
|
144
|
+
|
|
145
|
+ if (httpResponse.isSuccess()) {
|
|
146
|
+ JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
|
|
147
|
+ System.out.println(JSON.toJSONString(scrResponse, true));
|
|
148
|
+ if (200 == scrResponse.getInteger("code")) {
|
|
149
|
+ JSONObject resultObject = scrResponse.getJSONObject("data");
|
|
150
|
+ if (200 == resultObject.getInteger("code")) {
|
|
151
|
+ System.out.println(resultObject.getString("personId"));
|
|
152
|
+ } else {
|
|
153
|
+ System.out.println("task process fail:" + resultObject.getInteger("code"));
|
|
154
|
+ }
|
|
155
|
+ } else {
|
|
156
|
+ System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
|
|
157
|
+ }
|
|
158
|
+ } else {
|
|
159
|
+ System.out.println("response not success. status:" + httpResponse.getStatus());
|
|
160
|
+ }
|
|
161
|
+ } catch (ServerException e) {
|
|
162
|
+ e.printStackTrace();
|
|
163
|
+ } catch (ClientException e) {
|
|
164
|
+ e.printStackTrace();
|
|
165
|
+ } catch (Exception e) {
|
|
166
|
+ e.printStackTrace();
|
|
167
|
+ }
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ public void createFace(String personId, String url, String ...images) throws Exception {
|
|
171
|
+
|
|
172
|
+ try {
|
|
173
|
+ createPerson(personId, personId, url);
|
|
174
|
+ } catch (Exception e) {
|
|
175
|
+ log.error(e.toString());
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ AddFacesRequest addFacesRequest = new AddFacesRequest();
|
|
179
|
+ addFacesRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
|
|
180
|
+ addFacesRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
|
|
181
|
+ addFacesRequest.setEncoding("utf-8");
|
|
182
|
+
|
|
183
|
+ JSONObject data = new JSONObject();
|
|
184
|
+ /**
|
|
185
|
+ * personId: 用户自定义个体Id,必填
|
|
186
|
+ * urls: 图片url
|
|
187
|
+ */
|
|
188
|
+ data.put("personId", "personId_test_3");
|
|
189
|
+ data.put("urls", Arrays.asList("https://img.alicdn.com/tfs/xxx-550-407.jpg"));
|
|
190
|
+
|
|
191
|
+ addFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
|
|
192
|
+
|
|
193
|
+ /**
|
|
194
|
+ * 请务必设置超时时间
|
|
195
|
+ */
|
|
196
|
+ addFacesRequest.setConnectTimeout(3000);
|
|
197
|
+ addFacesRequest.setReadTimeout(6000);
|
|
198
|
+
|
|
199
|
+ try {
|
|
200
|
+ HttpResponse httpResponse = recognitionClient.doAction(addFacesRequest);
|
|
201
|
+
|
|
202
|
+ if (httpResponse.isSuccess()) {
|
|
203
|
+ JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
|
|
204
|
+ System.out.println(JSON.toJSONString(scrResponse, true));
|
|
205
|
+ if (200 == scrResponse.getInteger("code")) {
|
|
206
|
+ JSONObject resultObject = scrResponse.getJSONObject("data");
|
|
207
|
+ if (200 == resultObject.getInteger("code")) {
|
|
208
|
+ System.out.println(resultObject.getString("personId"));
|
|
209
|
+ } else {
|
|
210
|
+ System.out.println("task process fail:" + resultObject.getInteger("code"));
|
|
211
|
+ }
|
|
212
|
+ } else {
|
|
213
|
+ System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
|
|
214
|
+ }
|
|
215
|
+ } else {
|
|
216
|
+ System.out.println("response not success. status:" + httpResponse.getStatus());
|
|
217
|
+ }
|
|
218
|
+ } catch (ServerException e) {
|
|
219
|
+ e.printStackTrace();
|
|
220
|
+ } catch (ClientException e) {
|
|
221
|
+ e.printStackTrace();
|
|
222
|
+ } catch (Exception e) {
|
|
223
|
+ e.printStackTrace();
|
|
224
|
+ }
|
|
225
|
+ }
|
|
226
|
+
|
|
227
|
+ public void searchFace(String url, String ...images) throws Exception {
|
|
228
|
+ ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
|
|
229
|
+ imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
|
|
230
|
+ imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
|
|
231
|
+ imageSyncScanRequest.setEncoding("utf-8");
|
|
232
|
+ imageSyncScanRequest.setRegionId(AliProperties.face.getRegionId());
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+ List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
|
|
236
|
+ Map<String, Object> task = new LinkedHashMap<String, Object>();
|
|
237
|
+ task.put("dataId", UUID.randomUUID().toString());
|
|
238
|
+ task.put("url", "https://img.alicdn.com/tfs/xxx-550-407.jpg");
|
|
239
|
+ task.put("time", new Date());
|
|
240
|
+ Map<String, String> extras = new HashMap<String, String>();
|
|
241
|
+ extras.put("groupId", "java_sdk_test_group");
|
|
242
|
+ task.put("extras", extras);
|
|
243
|
+
|
|
244
|
+ tasks.add(task);
|
|
245
|
+ JSONObject data = new JSONObject();
|
|
246
|
+ /**
|
|
247
|
+ * sface-n: 人脸1-N
|
|
248
|
+ */
|
|
249
|
+ data.put("scenes", Arrays.asList("sface-n"));
|
|
250
|
+ data.put("tasks", tasks);
|
|
251
|
+
|
|
252
|
+ imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
|
|
253
|
+
|
|
254
|
+ /**
|
|
255
|
+ * 请务必设置超时时间
|
|
256
|
+ */
|
|
257
|
+ imageSyncScanRequest.setConnectTimeout(3000);
|
|
258
|
+ imageSyncScanRequest.setReadTimeout(6000);
|
|
259
|
+
|
|
260
|
+ try {
|
|
261
|
+ HttpResponse httpResponse = client.doAction(imageSyncScanRequest);
|
|
262
|
+
|
|
263
|
+ if (httpResponse.isSuccess()) {
|
|
264
|
+ JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
|
|
265
|
+ System.out.println(JSON.toJSONString(scrResponse, true));
|
|
266
|
+ if (200 == scrResponse.getInteger("code")) {
|
|
267
|
+ JSONArray taskResults = scrResponse.getJSONArray("data");
|
|
268
|
+ for (Object taskResult : taskResults) {
|
|
269
|
+ if(200 == ((JSONObject)taskResult).getInteger("code")){
|
|
270
|
+ JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
|
|
271
|
+ for (Object sceneResult : sceneResults) {
|
|
272
|
+ String scene = ((JSONObject)sceneResult).getString("scene");
|
|
273
|
+ String suggestion = ((JSONObject)sceneResult).getString("suggestion");
|
|
274
|
+ //根据scene和suggetion做相关的处理
|
|
275
|
+ //do something
|
|
276
|
+ System.out.println("args = [" + scene + "]");
|
|
277
|
+ System.out.println("args = [" + suggestion + "]");
|
|
278
|
+ }
|
|
279
|
+ }else{
|
|
280
|
+ System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
|
|
281
|
+ }
|
|
282
|
+ }
|
|
283
|
+ } else {
|
|
284
|
+ System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
|
|
285
|
+ }
|
|
286
|
+ } else {
|
|
287
|
+ System.out.println("response not success. status:" + httpResponse.getStatus());
|
|
288
|
+ }
|
|
289
|
+ } catch (ServerException e) {
|
|
290
|
+ e.printStackTrace();
|
|
291
|
+ } catch (ClientException e) {
|
|
292
|
+ e.printStackTrace();
|
|
293
|
+ } catch (Exception e){
|
|
294
|
+ e.printStackTrace();
|
|
295
|
+ }
|
31
|
296
|
}
|
32
|
297
|
|
33
|
298
|
/**
|