Browse Source

阿里云

傅行帆 6 years ago
parent
commit
4073ffd70f

+ 0
- 1184
.idea/workspace.xml
File diff suppressed because it is too large
View File


+ 9
- 3
src/main/java/com.huiju.welcome/controller/ImgController.java View File

9
 import org.apache.http.HttpStatus;
9
 import org.apache.http.HttpStatus;
10
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.beans.factory.annotation.Value;
11
 import org.springframework.beans.factory.annotation.Value;
12
+import org.springframework.web.bind.annotation.GetMapping;
12
 import org.springframework.web.bind.annotation.PostMapping;
13
 import org.springframework.web.bind.annotation.PostMapping;
13
 import org.springframework.web.bind.annotation.RequestMapping;
14
 import org.springframework.web.bind.annotation.RequestMapping;
14
 import org.springframework.web.bind.annotation.RequestParam;
15
 import org.springframework.web.bind.annotation.RequestParam;
30
 
31
 
31
 	@Autowired
32
 	@Autowired
32
 	TencentFaceUtils tencentFaceUtils;
33
 	TencentFaceUtils tencentFaceUtils;
34
+	
35
+	@Autowired
36
+	private AliFaceUtils aliFaceUtils;
33
 
37
 
34
-	@PostMapping("/face")
38
+	@GetMapping("/face")
35
 	public ResponseBean testUpload2Aliface() {
39
 	public ResponseBean testUpload2Aliface() {
36
 		try {
40
 		try {
37
-			tencentFaceUtils.searchFace("https://estateagents.oss-cn-shanghai.aliyuncs.com/1561458925754-%E6%9C%AA%E7%9F%A5%E6%96%87%E4%BB%B6.jpg");
38
-			return ResponseBean.success("");
41
+			//tencentFaceUtils.searchFace("https://estateagents.oss-cn-shanghai.aliyuncs.com/1561458925754-%E6%9C%AA%E7%9F%A5%E6%96%87%E4%BB%B6.jpg");
42
+			//return ResponseBean.success("");
43
+			//aliFaceUtils.createFace("10087","https://estateagents.oss-cn-shanghai.aliyuncs.com/1561458925754-%E6%9C%AA%E7%9F%A5%E6%96%87%E4%BB%B6.jpg");
44
+			return ResponseBean.success(aliFaceUtils.searchFace("https://estateagents.oss-cn-shanghai.aliyuncs.com/1561458925754-%E6%9C%AA%E7%9F%A5%E6%96%87%E4%BB%B6.jpg"));
39
 		} catch (Exception e) {
45
 		} catch (Exception e) {
40
 			e.printStackTrace();
46
 			e.printStackTrace();
41
 		}
47
 		}

+ 66
- 57
src/main/java/com.huiju.welcome/utils/AliFaceUtils.java View File

17
 import com.aliyuncs.http.HttpResponse;
17
 import com.aliyuncs.http.HttpResponse;
18
 import com.aliyuncs.http.MethodType;
18
 import com.aliyuncs.http.MethodType;
19
 import com.aliyuncs.profile.DefaultProfile;
19
 import com.aliyuncs.profile.DefaultProfile;
20
+import com.aliyuncs.profile.IClientProfile;
21
+import com.google.gson.JsonObject;
20
 import com.huiju.welcome.config.AliProperties;
22
 import com.huiju.welcome.config.AliProperties;
21
 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
23
 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
22
 import com.tencentcloudapi.iai.v20180301.models.GetPersonBaseInfoResponse;
24
 import com.tencentcloudapi.iai.v20180301.models.GetPersonBaseInfoResponse;
50
     
52
     
51
     
53
     
52
     @Autowired
54
     @Autowired
53
-    public AliFaceUtils(AliProperties p) {
55
+    public AliFaceUtils(AliProperties p) throws ClientException {
54
         profile = DefaultProfile.getProfile(AliProperties.face.getRegionId(), AliProperties.accessKeyId, AliProperties.accessKeySecret);
56
         profile = DefaultProfile.getProfile(AliProperties.face.getRegionId(), AliProperties.accessKeyId, AliProperties.accessKeySecret);
55
         client = new DefaultAcsClient(profile);
57
         client = new DefaultAcsClient(profile);
56
-        recognitionClient = new DefaultAcsClient(profile);
58
+        IClientProfile iClientProfile = DefaultProfile.getProfile(AliProperties.face.getRegionId(), AliProperties.accessKeyId, AliProperties.accessKeySecret);
59
+        recognitionClient = new DefaultAcsClient(iClientProfile);
57
     }
60
     }
58
     
61
     
59
-    public JSONObject getPersonBaseInfo(String personId) throws UnsupportedEncodingException {
62
+    /**
63
+     * 获取用户信息
64
+     * @param personId
65
+     * @return
66
+     * @throws UnsupportedEncodingException
67
+     */
68
+    public JSONObject getPersonBaseInfo(String personId) throws Exception {
69
+        JSONObject result = new JSONObject();
60
         GetPersonRequest getPersonRequest = new GetPersonRequest();
70
         GetPersonRequest getPersonRequest = new GetPersonRequest();
61
         getPersonRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
71
         getPersonRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
62
         getPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
72
         getPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
66
         /**
76
         /**
67
          * personId: 用户自定义个体Id,必填
77
          * personId: 用户自定义个体Id,必填
68
          */
78
          */
69
-        data.put("personId", "personId_test_3");
79
+        data.put("personId", personId);
70
     
80
     
71
     
81
     
72
         getPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
82
         getPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
79
     
89
     
80
         try {
90
         try {
81
             HttpResponse httpResponse = recognitionClient.doAction(getPersonRequest);
91
             HttpResponse httpResponse = recognitionClient.doAction(getPersonRequest);
82
-        
83
             if (httpResponse.isSuccess()) {
92
             if (httpResponse.isSuccess()) {
84
                 JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
93
                 JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
85
-                System.out.println(JSON.toJSONString(scrResponse, true));
86
                 if (200 == scrResponse.getInteger("code")) {
94
                 if (200 == scrResponse.getInteger("code")) {
87
                     JSONObject resultObject = scrResponse.getJSONObject("data");
95
                     JSONObject resultObject = scrResponse.getJSONObject("data");
88
                     if (200 == resultObject.getInteger("code")) {
96
                     if (200 == resultObject.getInteger("code")) {
89
-                        System.out.println(resultObject.getString("personId"));
97
+                        return resultObject;
90
                     } else {
98
                     } else {
91
-                        System.out.println("task process fail:" + resultObject.getInteger("code"));
99
+                        throw new Exception("task process fail:" + resultObject.getInteger("code"));
92
                     }
100
                     }
93
-                } else {
94
-                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
101
+                } else if(400 == scrResponse.getInteger("code")){
102
+                    return null;
103
+                }else{
104
+                    throw new Exception("detect not success. code:" + scrResponse.getInteger("code"));
95
                 }
105
                 }
96
-            } else {
97
-                System.out.println("response not success. status:" + httpResponse.getStatus());
106
+            }else{
107
+                throw new Exception("response not success. status:" + httpResponse.getStatus());
98
             }
108
             }
99
-        } catch (ServerException e) {
100
-            e.printStackTrace();
101
-        } catch (ClientException e) {
102
-            e.printStackTrace();
103
         } catch (Exception e) {
109
         } catch (Exception e) {
104
-            e.printStackTrace();
110
+            throw e;
105
         }
111
         }
106
-        return data;
107
     }
112
     }
108
     
113
     
109
-    public void createPerson(String personId, String personName, String url, String ...images) throws TencentCloudSDKException, UnsupportedEncodingException {
114
+    /**
115
+     * 穿件人员
116
+     * @param personId
117
+     * @param personName
118
+     * @throws TencentCloudSDKException
119
+     * @throws UnsupportedEncodingException
120
+     */
121
+    public JSONObject createPerson(String personId, String personName) throws Exception {
122
+        JSONObject result = new JSONObject();
110
         // 已经存在的人员, 不重复创建
123
         // 已经存在的人员, 不重复创建
111
         try {
124
         try {
112
-            getPersonBaseInfo(personId);
113
-            return;
125
+            if (null != getPersonBaseInfo(personId)){
126
+                return getPersonBaseInfo(personId);
127
+            }
114
         } catch (Exception e) {
128
         } catch (Exception e) {
115
-            e.printStackTrace();
129
+           throw e;
116
         }
130
         }
117
         AddPersonRequest addPersonRequest = new AddPersonRequest();
131
         AddPersonRequest addPersonRequest = new AddPersonRequest();
118
-        addPersonRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
119
-        addPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
132
+        // 指定api返回格式
133
+        addPersonRequest.setAcceptFormat(FormatType.JSON);
134
+        // 指定请求方法
135
+        addPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST);
120
         addPersonRequest.setEncoding("utf-8");
136
         addPersonRequest.setEncoding("utf-8");
121
     
137
     
122
         JSONObject data = new JSONObject();
138
         JSONObject data = new JSONObject();
126
          * name: 用户名称,非必填
142
          * name: 用户名称,非必填
127
          * note: 备注信息,非必填
143
          * note: 备注信息,非必填
128
          */
144
          */
129
-        data.put("personId", "personId_test_3");
130
-        data.put("groupIds", Arrays.asList("java_sdk_test_group"));
131
-        data.put("name", "测试");
145
+        data.put("personId", personId);
146
+        data.put("groupIds", Arrays.asList(AliProperties.face.getGroup()));
147
+        data.put("name", personName);
132
         data.put("note", "备注信息");
148
         data.put("note", "备注信息");
133
     
149
     
134
         addPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
150
         addPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
144
         
160
         
145
             if (httpResponse.isSuccess()) {
161
             if (httpResponse.isSuccess()) {
146
                 JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
162
                 JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
147
-                System.out.println(JSON.toJSONString(scrResponse, true));
148
                 if (200 == scrResponse.getInteger("code")) {
163
                 if (200 == scrResponse.getInteger("code")) {
149
                     JSONObject resultObject = scrResponse.getJSONObject("data");
164
                     JSONObject resultObject = scrResponse.getJSONObject("data");
150
                     if (200 == resultObject.getInteger("code")) {
165
                     if (200 == resultObject.getInteger("code")) {
151
-                        System.out.println(resultObject.getString("personId"));
166
+                        return resultObject;
152
                     } else {
167
                     } else {
153
-                        System.out.println("task process fail:" + resultObject.getInteger("code"));
168
+                        throw new Exception("task process fail:" + resultObject.getInteger("code"));
154
                     }
169
                     }
155
                 } else {
170
                 } else {
156
-                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
171
+                    throw new Exception("detect not success. code:" + scrResponse.getInteger("code"));
157
                 }
172
                 }
158
             } else {
173
             } else {
159
-                System.out.println("response not success. status:" + httpResponse.getStatus());
174
+                throw new Exception("response not success. status:" + httpResponse.getStatus());
160
             }
175
             }
161
-        } catch (ServerException e) {
162
-            e.printStackTrace();
163
-        } catch (ClientException e) {
164
-            e.printStackTrace();
165
         } catch (Exception e) {
176
         } catch (Exception e) {
166
-            e.printStackTrace();
177
+            throw e;
167
         }
178
         }
168
     }
179
     }
169
     
180
     
170
-    public void createFace(String personId, String url, String ...images) throws Exception {
171
-        
181
+    public JSONObject createFace(String personId, String url) throws Exception {
182
+        JSONObject result = new JSONObject();
172
         try {
183
         try {
173
-            createPerson(personId, personId, url);
184
+            createPerson(personId, personId);
174
         } catch (Exception e) {
185
         } catch (Exception e) {
175
-            log.error(e.toString());
186
+            throw e;
176
         }
187
         }
177
     
188
     
178
         AddFacesRequest addFacesRequest = new AddFacesRequest();
189
         AddFacesRequest addFacesRequest = new AddFacesRequest();
185
          * personId: 用户自定义个体Id,必填
196
          * personId: 用户自定义个体Id,必填
186
          * urls: 图片url
197
          * urls: 图片url
187
          */
198
          */
188
-        data.put("personId", "personId_test_3");
189
-        data.put("urls", Arrays.asList("https://img.alicdn.com/tfs/xxx-550-407.jpg"));
199
+        data.put("personId", personId);
200
+        data.put("urls", Arrays.asList(url));
190
     
201
     
191
         addFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
202
         addFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
192
     
203
     
222
         } catch (Exception e) {
233
         } catch (Exception e) {
223
             e.printStackTrace();
234
             e.printStackTrace();
224
         }
235
         }
236
+        return result;
225
     }
237
     }
226
     
238
     
227
-    public void searchFace(String url, String ...images) throws Exception {
239
+    public JSONObject searchFace(String url) throws Exception {
228
         ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
240
         ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
229
         imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
241
         imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
230
         imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
242
         imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
235
         List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
247
         List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
236
         Map<String, Object> task = new LinkedHashMap<String, Object>();
248
         Map<String, Object> task = new LinkedHashMap<String, Object>();
237
         task.put("dataId", UUID.randomUUID().toString());
249
         task.put("dataId", UUID.randomUUID().toString());
238
-        task.put("url", "https://img.alicdn.com/tfs/xxx-550-407.jpg");
250
+        task.put("url", url);
239
         task.put("time", new Date());
251
         task.put("time", new Date());
240
         Map<String, String> extras = new HashMap<String, String>();
252
         Map<String, String> extras = new HashMap<String, String>();
241
-        extras.put("groupId", "java_sdk_test_group");
253
+        extras.put("groupId", AliProperties.face.getGroup());
242
         task.put("extras", extras);
254
         task.put("extras", extras);
243
     
255
     
244
         tasks.add(task);
256
         tasks.add(task);
272
                                 String scene = ((JSONObject)sceneResult).getString("scene");
284
                                 String scene = ((JSONObject)sceneResult).getString("scene");
273
                                 String suggestion = ((JSONObject)sceneResult).getString("suggestion");
285
                                 String suggestion = ((JSONObject)sceneResult).getString("suggestion");
274
                                 //根据scene和suggetion做相关的处理
286
                                 //根据scene和suggetion做相关的处理
275
-                                //do something
276
-                                System.out.println("args = [" + scene + "]");
277
-                                System.out.println("args = [" + suggestion + "]");
287
+                                if (suggestion.equals("review")){
288
+                                    return ((JSONObject)sceneResult).getJSONArray("topPersonData").getJSONObject(0).getJSONArray("persons").getJSONObject(0);
289
+                                }
278
                             }
290
                             }
279
                         }else{
291
                         }else{
280
-                            System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
292
+                            throw new Exception("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
281
                         }
293
                         }
282
                     }
294
                     }
283
                 } else {
295
                 } else {
284
-                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
296
+                    throw new Exception("detect not success. code:" + scrResponse.getInteger("code"));
285
                 }
297
                 }
286
             } else {
298
             } else {
287
-                System.out.println("response not success. status:" + httpResponse.getStatus());
299
+                throw new Exception("response not success. status:" + httpResponse.getStatus());
288
             }
300
             }
289
-        } catch (ServerException e) {
290
-            e.printStackTrace();
291
-        } catch (ClientException e) {
292
-            e.printStackTrace();
293
         } catch (Exception e){
301
         } catch (Exception e){
294
-            e.printStackTrace();
302
+            throw e;
295
         }
303
         }
304
+        return null;
296
     }
305
     }
297
 
306
 
298
     /**
307
     /**