Parcourir la Source

修改Ai返回参数

Signed-off-by: konghl <konghl@asiainfo.com>
konghl 4 ans auparavant
Parent
commit
f5ea8487d0

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

@ -8,7 +8,6 @@ import java.util.List;
8 8
import java.util.Map;
9 9
10 10
import com.ai.bss.monitorManage.utils.*;
11
import com.sun.javafx.collections.MappingChange;
12 11
import org.apache.commons.lang.StringUtils;
13 12
import org.apache.logging.log4j.util.Strings;
14 13
import org.slf4j.Logger;
@ -146,6 +145,8 @@ public class KafkaProcessImpl implements KafkaProcess {
146 145
			List<Map> resourceToolInfoList = getResourceToolAllInfo(deviceCode);
147 146
			boolean isHuman=aiworkHumanDetect(filePath);
148 147
148
			Map<String,Integer> imageParam=AiIdentifyUtils.getImageParam(filePath);
149
149 150
			for (Map resourceToolInfoMap : resourceToolInfoList) {
150 151
				if (resourceToolInfoMap.get("aiTaskId") != null ) {
151 152
					String aiIdenModel = resourceToolInfoMap.get("aiIdenModel") == null ? ""
@ -163,22 +164,22 @@ public class KafkaProcessImpl implements KafkaProcess {
163 164
164 165
					if (MonitorManageConsts.AI_MODEL_FACE.equals(aiIdenModel)) {
165 166
						if (isHuman){
166
							aiIdenLogDto = faceAiRecog(aiIdenLogDto, filePath);
167
							aiIdenLogDto = faceAiRecog(aiIdenLogDto, filePath,imageParam);
167 168
						}
168 169
169 170
					} else if (MonitorManageConsts.AI_MODEL_SAFETY_HELMET.equals(aiIdenModel)) {
170 171
						if (isHuman) {
171
							aiIdenLogDto = aiHelmetDetect(aiIdenLogDto, filePath);
172
							aiIdenLogDto = aiHelmetDetect(aiIdenLogDto, filePath,imageParam);
172 173
						}
173 174
174 175
					}else if (MonitorManageConsts.AI_MODEL_SMOKE.equals(aiIdenModel)) {
175 176
							if (isHuman){
176
								aiIdenLogDto = smokingDetect(aiIdenLogDto, filePath);//吸烟识别
177
								aiIdenLogDto = smokingDetect(aiIdenLogDto, filePath,imageParam);//吸烟识别
177 178
						}
178 179
179 180
					}else if (MonitorManageConsts.AI_MODEL_CLOTHING_CODE.equals(aiIdenModel)) {
180 181
							if (isHuman){
181
								aiIdenLogDto=workClothesDetect(aiIdenLogDto, filePath);//工作服识别
182
								aiIdenLogDto=workClothesDetect(aiIdenLogDto, filePath,imageParam);//工作服识别
182 183
							}
183 184
						}
184 185
@ -186,7 +187,9 @@ public class KafkaProcessImpl implements KafkaProcess {
186 187
						aiIdenLogDto=instrumentDetect(aiIdenLogDto,filePath);//仪表识别
187 188
188 189
					} else if (MonitorManageConsts.AI_MODEL_STRANGER.equals(aiIdenModel)){
189
						aiIdenLogDto=strangerDetect(aiIdenLogDto,filePath);//陌生人识别
190
						if (isHuman) {
191
							aiIdenLogDto = strangerDetect(aiIdenLogDto, filePath, imageParam);//陌生人识别
192
						}
190 193
191 194
					} else {
192 195
@ -293,7 +296,7 @@ public class KafkaProcessImpl implements KafkaProcess {
293 296
	 * @param aiIdenLogDto
294 297
	 * @return
295 298
	 */
296
	private AiIdenLogDto aiHelmetDetect(AiIdenLogDto aiIdenLogDto, String aifilePath) {
299
	private AiIdenLogDto aiHelmetDetect(AiIdenLogDto aiIdenLogDto, String aifilePath,Map<String,Integer> imageParam) {
297 300
		try {
298 301
			log.info("开始AI帽子识别处理...");
299 302
			// 安全帽识别
@ -318,7 +321,7 @@ public class KafkaProcessImpl implements KafkaProcess {
318 321
							idenResultDto.setAlarmType(aiClassName);
319 322
							idenResultDto.setAlarmMemo(aiClassName);
320 323
							Map resultMap = (Map)jsonoArray.get(i);
321
							int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray)resultMap.get("box"));
324
							int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray)resultMap.get("box"),imageParam);
322 325
							idenResultDto.setBox(boxIntArr);
323 326
324 327
							idenResultDtoList.add(idenResultDto);
@ -334,7 +337,7 @@ public class KafkaProcessImpl implements KafkaProcess {
334 337
							idenResultDto.setAlarmMemo("未戴安全帽");
335 338
336 339
							Map resultMap = (Map)jsonoArray.get(i);
337
							int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray)resultMap.get("box"));
340
							int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray)resultMap.get("box"),imageParam);
338 341
							idenResultDto.setBox(boxIntArr);
339 342
340 343
							aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT_ALARM);
@ -407,7 +410,7 @@ public class KafkaProcessImpl implements KafkaProcess {
407 410
	 * @param aifilePath
408 411
	 * @return
409 412
	 */
410
	private AiIdenLogDto faceAiRecog(AiIdenLogDto aiIdenLogDto, String aifilePath) {
413
	private AiIdenLogDto faceAiRecog(AiIdenLogDto aiIdenLogDto, String aifilePath,Map<String,Integer> imageParam) {
411 414
		try {
412 415
			log.info("开始AI人脸识别处理...");
413 416
			// 人脸识别
@ -440,7 +443,7 @@ public class KafkaProcessImpl implements KafkaProcess {
440 443
					resultDto.setSimi(score.setScale(4,BigDecimal.ROUND_HALF_UP).floatValue());
441 444
					resultDto.setIsMatch(0);
442 445
443
					int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray) resultMap.get("box"));
446
					int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray) resultMap.get("box"),imageParam);
444 447
					resultDto.setBox(boxIntArr);
445 448
446 449
					aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT);
@ -450,7 +453,7 @@ public class KafkaProcessImpl implements KafkaProcess {
450 453
					resultDto.setSimi(score.setScale(4,BigDecimal.ROUND_HALF_UP).floatValue());
451 454
					resultDto.setIsMatch(1);
452 455
453
					int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray) resultMap.get("box"));
456
					int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray) resultMap.get("box"),imageParam);
454 457
					resultDto.setBox(boxIntArr);
455 458
456 459
					aiIdenLogDto.setRelateEmployeeRoleId(faceId);
@ -474,7 +477,7 @@ public class KafkaProcessImpl implements KafkaProcess {
474 477
	 * @param aifilePath
475 478
	 * @return
476 479
	 */
477
	private AiIdenLogDto strangerDetect(AiIdenLogDto aiIdenLogDto, String aifilePath) {
480
	private AiIdenLogDto strangerDetect(AiIdenLogDto aiIdenLogDto, String aifilePath,Map<String,Integer> imageParam) {
478 481
		try {
479 482
			log.info("开始AI陌生人识别处理...");
480 483
			// 人脸识别
@ -508,7 +511,7 @@ public class KafkaProcessImpl implements KafkaProcess {
508 511
					idenResultDto.setAlarmType(MonitorManageConsts.AI_ALARM_TYPE_STA);
509 512
					idenResultDto.setAlarmMemo("陌生人");
510 513
511
					int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray) resultMap.get("box"));
514
					int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray) resultMap.get("box"),imageParam);
512 515
					idenResultDto.setBox(boxIntArr);
513 516
514 517
					idenResultDtoList.add(idenResultDto);
@ -538,7 +541,7 @@ public class KafkaProcessImpl implements KafkaProcess {
538 541
     * AI吸烟检测
539 542
     *
540 543
     */
541
    private AiIdenLogDto smokingDetect(AiIdenLogDto aiIdenLogDto, String aifilePath){
544
    private AiIdenLogDto smokingDetect(AiIdenLogDto aiIdenLogDto, String aifilePath,Map<String,Integer> imageParam){
542 545
        try{
543 546
            log.info("开始AI抽烟识别处理...");
544 547
            //抽烟识别
@ -564,7 +567,7 @@ public class KafkaProcessImpl implements KafkaProcess {
564 567
                                    idenResultDto.setAlarmMemo("违规吸烟");
565 568
566 569
								    Map resultMap = (Map) jsonArray.get(i);
567
									int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray) resultMap.get("box"));
570
									int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray) resultMap.get("box"),imageParam);
568 571
									idenResultDto.setBox(boxIntArr);
569 572
570 573
                                    log.info("检测到违规吸烟");
@ -595,7 +598,7 @@ public class KafkaProcessImpl implements KafkaProcess {
595 598
*
596 599
*
597 600
* */
598
private AiIdenLogDto workClothesDetect(AiIdenLogDto aiIdenLogDto, String aifilePath){
601
private AiIdenLogDto workClothesDetect(AiIdenLogDto aiIdenLogDto, String aifilePath,Map<String,Integer> imageParam){
599 602
	try{
600 603
		log.info("开始服装识别处理...");
601 604
		//工作服识别
@ -618,7 +621,7 @@ private AiIdenLogDto workClothesDetect(AiIdenLogDto aiIdenLogDto, String aifileP
618 621
						idenResultDto.setAlarmMemo("未穿工作服");
619 622
						log.info("未穿工作服");
620 623
621
						int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray) resultList.get(i).get("box"));
624
						int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray) resultList.get(i).get("box"),imageParam);
622 625
						idenResultDto.setBox(boxIntArr);
623 626
624 627
						aiIdenLogDto.setIdenResultType(MonitorManageConsts.AI_IDENTIFY_RESULT_ALARM);
@ -631,7 +634,7 @@ private AiIdenLogDto workClothesDetect(AiIdenLogDto aiIdenLogDto, String aifileP
631 634
						idenResultDto.setAlarmType(aiClassName);
632 635
						idenResultDto.setAlarmMemo(aiClassName);
633 636
634
						int[] boxIntArr = ArrayUtils.JsonToArray((JSONArray) resultList.get(i).get("box"));
637
						int[] boxIntArr = AiIdentifyUtils.getBoxArray((JSONArray) resultList.get(i).get("box"),imageParam);
635 638
						idenResultDto.setBox(boxIntArr);
636 639
637 640
						idenResultDtoList.add(idenResultDto);

+ 102 - 0
monitor-manage-service/src/main/java/com/ai/bss/monitorManage/utils/AiIdentifyUtils.java

@ -0,0 +1,102 @@
1
package com.ai.bss.monitorManage.utils;
2
3
import com.alibaba.fastjson.JSONArray;
4
import com.mzlion.core.lang.CollectionUtils;
5
import lombok.extern.slf4j.Slf4j;
6
7
import javax.imageio.ImageIO;
8
import java.awt.image.BufferedImage;
9
import java.io.File;
10
import java.io.IOException;
11
import java.io.InputStream;
12
import java.math.BigDecimal;
13
import java.net.MalformedURLException;
14
import java.net.URL;
15
import java.util.Collections;
16
import java.util.HashMap;
17
import java.util.Map;
18
19
/**
20
 * @Auther: 王超
21
 * @Date: 2021/2/3 17:49
22
 * @Description:
23
 */
24
@Slf4j
25
public class AiIdentifyUtils {
26
27
    /**
28
     * JSONArray转int[]
29
     * @param jsonArray 原json
30
     *
31
     * @return boxIntArr
32
     */
33
    public static int[] getBoxArray(JSONArray jsonArray,Map<String,Integer> getImageParam) {
34
        if (CollectionUtils.isEmpty(jsonArray)|| jsonArray.size() !=4 ||CollectionUtils.isEmpty(getImageParam)){
35
            return new int[4];
36
        }
37
38
        /*int[] boxIntArr=new int[jsonArray.size()];
39
        for(int i = 0;i<jsonArray.size();i++){
40
            BigDecimal boxValue=(BigDecimal) jsonArray.get(i);
41
            boxIntArr[i] =boxValue.intValue();
42
        }*/
43
44
        int[] boxIntArr=new int[4];
45
        try{
46
            BigDecimal boxValue=(BigDecimal) jsonArray.get(0);
47
            boxIntArr[0] =boxValue.divide(new BigDecimal(getImageParam.get("width")),0,BigDecimal.ROUND_FLOOR).intValue();
48
            boxValue=(BigDecimal) jsonArray.get(1);
49
            boxIntArr[1] =boxValue.divide(new BigDecimal(getImageParam.get("height")),0,BigDecimal.ROUND_FLOOR).intValue();
50
            boxValue=(BigDecimal) jsonArray.get(2);
51
            boxIntArr[2] =boxValue.divide(new BigDecimal(getImageParam.get("width")),0,BigDecimal.ROUND_CEILING).intValue();
52
            boxValue=(BigDecimal) jsonArray.get(3);
53
            boxIntArr[3] =boxValue.divide(new BigDecimal(getImageParam.get("height")),0,BigDecimal.ROUND_CEILING).intValue();
54
        } catch (Exception e) {
55
            log.error("getBoxArray is error: errorMsg="+e.getMessage()+", jsonArray="+jsonArray);
56
        }
57
        return boxIntArr;
58
    }
59
60
    /**
61
     * 获取图片的长度和宽度
62
     * @param filePath
63
     * @return
64
     */
65
    public static Map<String,Integer> getImageParam(String filePath){
66
        Map<String,Integer> param=null;
67
        BufferedImage image = getBufferedImage(filePath);
68
        if (image!=null){
69
            param=new HashMap<String,Integer>();
70
            param.put("width",image.getWidth());// 图片宽度
71
            param.put("height",image.getHeight());// 图片高度
72
        }
73
        return param;
74
    }
75
76
    /**
77
     * 获取图片对象
78
     * @param filePath 图片地址
79
     * @return
80
     */
81
    private static BufferedImage getBufferedImage(String filePath) {
82
        BufferedImage img = null;
83
        InputStream is = null;
84
        try {
85
            URL url = new URL(filePath);
86
            is = url.openStream();
87
            img = ImageIO.read(is);
88
        } catch (MalformedURLException e) {
89
            log.error("getBufferedImage is MalformedURLException: "+e.getMessage()+", filePath="+filePath);
90
        } catch (IOException e) {
91
            log.error("getBufferedImage is IOException: "+e.getMessage()+", filePath="+filePath);
92
        } finally {
93
            try {
94
                is.close();
95
            } catch (IOException e) {
96
                log.error("getBufferedImage is IOException1: "+e.getMessage()+", filePath="+filePath);
97
            }
98
        }
99
        return img;
100
    }
101
102
}

+ 0 - 32
monitor-manage-service/src/main/java/com/ai/bss/monitorManage/utils/ArrayUtils.java

@ -1,32 +0,0 @@
1
package com.ai.bss.monitorManage.utils;
2
3
import com.alibaba.fastjson.JSONArray;
4
5
import java.math.BigDecimal;
6
import java.util.Calendar;
7
import java.util.Date;
8
9
/**
10
 * @Auther: 王超
11
 * @Date: 2021/2/3 17:49
12
 * @Description:
13
 */
14
public class ArrayUtils {
15
16
    /**
17
     * JSONArray转int[]
18
     * @param jsonArray 原json
19
     *
20
     * @return boxIntArr
21
     */
22
    public static int[] JsonToArray(JSONArray jsonArray) {
23
        int[] boxIntArr=new int[jsonArray.size()];
24
        for(int i = 0;i<jsonArray.size();i++){
25
            BigDecimal boxValue=(BigDecimal) jsonArray.get(i);
26
            boxIntArr[i] =boxValue.intValue();
27
        }
28
        return boxIntArr;
29
    }
30
31
32
}