Explorar el Código

Merge branch 'v4' of http://git.ycjcjy.com/welcome/service-welcome3 into v4

张延森 hace 6 años
padre
commit
07763ab95e

+ 9
- 3
src/main/java/com.huiju.welcome/controller/ImgController.java Ver fichero

@@ -9,6 +9,7 @@ import io.swagger.annotations.ApiParam;
9 9
 import org.apache.http.HttpStatus;
10 10
 import org.springframework.beans.factory.annotation.Autowired;
11 11
 import org.springframework.beans.factory.annotation.Value;
12
+import org.springframework.web.bind.annotation.GetMapping;
12 13
 import org.springframework.web.bind.annotation.PostMapping;
13 14
 import org.springframework.web.bind.annotation.RequestMapping;
14 15
 import org.springframework.web.bind.annotation.RequestParam;
@@ -30,12 +31,17 @@ public class ImgController {
30 31
 
31 32
 	@Autowired
32 33
 	TencentFaceUtils tencentFaceUtils;
34
+	
35
+	@Autowired
36
+	private AliFaceUtils aliFaceUtils;
33 37
 
34
-	@PostMapping("/face")
38
+	@GetMapping("/face")
35 39
 	public ResponseBean testUpload2Aliface() {
36 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 45
 		} catch (Exception e) {
40 46
 			e.printStackTrace();
41 47
 		}

+ 66
- 57
src/main/java/com.huiju.welcome/utils/AliFaceUtils.java Ver fichero

@@ -17,6 +17,8 @@ import com.aliyuncs.http.FormatType;
17 17
 import com.aliyuncs.http.HttpResponse;
18 18
 import com.aliyuncs.http.MethodType;
19 19
 import com.aliyuncs.profile.DefaultProfile;
20
+import com.aliyuncs.profile.IClientProfile;
21
+import com.google.gson.JsonObject;
20 22
 import com.huiju.welcome.config.AliProperties;
21 23
 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
22 24
 import com.tencentcloudapi.iai.v20180301.models.GetPersonBaseInfoResponse;
@@ -50,13 +52,21 @@ public class AliFaceUtils {
50 52
     
51 53
     
52 54
     @Autowired
53
-    public AliFaceUtils(AliProperties p) {
55
+    public AliFaceUtils(AliProperties p) throws ClientException {
54 56
         profile = DefaultProfile.getProfile(AliProperties.face.getRegionId(), AliProperties.accessKeyId, AliProperties.accessKeySecret);
55 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 70
         GetPersonRequest getPersonRequest = new GetPersonRequest();
61 71
         getPersonRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
62 72
         getPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
@@ -66,7 +76,7 @@ public class AliFaceUtils {
66 76
         /**
67 77
          * personId: 用户自定义个体Id,必填
68 78
          */
69
-        data.put("personId", "personId_test_3");
79
+        data.put("personId", personId);
70 80
     
71 81
     
72 82
         getPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
@@ -79,44 +89,50 @@ public class AliFaceUtils {
79 89
     
80 90
         try {
81 91
             HttpResponse httpResponse = recognitionClient.doAction(getPersonRequest);
82
-        
83 92
             if (httpResponse.isSuccess()) {
84 93
                 JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
85
-                System.out.println(JSON.toJSONString(scrResponse, true));
86 94
                 if (200 == scrResponse.getInteger("code")) {
87 95
                     JSONObject resultObject = scrResponse.getJSONObject("data");
88 96
                     if (200 == resultObject.getInteger("code")) {
89
-                        System.out.println(resultObject.getString("personId"));
97
+                        return resultObject;
90 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 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 124
         try {
112
-            getPersonBaseInfo(personId);
113
-            return;
125
+            if (null != getPersonBaseInfo(personId)){
126
+                return getPersonBaseInfo(personId);
127
+            }
114 128
         } catch (Exception e) {
115
-            e.printStackTrace();
129
+           throw e;
116 130
         }
117 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 136
         addPersonRequest.setEncoding("utf-8");
121 137
     
122 138
         JSONObject data = new JSONObject();
@@ -126,9 +142,9 @@ public class AliFaceUtils {
126 142
          * name: 用户名称,非必填
127 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 148
         data.put("note", "备注信息");
133 149
     
134 150
         addPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
@@ -144,35 +160,30 @@ public class AliFaceUtils {
144 160
         
145 161
             if (httpResponse.isSuccess()) {
146 162
                 JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
147
-                System.out.println(JSON.toJSONString(scrResponse, true));
148 163
                 if (200 == scrResponse.getInteger("code")) {
149 164
                     JSONObject resultObject = scrResponse.getJSONObject("data");
150 165
                     if (200 == resultObject.getInteger("code")) {
151
-                        System.out.println(resultObject.getString("personId"));
166
+                        return resultObject;
152 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 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 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 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 183
         try {
173
-            createPerson(personId, personId, url);
184
+            createPerson(personId, personId);
174 185
         } catch (Exception e) {
175
-            log.error(e.toString());
186
+            throw e;
176 187
         }
177 188
     
178 189
         AddFacesRequest addFacesRequest = new AddFacesRequest();
@@ -185,8 +196,8 @@ public class AliFaceUtils {
185 196
          * personId: 用户自定义个体Id,必填
186 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 202
         addFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
192 203
     
@@ -222,9 +233,10 @@ public class AliFaceUtils {
222 233
         } catch (Exception e) {
223 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 240
         ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
229 241
         imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
230 242
         imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
@@ -235,10 +247,10 @@ public class AliFaceUtils {
235 247
         List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
236 248
         Map<String, Object> task = new LinkedHashMap<String, Object>();
237 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 251
         task.put("time", new Date());
240 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 254
         task.put("extras", extras);
243 255
     
244 256
         tasks.add(task);
@@ -272,27 +284,24 @@ public class AliFaceUtils {
272 284
                                 String scene = ((JSONObject)sceneResult).getString("scene");
273 285
                                 String suggestion = ((JSONObject)sceneResult).getString("suggestion");
274 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 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 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 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 301
         } catch (Exception e){
294
-            e.printStackTrace();
302
+            throw e;
295 303
         }
304
+        return null;
296 305
     }
297 306
 
298 307
     /**