Bladeren bron

修改人脸识别AI调用服务

Signed-off-by: konghl <konghl@asiainfo.com>
konghl 4 jaren geleden
bovenliggende
commit
f1d39525fc

+ 2 - 1
monitor-manage-service/src/main/java/com/ai/bss/monitorManage/constant/MonitorManageConsts.java

@ -41,7 +41,8 @@ public class MonitorManageConsts {
41 41
    //AI识别结果:仪表
42 42
    public static final String AI_IDENTIFY_RESULT_INSTRUMENT= "INS";
43 43
44
    public static final String alarmEmployeeRoleId= "201613310867";
44
    //未知人员ID
45
    public static final String alarmEmployeeRoleId= "-1";
45 46
46 47
    public static String getSystemSeparator(){
47 48
        return File.separator;

+ 16 - 0
monitor-manage-service/src/main/java/com/ai/bss/monitorManage/model/FaceResultDto.java

@ -0,0 +1,16 @@
1
package com.ai.bss.monitorManage.model;
2
3
import lombok.Data;
4
5
@Data
6
public class FaceResultDto {
7
8
    private String userId;
9
10
    private float simi;
11
12
    private int isMatch;
13
14
    private int[] faceBox;
15
16
}

+ 1 - 0
monitor-manage-service/src/main/java/com/ai/bss/monitorManage/model/IdenResultDto.java

@ -8,5 +8,6 @@ public class IdenResultDto {
8 8
    private String alarmType;
9 9
    private String alarmMemo;
10 10
    private String alarmEmployeeRoleId;
11
    private int[] alarmBox;
11 12
12 13
}

+ 119 - 33
monitor-manage-service/src/main/java/com/ai/bss/monitorManage/service/impl/KafkaProcessImpl.java

@ -6,6 +6,7 @@ import java.util.HashMap;
6 6
import java.util.List;
7 7
import java.util.Map;
8 8
9
import com.ai.bss.monitorManage.model.FaceResultDto;
9 10
import org.apache.commons.lang.StringUtils;
10 11
import org.apache.logging.log4j.util.Strings;
11 12
import org.springframework.beans.factory.annotation.Autowired;
@ -183,6 +184,7 @@ public class KafkaProcessImpl implements KafkaProcess {
183 184
						aiIdenLogDto=instrumentDetect(aiIdenLogDto,filePath);//仪表识别
184 185
185 186
					} else if (MonitorManageConsts.AI_MODEL_STRANGER.equals(aiIdenModel)){
187
						aiIdenLogDto=strangerDetect(aiIdenLogDto,filePath);//陌生人识别
186 188
187 189
					} else {
188 190
@ -190,15 +192,6 @@ public class KafkaProcessImpl implements KafkaProcess {
190 192
				}
191 193
			}
192 194
193
			/*// 安全帽识别
194
			aiIdenLogDto.setTopic(topicAiTask);// AI识别TOPICs
195
			
196
			// 人体识别
197
			boolean isHuman = aiworkHumanDetect(filePath);
198
			// AI头盔检查
199
			if (isHuman) {
200
				aiIdenLogDto = aiHelmetDetect(aiIdenLogDto, filePath);
201
			}*/
202 195
		}
203 196
204 197
		log.info("-------等待任务--------");
@ -207,7 +200,14 @@ public class KafkaProcessImpl implements KafkaProcess {
207 200
	@Async
208 201
	@Override
209 202
	public void processVideoFile(String fileKey, String filePath) throws Exception {
210
		Thread.sleep(70000);
203
		String fileExpandedName=filePath.substring(filePath.lastIndexOf(".")+1);
204
		if ("m3u8".equals(fileExpandedName)){
205
			Thread.sleep(10000);
206
			return;
207
		}else{
208
			Thread.sleep(70000);
209
		}
210
211 211
		String deviceCode = "";
212 212
		String fileType = "";
213 213
		String fileName = "";
@ -225,6 +225,7 @@ public class KafkaProcessImpl implements KafkaProcess {
225 225
		monitorVideoLogDto = new MonitorVideoLogDto();
226 226
		fileName = filePath.substring(filePath.lastIndexOf(MonitorManageConsts.getSystemSeparator()) + 1,
227 227
				filePath.length());
228
228 229
		if (fileType.equals(MonitorManageConsts.FILE_TYPE_VIDEO)) {
229 230
			bucketName = minioConfig.getBucketAiVideo();
230 231
			time = fileName.substring(5, 19);
@ -260,7 +261,7 @@ public class KafkaProcessImpl implements KafkaProcess {
260 261
		String time = fileName.substring(5, 19);
261 262
		String dateTime = DateUtils.formatDate(DateUtils.convertDate(time, DateUtils.PURE_DATETIME_PATTERN),
262 263
				DateUtils.NORM_DATETIME_HHMM_PATTERN);
263
		System.out.println(dateTime);
264
		log.info(dateTime);
264 265
	}
265 266
266 267
	private Map<String, KafkaTemplate> kafkaTemplateMap = new HashMap<>();
@ -276,7 +277,7 @@ public class KafkaProcessImpl implements KafkaProcess {
276 277
277 278
		try {
278 279
			Object result = kafkaTemplate.send(topic, msgStr).get();
279
			System.out.println("发送kafka消息:topic=" + topic + "msg: " + msgStr);
280
			log.info("发送kafka消息:topic=" + topic + "msg: " + msgStr);
280 281
		} catch (Exception e) {
281 282
			log.error(e.getMessage());
282 283
			return false;
@ -298,7 +299,7 @@ public class KafkaProcessImpl implements KafkaProcess {
298 299
					Strings.EMPTY, Strings.EMPTY);
299 300
//			String aiResponseData = ebcAiService.helmetDetect(ImageBase64Converter.convertInputStreamToBase64(multipartFile.getInputStream()), Strings.EMPTY, Strings.EMPTY);
300 301
			JSONObject jsonObject = JSONObject.parseObject(aiResponseData);// JSONUtil.formatJsonStr(aiResponseData);
301
			System.out.println("AI帽子识别结果: " + jsonObject);
302
			log.info("AI帽子识别结果: " + jsonObject);
302 303
			IdenResultDto idenResultDto = null;
303 304
			List<IdenResultDto> idenResultDtoList = null;
304 305
			if (jsonObject.getString("Status").equals("Ok")) {
@ -309,27 +310,26 @@ public class KafkaProcessImpl implements KafkaProcess {
309 310
						JSONObject resultObj = JSONObject.parseObject(jsonoArray.get(i).toString());
310 311
						String aiClassName = resultObj.getString("class");
311 312
						// safety_hat表示安全帽;ordinart_hat表示普通帽子;chef_hat表示厨师帽;none表示未戴帽子
312
						if (aiClassName.equals("none")) {
313
						if(aiClassName.equals("safety_hat")){
313 314
							idenResultDto = new IdenResultDto();
314 315
							idenResultDto.setAlarmEmployeeRoleId(MonitorManageConsts.alarmEmployeeRoleId);
315
							idenResultDto.setAlarmType(MonitorManageConsts.AI_ALARM_TYPE_WEA);
316
							idenResultDto.setAlarmMemo("未戴帽子");
317
							aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT_ALARM);
316
							idenResultDto.setAlarmType(aiClassName);
317
							idenResultDto.setAlarmMemo(aiClassName);
318 318
							idenResultDtoList.add(idenResultDto);
319 319
							// aiIdenLogDto.setIdenResult(idenResultDtoList);
320 320
							aiIdenLogDto.setIdenResult(JSON.toJSONString(idenResultDtoList));
321
							aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT);
321 322
							// aiIdenLogDto.setAiIdenModel(MonitorManageConsts.AI_MODEL_CLOTHING_CODE);
322 323
							aiIdenLogDto.setRelateEmployeeRoleId(MonitorManageConsts.alarmEmployeeRoleId);
323
						} else {
324
							// if(aiClassName.equals("safety_hat")){
324
						}else{
325 325
							idenResultDto = new IdenResultDto();
326 326
							idenResultDto.setAlarmEmployeeRoleId(MonitorManageConsts.alarmEmployeeRoleId);
327
							idenResultDto.setAlarmType(aiClassName);
328
							idenResultDto.setAlarmMemo(aiClassName);
327
							idenResultDto.setAlarmType(MonitorManageConsts.AI_ALARM_TYPE_WEA);
328
							idenResultDto.setAlarmMemo("未戴安全帽");
329
							aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT_ALARM);
329 330
							idenResultDtoList.add(idenResultDto);
330 331
							// aiIdenLogDto.setIdenResult(idenResultDtoList);
331 332
							aiIdenLogDto.setIdenResult(JSON.toJSONString(idenResultDtoList));
332
							aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT);
333 333
							// aiIdenLogDto.setAiIdenModel(MonitorManageConsts.AI_MODEL_CLOTHING_CODE);
334 334
							aiIdenLogDto.setRelateEmployeeRoleId(MonitorManageConsts.alarmEmployeeRoleId);
335 335
						}
@ -403,31 +403,116 @@ public class KafkaProcessImpl implements KafkaProcess {
403 403
			String aiResponseData = ebcAiService.faceAiRecog(ImageBase64Converter.convertFileToBase64(aifilePath));
404 404
405 405
			JSONObject jsonObject = JSONObject.parseObject(aiResponseData);
406
			System.out.println("AI人脸识别结果: " + aiResponseData);
406
			log.info("AI人脸识别结果: " + aiResponseData);
407 407
408
			if ("1".equals(jsonObject.getString("errorcode")) && "success".equals(jsonObject.getString("message"))) {
409
				JSONObject resultJsonObject = JSONObject.parseObject(jsonObject.getString("result"));
408
			if (!"OK".equals(jsonObject.getString("Des")) || !"OK".equals(jsonObject.getString("Status"))) {
409
				log.info("人脸识别返回错误,不推送KAFKA消息");
410
				return aiIdenLogDto;
411
			}
412
413
			if (jsonObject.get("result")==null){
414
				log.info("人脸识别没有识别到人,不推送KAFKA消息");
415
				return aiIdenLogDto;
416
			}
417
418
			List<Map> resultList = JSONArray.parseArray(jsonObject.getString("result"),Map.class);
419
			FaceResultDto resultDto=null;
420
421
			for (Map resultMap:resultList) {
422
				resultDto=new FaceResultDto();
423
				String faceId=String.valueOf(resultMap.get("faceid"));
424
425
				if ("null".equals(faceId)||"Unknow".equals(faceId)){
426
					resultDto.setUserId(MonitorManageConsts.alarmEmployeeRoleId);
427
					resultDto.setSimi((float)resultMap.get("score"));
428
					resultDto.setIsMatch(0);
429
					resultDto.setFaceBox((int[]) resultMap.get("box"));
410 430
411
				if (resultJsonObject.getIntValue("is_match") == 0) {
412
					aiIdenLogDto.setIdenResult(jsonObject.getString("result"));
413 431
					aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT);
414
				} else {
415
					aiIdenLogDto.setIdenResult(jsonObject.getString("result"));
416
					aiIdenLogDto.setRelateEmployeeRoleId(resultJsonObject.getString("user_id"));
432
				}else {
433
					resultDto.setUserId(faceId);
434
					resultDto.setSimi((float)resultMap.get("score"));
435
					resultDto.setIsMatch(1);
436
					resultDto.setFaceBox((int[]) resultMap.get("box"));
437
438
					aiIdenLogDto.setRelateEmployeeRoleId(faceId);
417 439
					aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT_ATTENDANCE);
418 440
				}
419 441
442
				aiIdenLogDto.setIdenResult(JSON.toJSONString(resultDto));
420 443
				log.info("识别成功,推送KAFKA消息");
421 444
				processKafka(aiIdenLogDto.getTopic(), JSONObject.toJSONString(aiIdenLogDto));
422
			} else {
423
				log.info("没有识别到人,不推送KAFKA消息");
424
				return aiIdenLogDto;
425 445
			}
446
426 447
		} catch (Exception e) {
427 448
			log.error("AI处理异常,人脸识别");
428 449
		}
429 450
		return aiIdenLogDto;
430 451
	}
452
453
	/**
454
	 * 陌生人识别
455
	 * @param aiIdenLogDto
456
	 * @param aifilePath
457
	 * @return
458
	 */
459
	private AiIdenLogDto strangerDetect(AiIdenLogDto aiIdenLogDto, String aifilePath) {
460
		try {
461
			log.info("开始AI陌生人识别处理...");
462
			// 人脸识别
463
			String aiResponseData = ebcAiService.faceAiRecog(ImageBase64Converter.convertFileToBase64(aifilePath));
464
465
			JSONObject jsonObject = JSONObject.parseObject(aiResponseData);
466
			log.info("AI陌生人识别结果: " + aiResponseData);
467
468
			if (!"OK".equals(jsonObject.getString("Des")) || !"OK".equals(jsonObject.getString("Status"))) {
469
				log.info("陌生人识别返回错误,不推送KAFKA消息");
470
				return aiIdenLogDto;
471
			}
472
473
			if (jsonObject.get("result")==null){
474
				log.info("陌生人识别没有识别到人,不推送KAFKA消息");
475
				return aiIdenLogDto;
476
			}
477
478
			String resultJson=jsonObject.getString("result");
479
			List<Map> resultList = JSONArray.parseArray(resultJson,Map.class);
480
481
			IdenResultDto idenResultDto = null;
482
			List<IdenResultDto> idenResultDtoList = new ArrayList<IdenResultDto>();
483
484
			for (Map resultMap:resultList) {
485
				String faceId=String.valueOf(resultMap.get("faceid"));
486
				if ("null".equals(faceId)||"Unknow".equals(faceId)){
487
					log.info("检测到陌生人");
488
					idenResultDto = new IdenResultDto();
489
					idenResultDto.setAlarmEmployeeRoleId(MonitorManageConsts.alarmEmployeeRoleId);
490
					idenResultDto.setAlarmType(MonitorManageConsts.AI_ALARM_TYPE_STA);
491
					idenResultDto.setAlarmMemo("陌生人");
492
					idenResultDto.setAlarmBox((int[]) resultMap.get("box"));
493
					idenResultDtoList.add(idenResultDto);
494
				}
495
			}
496
497
			if (!CollectionUtils.isEmpty(idenResultDtoList)){
498
				aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT_ALARM);
499
				aiIdenLogDto.setIdenResult(JSON.toJSONString(idenResultDtoList));
500
				aiIdenLogDto.setRelateEmployeeRoleId(MonitorManageConsts.alarmEmployeeRoleId);
501
			}else{
502
				aiIdenLogDto.setIdenResult(resultJson);
503
				aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT);
504
			}
505
506
			log.info("陌生人识别成功,推送KAFKA消息");
507
			processKafka(aiIdenLogDto.getTopic(), JSONObject.toJSONString(aiIdenLogDto));
508
			log.info("消息推送完成");
509
510
		} catch (Exception e) {
511
			log.error("AI处理异常,陌生人识别");
512
		}
513
		return aiIdenLogDto;
514
	}
515
431 516
    /**
432 517
     * AI吸烟检测
433 518
     *
@ -437,6 +522,7 @@ public class KafkaProcessImpl implements KafkaProcess {
437 522
            log.info("开始AI抽烟识别处理...");
438 523
            //抽烟识别
439 524
            String aiResponseData=ebcAiService.smokingDetect(ImageBase64Converter.convertFileToBase64(aifilePath));
525
440 526
            IdenResultDto idenResultDto = null;
441 527
            List<IdenResultDto> idenResultDtoList = null;
442 528
            JSONObject jsonObject = JSONObject.parseObject(aiResponseData);

+ 24 - 5
monitor-manage-service/src/main/java/com/ai/bss/monitorManage/utils/FileUtils.java

@ -1,14 +1,33 @@
1 1
package com.ai.bss.monitorManage.utils;
2 2
3
import cn.hutool.core.io.FileUtil;
4
import com.ai.ipu.basic.string.Base64;
3
import java.io.BufferedInputStream;
4
import java.io.BufferedOutputStream;
5
import java.io.BufferedReader;
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.FileOutputStream;
9
import java.io.FileReader;
10
import java.io.FileWriter;
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.io.InputStreamReader;
14
import java.io.OutputStream;
15
import java.io.PrintWriter;
16
import java.io.UnsupportedEncodingException;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.Calendar;
20
import java.util.Date;
21
import java.util.HashMap;
22
import java.util.HashSet;
23
import java.util.List;
24
import java.util.Map;
25
5 26
import org.apache.commons.fileupload.FileItem;
6 27
import org.apache.commons.fileupload.FileItemFactory;
7 28
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
8 29
9
import java.io.*;
10
import java.nio.charset.Charset;
11
import java.util.*;
30
import cn.hutool.core.io.FileUtil;
12 31
13 32
14 33
public class FileUtils {

+ 9 - 5
monitor-manage-service/src/main/resources/application.properties

@ -53,9 +53,13 @@ minio.accessKey=minioadmin
53 53
minio.secretKey=minioadmin
54 54
minio.secure=false
55 55
minio.bucketHeaderImage=prod-dev
56
minio.faceAddServiceUrl=http://10.21.10.28:9018/api/face/add
57
minio.face-del-service-url=http://10.21.10.28:9018/api/face/del
58
minio.face-recog-service-url=http://10.21.10.28:9018/api/face/recog
56
#minio.faceAddServiceUrl=http://10.21.10.28:9018/api/face/add
57
#minio.face-del-service-url=http://10.21.10.28:9018/api/face/del
58
#minio.face-recog-service-url=http://10.21.10.28:9018/api/face/recog
59
minio.faceAddServiceUrl=http://10.1.251.211:7002/todo/api/v1.4/control/online/IMAGE/FACE/PREDICT
60
minio.face-del-service-url=http://10.1.251.211:7002/todo/api/v1.4/control/online/IMAGE/FACE/PREDICT
61
minio.face-recog-service-url=http://10.1.251.211:7002/todo/api/v1.4/control/online/IMAGE/FACE/PREDICT
62
minio.face-recog-mincosin-value=0.5
59 63
60 64
myminio.bucketAiImage=ai-image
61 65
myminio.bucketAiVideo=ai-video
@ -87,8 +91,8 @@ ebc.ai.smokingdet-url=http://10.1.251.213:5125/smokingdet
87 91
ebc.ai.instrument-det-url=http://10.1.251.213:19100/predict
88 92
ebc.ai.work-clothes-det-url=http://10.1.251.211:7003/todo/api/v1.4/control/online/IMAGE/HUMAN/PREDICT
89 93
90
ebc.ai.face-recog-service-url=http://10.21.10.28:9018/api/face/recog
91
94
#ebc.ai.face-recog-service-url=http://10.21.10.28:9018/api/face/recog
95
ebc.ai.face-recog-service-url=http://10.1.251.211:7002/todo/api/v1.4/control/online/IMAGE/FACE/PREDICT
92 96
93 97
#AI\u4efb\u52a1\u8bf7\u6c42\u5730\u5740
94 98
ebc.protection.resource-url=http://10.19.90.34:8018/sp/resourceTool/getResourceToolAllInfo

+ 7 - 3
security-protection-service/src/main/resources/application-minio.properties

@ -4,9 +4,13 @@ minio.port=19000
4 4
minio.accessKey=minioadmin
5 5
minio.secretKey=minioadmin
6 6
minio.secure=false
7
minio.faceAddServiceUrl=http://10.21.10.28:9018/api/face/add
8
minio.face-del-service-url=http://10.21.10.28:9018/api/face/del
9
minio.face-recog-service-url=http://10.21.10.28:9018/api/face/recog
7
#minio.faceAddServiceUrl=http://10.21.10.28:9018/api/face/add
8
#minio.face-del-service-url=http://10.21.10.28:9018/api/face/del
9
#minio.face-recog-service-url=http://10.21.10.28:9018/api/face/recog
10
minio.faceAddServiceUrl=http://10.1.251.211:7002/todo/api/v1.4/control/online/IMAGE/FACE/PREDICT
11
minio.face-del-service-url=http://10.1.251.211:7002/todo/api/v1.4/control/online/IMAGE/FACE/PREDICT
12
minio.face-recog-service-url=http://10.1.251.211:7002/todo/api/v1.4/control/online/IMAGE/FACE/PREDICT
13
minio.face-recog-mincosin-value=0.5
10 14
11 15
# \u4eba\u8138\u8bc6\u522b
12 16
minio.bucketHeaderImage=prod-dev