655 lines
38 KiB
Plaintext
655 lines
38 KiB
Plaintext
|
|
Index: securityControl-modules/securityControl-proteam/src/main/java/com/sercurityControl/proteam/util/VideoUtil.java
|
|||
|
|
IDEA additional info:
|
|||
|
|
Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
|
|||
|
|
<+>package com.sercurityControl.proteam.util;\r\n\r\nimport cn.hutool.http.HttpResponse;\r\nimport cn.hutool.http.HttpUtil;\r\nimport com.alibaba.fastjson2.JSON;\r\nimport com.securityControl.common.core.utils.DateUtils;\r\nimport com.securityControl.common.core.utils.file.ImageUtils;\r\nimport com.sercurityControl.proteam.domain.TImageLibrary;\r\nimport com.sercurityControl.proteam.service.TImageLibraryService;\r\nimport lombok.Data;\r\nimport org.bytedeco.javacv.FFmpegFrameGrabber;\r\nimport org.bytedeco.javacv.Frame;\r\nimport org.bytedeco.javacv.Java2DFrameConverter;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.beans.factory.annotation.Value;\r\nimport org.springframework.stereotype.Component;\r\n\r\nimport javax.imageio.ImageIO;\r\nimport java.awt.*;\r\nimport java.awt.image.BufferedImage;\r\nimport java.io.*;\r\nimport java.util.*;\r\nimport java.util.List;\r\nimport java.util.stream.Collectors;\r\n\r\n/**\r\n * @author xxx\r\n */\r\n\r\n@Component\r\npublic class VideoUtil {\r\n\r\n private static final Logger log = LoggerFactory.getLogger(VideoUtil.class);\r\n\r\n private static final Map<String, Object> responseMp = new HashMap<>(4);\r\n\r\n/**\r\n * 统一视频平台地址\r\n */\r\n\r\n @Value(\"${video.address}\")\r\n private String VIDEO_ADDRESS;\r\n\r\n/**\r\n * 统一视频平台账号\r\n */\r\n\r\n @Value(\"${video.username}\")\r\n private String VIDEO_USERNAME;\r\n\r\n/**\r\n * 统一视频平台密码\r\n */\r\n\r\n @Value(\"${video.password}\")\r\n private String VIDEO_PASSWORD;\r\n\r\n/**\r\n * 图片文件存储在本地的根路径\r\n */\r\n\r\n @Value(\"${file.path}\")\r\n private String localFilePath;\r\n\r\n/**\r\n * 人工智能平台请求接口\r\n */\r\n\r\n @Value(\"${ai.url}\")\r\n private String url;\r\n\r\n @Autowired\r\n private TImageLibraryService tImageLibraryService;\r\n\r\n/**\r\n * 违章类型\r\n */\r\n\r\n private static final String[] RISK_CONSTANTS = new String[]{\"未佩戴安全帽\", \"未规范着装\", \"未佩戴安全带\", \"警戒区闯入\", \"吸烟\", \"作业无监护\", \"登高行为\", \"作业现场无人\"};\r\n\r\n\r\n/**\r\n * 登录统一视频平台登录接口\r\n */\r\n\r\n private final String loginUrl = \"/uvp-backend-common/api/v1/authorization\";\r\n\r\n/**\r\n * 登录统一视频平台视频流接口\r\n */\r\n\r\n private final String videoUrl = \"/uvp-backend-common/api/v1/resource/queryDev?ak=%s&token=%s×tamp=1609297100943&nonce=00b215ed4710491e89786a6426b1aa0e\";\r\n\r\n\r\n/**\r\n * 获取统一视频平台视频流\r\n */\r\n\r\n private File getVideoStream() {\r\n String formatUrl = String.format(videoUrl, VIDEO_USERNAME, getToken());\r\n HttpResponse execute = HttpUtil.createPost(VIDEO_ADDRESS + formatUrl).execute();\r\n boolean ok = execute.isOk();\r\n log.info(\"请求统一视屏平台获取视频流接口结果是否成功:{}\", ok ? \"成功\" : \"失败\");\r\n if (ok) {\r\n String responseBody = execute.body();\r\n log.info(\"统一视屏平台登录返回结果:{}\", responseBody);\r\n File file = JSON.parseObject(responseBody, File.class);\r\n return file;\r\n\r\n }\r\n return null;\r\n }\r\n\r\n\r\n/**\r\n * 统一视频平台登录获取token\r\n *\r\n * @return\r\n */\r\n\r\n private String getToken() {\r\n Map<String, Object> body = new HashMap<>();\r\n body.put(\"ak\", VIDEO_USERNAME);\r\n body.put(\"sk\", VIDEO_PASSWORD);\r\n HttpResponse execute = HttpUtil.createPost(VIDEO_ADDRESS + loginUrl).body(JSON.toJSONString(body)).execute();\r\n boolean ok = execute.isOk();\r\n log.info(\"请求统一视屏平台登录接口结果是否成功:{}\", ok ? \"成功\" : \"失败\");\r\n if (ok) {\r\n String respons
|
|||
|
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
|||
|
|
<+>UTF-8
|
|||
|
|
===================================================================
|
|||
|
|
--- securityControl-modules/securityControl-proteam/src/main/java/com/sercurityControl/proteam/util/VideoUtil.java (revision 58a17f0f4b1fa497016769eab3a4352daf5cb11d)
|
|||
|
|
+++ securityControl-modules/securityControl-proteam/src/main/java/com/sercurityControl/proteam/util/VideoUtil.java (date 1677641998857)
|
|||
|
|
@@ -1,322 +1,322 @@
|
|||
|
|
-package com.sercurityControl.proteam.util;
|
|||
|
|
-
|
|||
|
|
-import cn.hutool.http.HttpResponse;
|
|||
|
|
-import cn.hutool.http.HttpUtil;
|
|||
|
|
-import com.alibaba.fastjson2.JSON;
|
|||
|
|
-import com.securityControl.common.core.utils.DateUtils;
|
|||
|
|
-import com.securityControl.common.core.utils.file.ImageUtils;
|
|||
|
|
-import com.sercurityControl.proteam.domain.TImageLibrary;
|
|||
|
|
-import com.sercurityControl.proteam.service.TImageLibraryService;
|
|||
|
|
-import lombok.Data;
|
|||
|
|
-import org.bytedeco.javacv.FFmpegFrameGrabber;
|
|||
|
|
-import org.bytedeco.javacv.Frame;
|
|||
|
|
-import org.bytedeco.javacv.Java2DFrameConverter;
|
|||
|
|
-import org.slf4j.Logger;
|
|||
|
|
-import org.slf4j.LoggerFactory;
|
|||
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|||
|
|
-import org.springframework.stereotype.Component;
|
|||
|
|
-
|
|||
|
|
-import javax.imageio.ImageIO;
|
|||
|
|
-import java.awt.*;
|
|||
|
|
-import java.awt.image.BufferedImage;
|
|||
|
|
-import java.io.*;
|
|||
|
|
-import java.util.*;
|
|||
|
|
-import java.util.List;
|
|||
|
|
-import java.util.stream.Collectors;
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * @author xxx
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
-@Component
|
|||
|
|
-public class VideoUtil {
|
|||
|
|
-
|
|||
|
|
- private static final Logger log = LoggerFactory.getLogger(VideoUtil.class);
|
|||
|
|
-
|
|||
|
|
- private static final Map<String, Object> responseMp = new HashMap<>(4);
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 统一视频平台地址
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- @Value("${video.address}")
|
|||
|
|
- private String VIDEO_ADDRESS;
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 统一视频平台账号
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- @Value("${video.username}")
|
|||
|
|
- private String VIDEO_USERNAME;
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 统一视频平台密码
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- @Value("${video.password}")
|
|||
|
|
- private String VIDEO_PASSWORD;
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 图片文件存储在本地的根路径
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- @Value("${file.path}")
|
|||
|
|
- private String localFilePath;
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 人工智能平台请求接口
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- @Value("${ai.url}")
|
|||
|
|
- private String url;
|
|||
|
|
-
|
|||
|
|
- @Autowired
|
|||
|
|
- private TImageLibraryService tImageLibraryService;
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 违章类型
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- private static final String[] RISK_CONSTANTS = new String[]{"未佩戴安全帽", "未规范着装", "未佩戴安全带", "警戒区闯入", "吸烟", "作业无监护", "登高行为", "作业现场无人"};
|
|||
|
|
-
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 登录统一视频平台登录接口
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- private final String loginUrl = "/uvp-backend-common/api/v1/authorization";
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 登录统一视频平台视频流接口
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- private final String videoUrl = "/uvp-backend-common/api/v1/resource/queryDev?ak=%s&token=%s×tamp=1609297100943&nonce=00b215ed4710491e89786a6426b1aa0e";
|
|||
|
|
-
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 获取统一视频平台视频流
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- private File getVideoStream() {
|
|||
|
|
- String formatUrl = String.format(videoUrl, VIDEO_USERNAME, getToken());
|
|||
|
|
- HttpResponse execute = HttpUtil.createPost(VIDEO_ADDRESS + formatUrl).execute();
|
|||
|
|
- boolean ok = execute.isOk();
|
|||
|
|
- log.info("请求统一视屏平台获取视频流接口结果是否成功:{}", ok ? "成功" : "失败");
|
|||
|
|
- if (ok) {
|
|||
|
|
- String responseBody = execute.body();
|
|||
|
|
- log.info("统一视屏平台登录返回结果:{}", responseBody);
|
|||
|
|
- File file = JSON.parseObject(responseBody, File.class);
|
|||
|
|
- return file;
|
|||
|
|
-
|
|||
|
|
- }
|
|||
|
|
- return null;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 统一视频平台登录获取token
|
|||
|
|
- *
|
|||
|
|
- * @return
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- private String getToken() {
|
|||
|
|
- Map<String, Object> body = new HashMap<>();
|
|||
|
|
- body.put("ak", VIDEO_USERNAME);
|
|||
|
|
- body.put("sk", VIDEO_PASSWORD);
|
|||
|
|
- HttpResponse execute = HttpUtil.createPost(VIDEO_ADDRESS + loginUrl).body(JSON.toJSONString(body)).execute();
|
|||
|
|
- boolean ok = execute.isOk();
|
|||
|
|
- log.info("请求统一视屏平台登录接口结果是否成功:{}", ok ? "成功" : "失败");
|
|||
|
|
- if (ok) {
|
|||
|
|
- String responseBody = execute.body();
|
|||
|
|
- log.info("统一视屏平台登录返回结果:{}", responseBody);
|
|||
|
|
- Map<String, Object> resultMap = JSON.parseObject(responseBody, Map.class);
|
|||
|
|
- return (String) resultMap.get("resultValue");
|
|||
|
|
-
|
|||
|
|
- }
|
|||
|
|
- return null;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 根据读取到的视频文件,获取视频中的每一帧图片
|
|||
|
|
- *
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- public void getVideoPic() {
|
|||
|
|
- //统一视频平台获取视频流
|
|||
|
|
- File video = getVideoStream();
|
|||
|
|
- if (null == video) {
|
|||
|
|
- log.info("未获取到实时视频!!!");
|
|||
|
|
- return;
|
|||
|
|
- }
|
|||
|
|
- //1.根据一个视频文件(视频流),创建一个按照视频中每一帧进行抓取图片的抓取对象
|
|||
|
|
- FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
|
|||
|
|
- Map<String, Object> strMap = new HashMap<>();
|
|||
|
|
- List<String> strList = new ArrayList<>();
|
|||
|
|
- try {
|
|||
|
|
- ff.start();
|
|||
|
|
- //抓每一帧图片
|
|||
|
|
- //2.先知道这个视频一共有多少帧
|
|||
|
|
- int length = ff.getLengthInFrames();
|
|||
|
|
- log.info("视频帧数{}", length);
|
|||
|
|
- //3.读取视频中每一帧图片
|
|||
|
|
- Frame frame = null;
|
|||
|
|
- TImageLibrary imageLibrary;
|
|||
|
|
- for (int i = 1; i < length; i++) {
|
|||
|
|
- frame = ff.grabFrame();
|
|||
|
|
- if (frame.image == null) {
|
|||
|
|
- continue;
|
|||
|
|
- }
|
|||
|
|
- //将获取的帧,存储为图片
|
|||
|
|
- Java2DFrameConverter converter = new Java2DFrameConverter();//创建一个帧-->图片的转换器
|
|||
|
|
- BufferedImage image = converter.getBufferedImage(frame);//转换
|
|||
|
|
- //图片原始高度宽度
|
|||
|
|
- int height = image.getHeight();
|
|||
|
|
- int width = image.getWidth();
|
|||
|
|
- //压缩比例
|
|||
|
|
- float scale = 0.5f;
|
|||
|
|
- //压缩后高度和宽度
|
|||
|
|
- int doWithHeight = (int) (scale * height);
|
|||
|
|
- int doWithWidth = (int) (scale * width);
|
|||
|
|
- //图片压缩
|
|||
|
|
- BufferedImage finalImage = new BufferedImage(doWithWidth, doWithHeight, BufferedImage.TYPE_INT_RGB);
|
|||
|
|
- finalImage.getGraphics().drawImage(image.getScaledInstance(doWithWidth, doWithHeight, Image.SCALE_SMOOTH), 0, 0, null);
|
|||
|
|
- //将图片保存到目标文件中
|
|||
|
|
- String img = localFilePath + i + ".png";
|
|||
|
|
- File picFile = new File(img);
|
|||
|
|
- ImageIO.write(finalImage, "png", picFile);
|
|||
|
|
- //处理发送请求
|
|||
|
|
- Map<String, Object> body = new HashMap<>();
|
|||
|
|
- //aqm:安全帽,gz:未着装规范,aqd:未佩戴安全帽,jjqcr:警戒区闯入,xy:吸烟,zywjh:作业无监护
|
|||
|
|
- String[] arr = new String[]{"aqm", "gz", "aqd", "jjqcr", "xy", "zywjh"};
|
|||
|
|
- body.put("detect_type", arr);
|
|||
|
|
- body.put("format", "0");
|
|||
|
|
- body.put("image", ImageUtils.getImgBase(img));
|
|||
|
|
- HttpResponse execute = HttpUtil.createPost(url).body(JSON.toJSONString(body)).execute();
|
|||
|
|
- boolean ok = execute.isOk();
|
|||
|
|
- log.info("请求结果是否成功:{}", ok ? "成功" : "失败");
|
|||
|
|
- if (ok) {
|
|||
|
|
- String responseBody = execute.body();
|
|||
|
|
- log.info("返回结果:{}", responseBody);
|
|||
|
|
- responseData responseData = JSON.parseObject(responseBody, responseData.class);
|
|||
|
|
- List<ImageDefect> imageDefect = responseData.getImageDefect();
|
|||
|
|
- String className = imageDefect.stream().map(ImageDefect::getClassName).collect(Collectors.joining(","));
|
|||
|
|
- log.info("className为:{}", className);
|
|||
|
|
- //是否含有违章行为
|
|||
|
|
- boolean illegal = hasIllegalBehavior(imageDefect);
|
|||
|
|
- log.info("动作是否违章:{}", illegal ? "没有违章" : "违章");
|
|||
|
|
- //判断是否为重复动作&&是否有违章行为
|
|||
|
|
- String preClassName = (String) responseMp.get("className");
|
|||
|
|
- if (Objects.equals(preClassName, className)) {
|
|||
|
|
- log.info("重复动作,不做处理");
|
|||
|
|
- //动作没变化且没有违章则删除
|
|||
|
|
- if (illegal) {
|
|||
|
|
- log.info("");
|
|||
|
|
- if (picFile.exists()) {
|
|||
|
|
- log.info("重复动作且没有违章图片是否删除成功:{}", picFile.delete() ? "成功" : "失败");
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
- } else {
|
|||
|
|
- log.info("=======非重复动作记录动作变化======");
|
|||
|
|
- imageLibrary = new TImageLibrary();
|
|||
|
|
- imageLibrary.setClassName(className);
|
|||
|
|
- imageLibrary.setImgUrl(img);
|
|||
|
|
- imageLibrary.setStartTime(DateUtils.getNowDate());
|
|||
|
|
- imageLibrary.setEndTime(DateUtils.getNowDate());
|
|||
|
|
- tImageLibraryService.insert(imageLibrary);
|
|||
|
|
- //查询该动作最新的一条数据
|
|||
|
|
- if (null != preClassName) {
|
|||
|
|
- //修改最近动作结束时间
|
|||
|
|
- TImageLibrary preImage = tImageLibraryService.getByClassName(preClassName);
|
|||
|
|
- if (null != preImage) {
|
|||
|
|
- preImage.setEndTime(DateUtils.getNowDate());
|
|||
|
|
- tImageLibraryService.updateEndTime(preImage);
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
- responseMp.put("className", className);
|
|||
|
|
- log.info("结果集中数据为:{}", JSON.toJSONString(responseMp));
|
|||
|
|
- }
|
|||
|
|
- if (!ok) {
|
|||
|
|
- log.info("请求失败:请检查原因:{}", execute);
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
- strMap.put("height", ff.getImageHeight());//视频高度
|
|||
|
|
- log.info("视频高度{}", ff.getImageHeight());
|
|||
|
|
- strMap.put("width", ff.getImageWidth());//视频宽度
|
|||
|
|
- log.info("视频宽度{}", ff.getImageWidth());
|
|||
|
|
- strMap.put("frame", ff.getFrameRate());//帧率
|
|||
|
|
- log.info("帧率{}", ff.getFrameRate());
|
|||
|
|
- strMap.put("imgPath", strList);//每一帧图像数据
|
|||
|
|
- ff.stop();
|
|||
|
|
- } catch (Exception e) {
|
|||
|
|
- e.printStackTrace();
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
-/**
|
|||
|
|
- * 判断是否违章
|
|||
|
|
- *
|
|||
|
|
- * @param imageDefect
|
|||
|
|
- * @return
|
|||
|
|
- */
|
|||
|
|
-
|
|||
|
|
- private boolean hasIllegalBehavior(List<ImageDefect> imageDefect) {
|
|||
|
|
- boolean illegal = true;
|
|||
|
|
- for (String risk : RISK_CONSTANTS) {
|
|||
|
|
- boolean contains = imageDefect.stream().map(ImageDefect::getClassName).collect(Collectors.toList()).contains(risk);
|
|||
|
|
- if (contains) {
|
|||
|
|
- illegal = false;
|
|||
|
|
- break;
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
- return illegal;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- @Data
|
|||
|
|
- private static class responseData {
|
|||
|
|
- private String resultCode;
|
|||
|
|
- private String resultMsg;
|
|||
|
|
- private List<ImageDefect> imageDefect;
|
|||
|
|
-
|
|||
|
|
- public String getResultCode() {
|
|||
|
|
- return resultCode;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- public void setResultCode(String resultCode) {
|
|||
|
|
- this.resultCode = resultCode;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- public String getResultMsg() {
|
|||
|
|
- return resultMsg;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- public void setResultMsg(String resultMsg) {
|
|||
|
|
- this.resultMsg = resultMsg;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- public List<ImageDefect> getImageDefect() {
|
|||
|
|
- return imageDefect;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- public void setImageDefect(List<ImageDefect> imageDefect) {
|
|||
|
|
- this.imageDefect = imageDefect;
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- @Data
|
|||
|
|
- private static class ImageDefect {
|
|||
|
|
- private String className;
|
|||
|
|
-
|
|||
|
|
- public String getClassName() {
|
|||
|
|
- return className;
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
- public void setClassName(String className) {
|
|||
|
|
- this.className = className;
|
|||
|
|
- }
|
|||
|
|
- }
|
|||
|
|
-
|
|||
|
|
-
|
|||
|
|
-}
|
|||
|
|
-
|
|||
|
|
+//package com.sercurityControl.proteam.util;
|
|||
|
|
+//
|
|||
|
|
+//import cn.hutool.http.HttpResponse;
|
|||
|
|
+//import cn.hutool.http.HttpUtil;
|
|||
|
|
+//import com.alibaba.fastjson2.JSON;
|
|||
|
|
+//import com.securityControl.common.core.utils.DateUtils;
|
|||
|
|
+//import com.securityControl.common.core.utils.file.ImageUtils;
|
|||
|
|
+//import com.sercurityControl.proteam.domain.TImageLibrary;
|
|||
|
|
+//import com.sercurityControl.proteam.service.TImageLibraryService;
|
|||
|
|
+//import lombok.Data;
|
|||
|
|
+//import org.bytedeco.javacv.FFmpegFrameGrabber;
|
|||
|
|
+//import org.bytedeco.javacv.Frame;
|
|||
|
|
+//import org.bytedeco.javacv.Java2DFrameConverter;
|
|||
|
|
+//import org.slf4j.Logger;
|
|||
|
|
+//import org.slf4j.LoggerFactory;
|
|||
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
+//import org.springframework.beans.factory.annotation.Value;
|
|||
|
|
+//import org.springframework.stereotype.Component;
|
|||
|
|
+//
|
|||
|
|
+//import javax.imageio.ImageIO;
|
|||
|
|
+//import java.awt.*;
|
|||
|
|
+//import java.awt.image.BufferedImage;
|
|||
|
|
+//import java.io.*;
|
|||
|
|
+//import java.util.*;
|
|||
|
|
+//import java.util.List;
|
|||
|
|
+//import java.util.stream.Collectors;
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * @author xxx
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+//@Component
|
|||
|
|
+//public class VideoUtil {
|
|||
|
|
+//
|
|||
|
|
+// private static final Logger log = LoggerFactory.getLogger(VideoUtil.class);
|
|||
|
|
+//
|
|||
|
|
+// private static final Map<String, Object> responseMp = new HashMap<>(4);
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 统一视频平台地址
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// @Value("${video.address}")
|
|||
|
|
+// private String VIDEO_ADDRESS;
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 统一视频平台账号
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// @Value("${video.username}")
|
|||
|
|
+// private String VIDEO_USERNAME;
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 统一视频平台密码
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// @Value("${video.password}")
|
|||
|
|
+// private String VIDEO_PASSWORD;
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 图片文件存储在本地的根路径
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// @Value("${file.path}")
|
|||
|
|
+// private String localFilePath;
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 人工智能平台请求接口
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// @Value("${ai.url}")
|
|||
|
|
+// private String url;
|
|||
|
|
+//
|
|||
|
|
+// @Autowired
|
|||
|
|
+// private TImageLibraryService tImageLibraryService;
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 违章类型
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// private static final String[] RISK_CONSTANTS = new String[]{"未佩戴安全帽", "未规范着装", "未佩戴安全带", "警戒区闯入", "吸烟", "作业无监护", "登高行为", "作业现场无人"};
|
|||
|
|
+//
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 登录统一视频平台登录接口
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// private final String loginUrl = "/uvp-backend-common/api/v1/authorization";
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 登录统一视频平台视频流接口
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// private final String videoUrl = "/uvp-backend-common/api/v1/resource/queryDev?ak=%s&token=%s×tamp=1609297100943&nonce=00b215ed4710491e89786a6426b1aa0e";
|
|||
|
|
+//
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 获取统一视频平台视频流
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// private File getVideoStream() {
|
|||
|
|
+// String formatUrl = String.format(videoUrl, VIDEO_USERNAME, getToken());
|
|||
|
|
+// HttpResponse execute = HttpUtil.createPost(VIDEO_ADDRESS + formatUrl).execute();
|
|||
|
|
+// boolean ok = execute.isOk();
|
|||
|
|
+// log.info("请求统一视屏平台获取视频流接口结果是否成功:{}", ok ? "成功" : "失败");
|
|||
|
|
+// if (ok) {
|
|||
|
|
+// String responseBody = execute.body();
|
|||
|
|
+// log.info("统一视屏平台登录返回结果:{}", responseBody);
|
|||
|
|
+// File file = JSON.parseObject(responseBody, File.class);
|
|||
|
|
+// return file;
|
|||
|
|
+//
|
|||
|
|
+// }
|
|||
|
|
+// return null;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 统一视频平台登录获取token
|
|||
|
|
+// *
|
|||
|
|
+// * @return
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// private String getToken() {
|
|||
|
|
+// Map<String, Object> body = new HashMap<>();
|
|||
|
|
+// body.put("ak", VIDEO_USERNAME);
|
|||
|
|
+// body.put("sk", VIDEO_PASSWORD);
|
|||
|
|
+// HttpResponse execute = HttpUtil.createPost(VIDEO_ADDRESS + loginUrl).body(JSON.toJSONString(body)).execute();
|
|||
|
|
+// boolean ok = execute.isOk();
|
|||
|
|
+// log.info("请求统一视屏平台登录接口结果是否成功:{}", ok ? "成功" : "失败");
|
|||
|
|
+// if (ok) {
|
|||
|
|
+// String responseBody = execute.body();
|
|||
|
|
+// log.info("统一视屏平台登录返回结果:{}", responseBody);
|
|||
|
|
+// Map<String, Object> resultMap = JSON.parseObject(responseBody, Map.class);
|
|||
|
|
+// return (String) resultMap.get("resultValue");
|
|||
|
|
+//
|
|||
|
|
+// }
|
|||
|
|
+// return null;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 根据读取到的视频文件,获取视频中的每一帧图片
|
|||
|
|
+// *
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// public void getVideoPic() {
|
|||
|
|
+// //统一视频平台获取视频流
|
|||
|
|
+// File video = getVideoStream();
|
|||
|
|
+// if (null == video) {
|
|||
|
|
+// log.info("未获取到实时视频!!!");
|
|||
|
|
+// return;
|
|||
|
|
+// }
|
|||
|
|
+// //1.根据一个视频文件(视频流),创建一个按照视频中每一帧进行抓取图片的抓取对象
|
|||
|
|
+// FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
|
|||
|
|
+// Map<String, Object> strMap = new HashMap<>();
|
|||
|
|
+// List<String> strList = new ArrayList<>();
|
|||
|
|
+// try {
|
|||
|
|
+// ff.start();
|
|||
|
|
+// //抓每一帧图片
|
|||
|
|
+// //2.先知道这个视频一共有多少帧
|
|||
|
|
+// int length = ff.getLengthInFrames();
|
|||
|
|
+// log.info("视频帧数{}", length);
|
|||
|
|
+// //3.读取视频中每一帧图片
|
|||
|
|
+// Frame frame = null;
|
|||
|
|
+// TImageLibrary imageLibrary;
|
|||
|
|
+// for (int i = 1; i < length; i++) {
|
|||
|
|
+// frame = ff.grabFrame();
|
|||
|
|
+// if (frame.image == null) {
|
|||
|
|
+// continue;
|
|||
|
|
+// }
|
|||
|
|
+// //将获取的帧,存储为图片
|
|||
|
|
+// Java2DFrameConverter converter = new Java2DFrameConverter();//创建一个帧-->图片的转换器
|
|||
|
|
+// BufferedImage image = converter.getBufferedImage(frame);//转换
|
|||
|
|
+// //图片原始高度宽度
|
|||
|
|
+// int height = image.getHeight();
|
|||
|
|
+// int width = image.getWidth();
|
|||
|
|
+// //压缩比例
|
|||
|
|
+// float scale = 0.5f;
|
|||
|
|
+// //压缩后高度和宽度
|
|||
|
|
+// int doWithHeight = (int) (scale * height);
|
|||
|
|
+// int doWithWidth = (int) (scale * width);
|
|||
|
|
+// //图片压缩
|
|||
|
|
+// BufferedImage finalImage = new BufferedImage(doWithWidth, doWithHeight, BufferedImage.TYPE_INT_RGB);
|
|||
|
|
+// finalImage.getGraphics().drawImage(image.getScaledInstance(doWithWidth, doWithHeight, Image.SCALE_SMOOTH), 0, 0, null);
|
|||
|
|
+// //将图片保存到目标文件中
|
|||
|
|
+// String img = localFilePath + i + ".png";
|
|||
|
|
+// File picFile = new File(img);
|
|||
|
|
+// ImageIO.write(finalImage, "png", picFile);
|
|||
|
|
+// //处理发送请求
|
|||
|
|
+// Map<String, Object> body = new HashMap<>();
|
|||
|
|
+// //aqm:安全帽,gz:未着装规范,aqd:未佩戴安全帽,jjqcr:警戒区闯入,xy:吸烟,zywjh:作业无监护
|
|||
|
|
+// String[] arr = new String[]{"aqm", "gz", "aqd", "jjqcr", "xy", "zywjh"};
|
|||
|
|
+// body.put("detect_type", arr);
|
|||
|
|
+// body.put("format", "0");
|
|||
|
|
+// body.put("image", ImageUtils.getImgBase(img));
|
|||
|
|
+// HttpResponse execute = HttpUtil.createPost(url).body(JSON.toJSONString(body)).execute();
|
|||
|
|
+// boolean ok = execute.isOk();
|
|||
|
|
+// log.info("请求结果是否成功:{}", ok ? "成功" : "失败");
|
|||
|
|
+// if (ok) {
|
|||
|
|
+// String responseBody = execute.body();
|
|||
|
|
+// log.info("返回结果:{}", responseBody);
|
|||
|
|
+// responseData responseData = JSON.parseObject(responseBody, responseData.class);
|
|||
|
|
+// List<ImageDefect> imageDefect = responseData.getImageDefect();
|
|||
|
|
+// String className = imageDefect.stream().map(ImageDefect::getClassName).collect(Collectors.joining(","));
|
|||
|
|
+// log.info("className为:{}", className);
|
|||
|
|
+// //是否含有违章行为
|
|||
|
|
+// boolean illegal = hasIllegalBehavior(imageDefect);
|
|||
|
|
+// log.info("动作是否违章:{}", illegal ? "没有违章" : "违章");
|
|||
|
|
+// //判断是否为重复动作&&是否有违章行为
|
|||
|
|
+// String preClassName = (String) responseMp.get("className");
|
|||
|
|
+// if (Objects.equals(preClassName, className)) {
|
|||
|
|
+// log.info("重复动作,不做处理");
|
|||
|
|
+// //动作没变化且没有违章则删除
|
|||
|
|
+// if (illegal) {
|
|||
|
|
+// log.info("");
|
|||
|
|
+// if (picFile.exists()) {
|
|||
|
|
+// log.info("重复动作且没有违章图片是否删除成功:{}", picFile.delete() ? "成功" : "失败");
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+// } else {
|
|||
|
|
+// log.info("=======非重复动作记录动作变化======");
|
|||
|
|
+// imageLibrary = new TImageLibrary();
|
|||
|
|
+// imageLibrary.setClassName(className);
|
|||
|
|
+// imageLibrary.setImgUrl(img);
|
|||
|
|
+// imageLibrary.setStartTime(DateUtils.getNowDate());
|
|||
|
|
+// imageLibrary.setEndTime(DateUtils.getNowDate());
|
|||
|
|
+// tImageLibraryService.insert(imageLibrary);
|
|||
|
|
+// //查询该动作最新的一条数据
|
|||
|
|
+// if (null != preClassName) {
|
|||
|
|
+// //修改最近动作结束时间
|
|||
|
|
+// TImageLibrary preImage = tImageLibraryService.getByClassName(preClassName);
|
|||
|
|
+// if (null != preImage) {
|
|||
|
|
+// preImage.setEndTime(DateUtils.getNowDate());
|
|||
|
|
+// tImageLibraryService.updateEndTime(preImage);
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+// responseMp.put("className", className);
|
|||
|
|
+// log.info("结果集中数据为:{}", JSON.toJSONString(responseMp));
|
|||
|
|
+// }
|
|||
|
|
+// if (!ok) {
|
|||
|
|
+// log.info("请求失败:请检查原因:{}", execute);
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+// strMap.put("height", ff.getImageHeight());//视频高度
|
|||
|
|
+// log.info("视频高度{}", ff.getImageHeight());
|
|||
|
|
+// strMap.put("width", ff.getImageWidth());//视频宽度
|
|||
|
|
+// log.info("视频宽度{}", ff.getImageWidth());
|
|||
|
|
+// strMap.put("frame", ff.getFrameRate());//帧率
|
|||
|
|
+// log.info("帧率{}", ff.getFrameRate());
|
|||
|
|
+// strMap.put("imgPath", strList);//每一帧图像数据
|
|||
|
|
+// ff.stop();
|
|||
|
|
+// } catch (Exception e) {
|
|||
|
|
+// e.printStackTrace();
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+///**
|
|||
|
|
+// * 判断是否违章
|
|||
|
|
+// *
|
|||
|
|
+// * @param imageDefect
|
|||
|
|
+// * @return
|
|||
|
|
+// */
|
|||
|
|
+//
|
|||
|
|
+// private boolean hasIllegalBehavior(List<ImageDefect> imageDefect) {
|
|||
|
|
+// boolean illegal = true;
|
|||
|
|
+// for (String risk : RISK_CONSTANTS) {
|
|||
|
|
+// boolean contains = imageDefect.stream().map(ImageDefect::getClassName).collect(Collectors.toList()).contains(risk);
|
|||
|
|
+// if (contains) {
|
|||
|
|
+// illegal = false;
|
|||
|
|
+// break;
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+// return illegal;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// @Data
|
|||
|
|
+// private static class responseData {
|
|||
|
|
+// private String resultCode;
|
|||
|
|
+// private String resultMsg;
|
|||
|
|
+// private List<ImageDefect> imageDefect;
|
|||
|
|
+//
|
|||
|
|
+// public String getResultCode() {
|
|||
|
|
+// return resultCode;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// public void setResultCode(String resultCode) {
|
|||
|
|
+// this.resultCode = resultCode;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// public String getResultMsg() {
|
|||
|
|
+// return resultMsg;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// public void setResultMsg(String resultMsg) {
|
|||
|
|
+// this.resultMsg = resultMsg;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// public List<ImageDefect> getImageDefect() {
|
|||
|
|
+// return imageDefect;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// public void setImageDefect(List<ImageDefect> imageDefect) {
|
|||
|
|
+// this.imageDefect = imageDefect;
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// @Data
|
|||
|
|
+// private static class ImageDefect {
|
|||
|
|
+// private String className;
|
|||
|
|
+//
|
|||
|
|
+// public String getClassName() {
|
|||
|
|
+// return className;
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+// public void setClassName(String className) {
|
|||
|
|
+// this.className = className;
|
|||
|
|
+// }
|
|||
|
|
+// }
|
|||
|
|
+//
|
|||
|
|
+//
|
|||
|
|
+//}
|
|||
|
|
+//
|