张延森 5 anni fa
parent
commit
1dd1e6e927

+ 1
- 0
src/main/java/com.huiju.welcome/common/Action.java Vedi File

@@ -13,6 +13,7 @@ public class Action {
13 13
     public final static String TYPE_DEFAULT = "default";
14 14
     public final static String TYPE_ACK = "ack";
15 15
     public final static String TYPE_DISPATCH_FACE = "dispatch-face";
16
+    public final static String TYPE_DELETE_FACE = "delete-face";
16 17
     public final static String TYPE_TO_FRONT = "to-front";
17 18
     public final static String TYPE_NEW_MAIN_USHER = "new-main-usher";
18 19
     public final static String TYPE_NEW_FIRST_USHER = "new-first-usher";

+ 10
- 0
src/main/java/com.huiju.welcome/mapper/TaFaceLibMapper.java Vedi File

@@ -0,0 +1,10 @@
1
+package com.huiju.welcome.mapper;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.huiju.welcome.model.TaFaceLib;
5
+import org.apache.ibatis.annotations.Mapper;
6
+
7
+
8
+@Mapper
9
+public interface TaFaceLibMapper extends BaseMapper<TaFaceLib> {
10
+}

+ 33
- 0
src/main/java/com.huiju.welcome/model/TaFaceLib.java Vedi File

@@ -0,0 +1,33 @@
1
+package com.huiju.welcome.model;
2
+
3
+
4
+import com.baomidou.mybatisplus.annotation.IdType;
5
+import com.baomidou.mybatisplus.annotation.TableId;
6
+import lombok.Data;
7
+import lombok.EqualsAndHashCode;
8
+import lombok.experimental.Accessors;
9
+
10
+import java.io.Serializable;
11
+
12
+@Data
13
+@EqualsAndHashCode(callSuper = false)
14
+@Accessors(chain = true)
15
+public class TaFaceLib implements Serializable {
16
+
17
+    private static final long serialVersionUID = 1L;
18
+
19
+    @TableId(value = "serial_no", type = IdType.AUTO)
20
+    private Integer serialNo;
21
+
22
+    private String libCode;
23
+
24
+    private Integer localPerson;
25
+
26
+    private Integer localFace;
27
+
28
+    private String libPerson;
29
+
30
+    private String libFace;
31
+
32
+    private String deviceId;
33
+}

+ 2
- 3
src/main/java/com.huiju.welcome/service/ICameraService.java Vedi File

@@ -1,14 +1,13 @@
1 1
 package com.huiju.welcome.service;
2 2
 
3
-import com.huiju.welcome.model.TaCustomer;
4 3
 import com.huiju.welcome.model.TaDevice;
5
-import com.huiju.welcome.model.TaFirstUsherRecord;
6
-import com.huiju.welcome.model.TaMainUsherRecord;
7 4
 import com.huiju.welcome.model.TaPerson;
8 5
 import com.huiju.welcome.model.TaSnapshotPerson;
9 6
 
10 7
 public interface ICameraService {
11 8
     String dispatchPerson(TaPerson taPerson, TaDevice ...devices);
12 9
 
10
+    boolean deleteFace(Integer personId);
11
+
13 12
     boolean newPersonByDevice(TaSnapshotPerson taSnapshotPerson);
14 13
 }

+ 80
- 9
src/main/java/com.huiju.welcome/service/impl/CameraServiceImpl.java Vedi File

@@ -1,5 +1,6 @@
1 1
 package com.huiju.welcome.service.impl;
2 2
 
3
+import com.alibaba.fastjson.JSONArray;
3 4
 import com.alibaba.fastjson.JSONObject;
4 5
 import com.alibaba.fastjson.serializer.SerializerFeature;
5 6
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -8,19 +9,14 @@ import com.huiju.welcome.common.WebSocketServer;
8 9
 import com.huiju.welcome.mapper.SysUserMapper;
9 10
 import com.huiju.welcome.mapper.TaCustomerMapMapper;
10 11
 import com.huiju.welcome.mapper.TaCustomerMapper;
11
-import com.huiju.welcome.model.SysUser;
12
-import com.huiju.welcome.model.TaCustomer;
13
-import com.huiju.welcome.model.TaCustomerMap;
14
-import com.huiju.welcome.model.TaDevice;
15
-import com.huiju.welcome.model.TaFirstUsherRecord;
16
-import com.huiju.welcome.model.TaMainUsherRecord;
17
-import com.huiju.welcome.model.TaPerson;
18
-import com.huiju.welcome.model.TaSnapshotPerson;
12
+import com.huiju.welcome.mapper.TaFaceLibMapper;
13
+import com.huiju.welcome.model.*;
19 14
 import com.huiju.welcome.service.*;
20 15
 import com.huiju.welcome.utils.*;
21 16
 import com.tencentcloudapi.common.exception.TencentCloudSDKException;
22 17
 import io.swagger.models.auth.In;
23 18
 import lombok.extern.slf4j.Slf4j;
19
+import org.apache.http.HttpStatus;
24 20
 import org.springframework.beans.factory.annotation.Autowired;
25 21
 import org.springframework.beans.factory.annotation.Value;
26 22
 import org.springframework.stereotype.Service;
@@ -38,6 +34,9 @@ import java.util.Map;
38 34
 @Slf4j
39 35
 @Service
40 36
 public class CameraServiceImpl implements ICameraService {
37
+    final static String HIK_FACE_LIB = "hik-face";
38
+    final static String ALI_FACE_LIB = "ali-face";
39
+
41 40
     @Autowired
42 41
     private ITaDeviceService taDeviceService;
43 42
 
@@ -71,6 +70,9 @@ public class CameraServiceImpl implements ICameraService {
71 70
     @Autowired
72 71
     private SysUserMapper sysUserMapper;
73 72
 
73
+    @Autowired
74
+    private TaFaceLibMapper taFaceLibMapper;
75
+
74 76
     @Autowired
75 77
     private IMessageService iMessageService;
76 78
     
@@ -99,6 +101,16 @@ public class CameraServiceImpl implements ICameraService {
99 101
                 Action act = Action.create(Action.TYPE_DISPATCH_FACE, payload);
100 102
 
101 103
                 WebSocketServer.sendInfo(act.toJSON(), "device", device.getDeviceId());
104
+
105
+                // 假设海康设备下发成功
106
+                TaFaceLib taFaceLib = new TaFaceLib();
107
+                taFaceLib.setLocalPerson(taPerson.getPersonId());
108
+                taFaceLib.setLibCode(HIK_FACE_LIB);    // 海康设备
109
+                taFaceLib.setLibPerson(taPerson.getPersonId().toString());
110
+                taFaceLib.setLibFace(taPerson.getPersonId().toString());
111
+                taFaceLib.setDeviceId(device.getDeviceId());
112
+                taFaceLibMapper.insert(taFaceLib);
113
+
102 114
             } catch (Exception e){
103 115
                 e.printStackTrace();
104 116
                 return "连接设备(" + device.getDeviceId() + ")人脸下发服务 失败: "+ e.getMessage();
@@ -114,7 +126,27 @@ public class CameraServiceImpl implements ICameraService {
114 126
 //        }
115 127
 
116 128
         try {
117
-            aliFaceUtils.createFace(String.valueOf(taPerson.getRealId()), taPerson.getAvatar());
129
+            JSONObject result = aliFaceUtils.createFace(String.valueOf(taPerson.getRealId()), taPerson.getAvatar());
130
+            if (null != result && result.getInteger("code") == HttpStatus.SC_OK) {
131
+                JSONArray faceImageItems = result.getJSONArray("faceImageItems");
132
+                if (null != faceImageItems && faceImageItems.size() > 0) {
133
+                    JSONObject faceInfo = faceImageItems.getJSONObject(0);
134
+//                    Double faceId = faceInfo.getDouble("faceId");     // 阿里返回值是一个 double 类型
135
+                    String faceId = faceInfo.getString("faceId");
136
+
137
+                    //
138
+                    TaFaceLib taFaceLib = new TaFaceLib();
139
+                    taFaceLib.setLocalPerson(taPerson.getPersonId());
140
+                    taFaceLib.setLibCode(ALI_FACE_LIB);    // 海康设备
141
+                    taFaceLib.setLibPerson(taPerson.getPersonId().toString());
142
+                    taFaceLib.setLibFace(faceId);
143
+//                    taFaceLib.setDeviceId();
144
+                    taFaceLibMapper.insert(taFaceLib);
145
+                }
146
+
147
+            } else {
148
+                log.error("下发人脸到阿里云失败: 原因未知");
149
+            }
118 150
         } catch (Exception e) {
119 151
             e.printStackTrace();
120 152
             log.error("下发人脸到阿里云失败: " + e.getMessage());
@@ -123,6 +155,45 @@ public class CameraServiceImpl implements ICameraService {
123 155
         return "";
124 156
     }
125 157
 
158
+    @Override
159
+    public boolean deleteFace(Integer personId) {
160
+        QueryWrapper<TaFaceLib> wrapper = new QueryWrapper<>();
161
+        wrapper.eq("local_person", personId);
162
+
163
+        List<TaFaceLib> taFaceLibs = taFaceLibMapper.selectList(wrapper);
164
+        if (null == taFaceLibs || taFaceLibs.size() == 0) {
165
+            return true;
166
+        }
167
+
168
+        for (TaFaceLib taFaceLib: taFaceLibs) {
169
+            String libCode = taFaceLib.getLibCode();
170
+
171
+            if (libCode.indexOf(ALI_FACE_LIB) > -1) {
172
+                // 删除阿里人脸
173
+                try {
174
+                    aliFaceUtils.deleteFace(taFaceLib.getLibPerson(), taFaceLib.getLibFace());
175
+                } catch (Exception e) {
176
+                    e.printStackTrace();
177
+                }
178
+            } else {
179
+                // 删除海康人脸
180
+                try {
181
+                    Map<String, Object> payload = new HashMap<>();
182
+                    payload.put("personId", personId);
183
+                    Action act = Action.create(Action.TYPE_DELETE_FACE, payload);
184
+                    WebSocketServer.sendInfo(act.toJSON(), "device", taFaceLib.getDeviceId());
185
+                } catch (Exception e) {
186
+                    e.printStackTrace();
187
+                }
188
+            }
189
+
190
+            // 清除数据库
191
+            taFaceLibMapper.deleteById(taFaceLib);
192
+        }
193
+
194
+        return false;
195
+    }
196
+
126 197
     @Override
127 198
     public boolean newPersonByDevice(TaSnapshotPerson taSnapshotPerson) {
128 199
         boolean workingTime = iSysParamService.isInWorkingTime(LocalDateTime.now());

+ 38
- 0
src/main/java/com.huiju.welcome/utils/AliFaceUtils.java Vedi File

@@ -10,6 +10,7 @@ import com.aliyuncs.IAcsClient;
10 10
 import com.aliyuncs.exceptions.ClientException;
11 11
 import com.aliyuncs.exceptions.ServerException;
12 12
 import com.aliyuncs.green.model.v20180509.AddFacesRequest;
13
+import com.aliyuncs.green.model.v20180509.DeleteFacesRequest;
13 14
 import com.aliyuncs.green.model.v20180509.AddPersonRequest;
14 15
 import com.aliyuncs.green.model.v20180509.GetPersonRequest;
15 16
 import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
@@ -273,6 +274,7 @@ public class AliFaceUtils {
273 274
                 if (200 == scrResponse.getInteger("code")) {
274 275
                     JSONObject resultObject = scrResponse.getJSONObject("data");
275 276
                     if (200 == resultObject.getInteger("code")) {
277
+                        result = resultObject;
276 278
                         System.out.println(resultObject.getString("personId"));
277 279
                     } else {
278 280
                         System.out.println("task process fail:" + resultObject.getInteger("code"));
@@ -293,6 +295,42 @@ public class AliFaceUtils {
293 295
         return result;
294 296
     }
295 297
 
298
+    public void deleteFace(String personId, String faceId) throws Exception {
299
+
300
+        DeleteFacesRequest deleteFacesRequest = new DeleteFacesRequest();
301
+        deleteFacesRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
302
+        deleteFacesRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
303
+        deleteFacesRequest.setEncoding("utf-8");
304
+
305
+        JSONObject data = new JSONObject();
306
+        data.put("personId", personId);
307
+        data.put("faceIds", new ArrayList<String>(){{
308
+            add(faceId);
309
+        }});
310
+
311
+        deleteFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
312
+        deleteFacesRequest.setConnectTimeout(3000);
313
+        deleteFacesRequest.setReadTimeout(6000);
314
+
315
+        try {
316
+            HttpResponse httpResponse = recognitionClient.doAction(deleteFacesRequest);
317
+
318
+            if (httpResponse.isSuccess()) {
319
+                return;
320
+            } else {
321
+                log.error("删除人脸失败: {}", httpResponse.getHttpContentString());
322
+            }
323
+        } catch (ServerException e) {
324
+            e.printStackTrace();
325
+        } catch (ClientException e) {
326
+            e.printStackTrace();
327
+        } catch (Exception e) {
328
+            e.printStackTrace();
329
+        }
330
+
331
+        return;
332
+    }
333
+
296 334
     /**
297 335
      * 人脸查询
298 336
      * @param url

+ 6
- 6
src/main/resources/application.yml Vedi File

@@ -1,12 +1,12 @@
1 1
 spring:
2 2
   datasource:
3 3
     driver-class-name: com.mysql.cj.jdbc.Driver
4
-    url: jdbc:mysql://rm-uf6z3z6jq11x653d77o.mysql.rds.aliyuncs.com:3306/welcome3?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
5
-    username: welcome
6
-    password: s190QU2#z%hdU%^FPkY9qjls
7
-#    url: jdbc:mysql://47.101.36.130:3306/welcome3?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
8
-#    username: root
9
-#    password: Wt4q*/9}i00q@0a4jM{z6O#h129@F/
4
+#    url: jdbc:mysql://rm-uf6z3z6jq11x653d77o.mysql.rds.aliyuncs.com:3306/welcome3?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
5
+#    username: welcome
6
+#    password: s190QU2#z%hdU%^FPkY9qjls
7
+    url: jdbc:mysql://47.101.36.130:3306/welcome3?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
8
+    username: root
9
+    password: Wt4q*/9}i00q@0a4jM{z6O#h129@F/
10 10
   servlet:
11 11
     multipart:
12 12
       max-file-size: 1024MB