|
|
@@ -3,14 +3,15 @@ package com.zd.forward.serivce;
|
|
|
import com.zd.common.core.exception.ServiceException;
|
|
|
import com.zd.common.core.utils.IdGen;
|
|
|
import com.zd.forward.config.AlgorithmYml;
|
|
|
+import com.zd.forward.domain.AnalysisData;
|
|
|
import com.zd.forward.domain.DataPostAnalysisRespDto;
|
|
|
import com.zd.forward.domain.ImgPostResponse;
|
|
|
import com.zd.forward.properties.FireProperties;
|
|
|
-import com.zd.forward.util.HttpUtils;
|
|
|
+import com.zd.forward.utils.HttpUtils;
|
|
|
+import com.zd.forward.utils.VideoUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.bytedeco.javacv.FFmpegFrameGrabber;
|
|
|
import org.bytedeco.javacv.Frame;
|
|
|
-import org.bytedeco.javacv.FrameGrabber;
|
|
|
import org.bytedeco.javacv.Java2DFrameConverter;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
@@ -23,14 +24,17 @@ import javax.imageio.ImageIO;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
-import static com.zd.forward.util.HttpUtils.getHttpEntityMap;
|
|
|
+import static com.zd.forward.utils.HttpUtils.getHttpEntityMap;
|
|
|
|
|
|
/**
|
|
|
* 火焰图片抓拍处理
|
|
|
+ *
|
|
|
* @author Administrator
|
|
|
*/
|
|
|
@Service
|
|
|
@@ -42,12 +46,20 @@ public class FireImageService {
|
|
|
@Resource
|
|
|
private AlgorithmYml algorithmYml;
|
|
|
|
|
|
- @Resource(name = "restTemplateLocal")
|
|
|
- private RestTemplate restTemplateLocal;
|
|
|
+ @Resource
|
|
|
+ private RestTemplate restTemplate;
|
|
|
@Resource
|
|
|
private SendSginAccessLogService sendSginAccessLogService;
|
|
|
|
|
|
- private static final Integer SUCCESS_CODE=1000;
|
|
|
+ /**
|
|
|
+ * 算法接口返回值
|
|
|
+ */
|
|
|
+ private static final Integer SUCCESS_CODE = 1000;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成的图片的类型
|
|
|
+ */
|
|
|
+ private static final String IMAGE_FORMAT = "jpg";
|
|
|
|
|
|
/**
|
|
|
* 上传文件存储在本地的根路径
|
|
|
@@ -55,58 +67,65 @@ public class FireImageService {
|
|
|
@Value("${file.path:/home/AIPIC}")
|
|
|
private String imagePath;
|
|
|
|
|
|
- public void catchImage() {
|
|
|
-
|
|
|
+ public void catchImage() throws Exception {
|
|
|
String streamUrl = fireProperties.getStreamUrl();
|
|
|
if (streamUrl == null) {
|
|
|
throw new ServiceException("未配置流媒体地址");
|
|
|
}
|
|
|
|
|
|
- try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(streamUrl)) {
|
|
|
+ try (FFmpegFrameGrabber grabber = VideoUtils.createGrabber(streamUrl)) {
|
|
|
grabber.start();
|
|
|
+ String uuid = IdGen.uuid();
|
|
|
+ //文件储存对象
|
|
|
+ String fileName = imagePath + File.separator + uuid;
|
|
|
+ File tempFile = File.createTempFile(fileName, "." + IMAGE_FORMAT);
|
|
|
//获取第一帧
|
|
|
Frame frame = grabber.grabFrame();
|
|
|
if (frame != null) {
|
|
|
- int flag = 0;
|
|
|
- while (frame != null) {
|
|
|
- //视频快照
|
|
|
- frame = grabber.grabImage();
|
|
|
- long uuid = IdGen.snowflakeId();
|
|
|
- //文件储存对象
|
|
|
- String fileName = imagePath + File.separator + uuid + "_" + flag;
|
|
|
- File tempFile = File.createTempFile(fileName, ".jpg");
|
|
|
- ImageIO.write(frameToBufferedImage(frame), "jpg", tempFile);
|
|
|
- send(tempFile);
|
|
|
+ //视频快照
|
|
|
+ frame = grabber.grabImage();
|
|
|
+ BufferedImage bufferedImage = frameToBufferedImage(frame);
|
|
|
+ if (bufferedImage != null) {
|
|
|
+ ImageIO.write(bufferedImage, IMAGE_FORMAT, tempFile);
|
|
|
}
|
|
|
- //停止
|
|
|
+ log.info("图片地址为[{}]", tempFile.getAbsoluteFile());
|
|
|
grabber.stop();
|
|
|
+ send(tempFile);
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- throw new ServiceException(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void send(File file) {
|
|
|
+ private void send(File file) throws IOException {
|
|
|
if (fireProperties.getAlgoId() == null) {
|
|
|
throw new ServiceException("未配置火焰算法ID");
|
|
|
}
|
|
|
MultiValueMap<String, Object> params = HttpUtils.getMultiValueMap(fireProperties, null);
|
|
|
HttpEntity<MultiValueMap<String, Object>> files = getHttpEntityMap(file, params);
|
|
|
- ImgPostResponse<DataPostAnalysisRespDto> send = HttpUtils.send(restTemplateLocal, files, algorithmYml);
|
|
|
+ ImgPostResponse<DataPostAnalysisRespDto> send = HttpUtils.send(restTemplate, files, algorithmYml);
|
|
|
if (send == null || send.getStatus_code() != SUCCESS_CODE) {
|
|
|
assert send != null;
|
|
|
log.error(send.getMessage());
|
|
|
- throw new ServiceException(send.getMessage());
|
|
|
+ return;
|
|
|
}
|
|
|
DataPostAnalysisRespDto data = send.getData();
|
|
|
|
|
|
- Map<String, Object> result = (Map<String, Object>) Optional.ofNullable(data.getAnalysisDatas().get(0).getResult()).orElse(Collections.emptyMap());
|
|
|
- Map<String, Object> algorithmData = (Map<String, Object>) Optional.ofNullable(result.get("algorithm_data")).orElse(Collections.emptyMap());
|
|
|
- boolean alert = "false".equals(algorithmData.getOrDefault("is_alert", "").toString());
|
|
|
- if (!alert) {
|
|
|
- sendSginAccessLogService.saveAlarm(fireProperties);
|
|
|
+ List<AnalysisData> analysisDatas = data.getAnalysisDatas();
|
|
|
+ AnalysisData analysisData = analysisDatas.get(0);
|
|
|
+ int code = analysisData.getCode();
|
|
|
+ if (code==-1){
|
|
|
+ log.error("==============请求失败:{}=================",analysisData.getMsg());
|
|
|
+ throw new ServiceException(analysisData.getMsg());
|
|
|
+ }else {
|
|
|
+ log.info("===============向算法服务发送数据完成====================");
|
|
|
+ Map<String, Object> result = (Map<String, Object>) Optional.ofNullable(analysisData.getResult()).orElse(Collections.emptyMap());
|
|
|
+ Map<String, Object> algorithmData = (Map<String, Object>) Optional.ofNullable(result.get("algorithm_data")).orElse(Collections.emptyMap());
|
|
|
+ boolean alert = "false".equals(algorithmData.getOrDefault("is_alert", "").toString());
|
|
|
+ if (!alert) {
|
|
|
+ log.info("===============返回告警信息====================");
|
|
|
+ sendSginAccessLogService.saveAlarm(fireProperties);
|
|
|
+ }
|
|
|
}
|
|
|
+ Files.delete(file.getAbsoluteFile().toPath());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -114,7 +133,7 @@ public class FireImageService {
|
|
|
*/
|
|
|
public static BufferedImage frameToBufferedImage(Frame frame) {
|
|
|
//创建BufferedImage对象
|
|
|
- try (Java2DFrameConverter converter = new Java2DFrameConverter()){
|
|
|
+ try (Java2DFrameConverter converter = new Java2DFrameConverter()) {
|
|
|
return converter.getBufferedImage(frame);
|
|
|
}
|
|
|
}
|