From 3c3f2fe25b50b9204698217d4c4d365f100ebcb2 Mon Sep 17 00:00:00 2001
From: Joshi <3040996759@qq.com>
Date: Sat, 27 Sep 2025 18:52:40 +0800
Subject: [PATCH] =?UTF-8?q?feat(video):=20=E6=B7=BB=E5=8A=A0=E8=A7=86?=
=?UTF-8?q?=E9=A2=91=E5=88=86=E6=9E=90=E9=85=8D=E7=BD=AE=E5=92=8CRTSP?=
=?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=90=AF=E5=8A=A8=E8=84=9A=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 在application.yml中添加视频分析相关配置项,包括AI检测开关、RTSP连接参数
- 新增detection-config.json模型配置文件,支持YOLO目标检测模型
- 优化RTSP流媒体传输配置,添加TCP/UDP传输协议支持和超时重试机制
-修复模型配置文件路径问题,从/resources/models/调整为/libs/models/
- 更新JavaCV和OpenCV依赖版本,提升视频处理性能
- 添加Windows和Linux平台的RTSP测试服务器启动脚本
- 默认关闭AI检测功能,避免启动时出现错误- 增强RTSP连接稳定性,添加更多兼容性选项
---
bin/start-rtsp-server.bat | 20 +++++++++
bin/start-rtsp-server.sh | 24 +++++++++++
pom.xml | 2 +-
ruoyi-admin/pom.xml | 5 +--
.../src/main/resources/application.yml | 14 +++++++
ruoyi-video/pom.xml | 42 +++++++++----------
.../impl/InspectionTaskServiceImpl.java | 2 +-
.../thread/MediaTransferFlvByJavacv.java | 15 +++++--
.../resources/models/detection-config.json | 33 +++++++++++++++
9 files changed, 128 insertions(+), 29 deletions(-)
create mode 100644 bin/start-rtsp-server.bat
create mode 100644 bin/start-rtsp-server.sh
create mode 100644 ruoyi-video/src/main/resources/models/detection-config.json
diff --git a/bin/start-rtsp-server.bat b/bin/start-rtsp-server.bat
new file mode 100644
index 0000000..977208e
--- /dev/null
+++ b/bin/start-rtsp-server.bat
@@ -0,0 +1,20 @@
+@echo off
+echo 启动RTSP测试服务器...
+echo.
+echo 请确保已安装FFmpeg
+echo.
+
+REM 使用FFmpeg创建一个简单的RTSP服务器
+REM 这里使用测试视频文件,你可以替换为实际的视频文件路径
+set VIDEO_FILE=test.mp4
+
+REM 如果没有测试视频,创建一个测试模式
+if not exist "%VIDEO_FILE%" (
+ echo 创建测试视频流...
+ ffmpeg -f lavfi -i testsrc=duration=3600:size=640x480:rate=25 -f rtsp rtsp://localhost:8554/11
+) else (
+ echo 使用视频文件: %VIDEO_FILE%
+ ffmpeg -re -i "%VIDEO_FILE%" -c copy -f rtsp rtsp://localhost:8554/11
+)
+
+pause
\ No newline at end of file
diff --git a/bin/start-rtsp-server.sh b/bin/start-rtsp-server.sh
new file mode 100644
index 0000000..34d3496
--- /dev/null
+++ b/bin/start-rtsp-server.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+echo "启动RTSP测试服务器..."
+echo
+
+# 检查FFmpeg是否安装
+if ! command -v ffmpeg &> /dev/null; then
+ echo "错误: FFmpeg未安装,请先安装FFmpeg"
+ echo "Ubuntu/Debian: sudo apt install ffmpeg"
+ echo "CentOS/RHEL: sudo yum install ffmpeg"
+ exit 1
+fi
+
+# 视频文件路径
+VIDEO_FILE="test.mp4"
+
+# 如果没有测试视频,创建一个测试模式
+if [ ! -f "$VIDEO_FILE" ]; then
+ echo "创建测试视频流..."
+ ffmpeg -f lavfi -i testsrc=duration=3600:size=640x480:rate=25 -c:v libx264 -preset ultrafast -f rtsp rtsp://localhost:8554/11
+else
+ echo "使用视频文件: $VIDEO_FILE"
+ ffmpeg -re -i "$VIDEO_FILE" -c copy -f rtsp rtsp://localhost:8554/11
+fi
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 532173d..c20c466 100644
--- a/pom.xml
+++ b/pom.xml
@@ -42,7 +42,7 @@
org.bytedeco
javacv-platform
- 1.5.9
+ 1.5.10
diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml
index 37872e7..1450169 100644
--- a/ruoyi-admin/pom.xml
+++ b/ruoyi-admin/pom.xml
@@ -63,9 +63,8 @@
spring-context
- org.bytedeco
- javacv-platform
- 1.5.9
+ org.springframework
+ spring-context
diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml
index e0a5952..daa28a0 100644
--- a/ruoyi-admin/src/main/resources/application.yml
+++ b/ruoyi-admin/src/main/resources/application.yml
@@ -152,3 +152,17 @@ mediasServer:
arcFace:
appId: '替换成你的appId'
sdkKey: '替换成你的sdkKey'
+
+# 视频分析配置
+video:
+ # 是否启用AI检测功能
+ detection:
+ enabled: false
+ # RTSP配置
+ rtsp:
+ # 连接超时时间(微秒)
+ timeout: 10000000
+ # 传输协议 tcp/udp
+ transport: tcp
+ # 重试次数
+ retryCount: 3
diff --git a/ruoyi-video/pom.xml b/ruoyi-video/pom.xml
index 7700158..cce0a47 100644
--- a/ruoyi-video/pom.xml
+++ b/ruoyi-video/pom.xml
@@ -18,11 +18,6 @@
-
- org.bytedeco
- javacv-platform
- 1.5.9
-
com.ruoyi
@@ -35,17 +30,17 @@
1.18.24
-
- org.bytedeco
- javacv
- 1.5.5
-
+
+
+
+
+
-
- org.bytedeco
- ffmpeg-platform
- 4.3.2-1.5.5
-
+
+
+
+
+
cn.hutool
@@ -94,12 +89,12 @@
jackson-databind
2.17.1
-
-
- org.bytedeco
- javacv-platform
- 1.5.10
-
+
+
+
+
+
+
@@ -107,6 +102,11 @@
opencv-platform
4.9.0-1.5.10
+
+ org.bytedeco
+ ffmpeg-platform
+ 6.1.1-1.5.10
+
diff --git a/ruoyi-video/src/main/java/com/ruoyi/video/service/impl/InspectionTaskServiceImpl.java b/ruoyi-video/src/main/java/com/ruoyi/video/service/impl/InspectionTaskServiceImpl.java
index 0a2f639..7f92fdf 100644
--- a/ruoyi-video/src/main/java/com/ruoyi/video/service/impl/InspectionTaskServiceImpl.java
+++ b/ruoyi-video/src/main/java/com/ruoyi/video/service/impl/InspectionTaskServiceImpl.java
@@ -181,7 +181,7 @@ public class InspectionTaskServiceImpl implements InspectionTaskService {
// 初始化模型管理器
if (modelManager == null) {
modelManager = new ModelManager();
- URL json = getClass().getResource("/models/models.json");
+ URL json = getClass().getResource("/libs/models/models.json");
if (json != null) {
modelManager.load(json);
}
diff --git a/ruoyi-video/src/main/java/com/ruoyi/video/thread/MediaTransferFlvByJavacv.java b/ruoyi-video/src/main/java/com/ruoyi/video/thread/MediaTransferFlvByJavacv.java
index e76400a..1dc5c76 100644
--- a/ruoyi-video/src/main/java/com/ruoyi/video/thread/MediaTransferFlvByJavacv.java
+++ b/ruoyi-video/src/main/java/com/ruoyi/video/thread/MediaTransferFlvByJavacv.java
@@ -69,7 +69,7 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
/*** ====== 新增:推理相关字段 ====== ***/
// 开关:是否启用检测(可对外提供 setter)
- private boolean enableDetection = true;
+ private boolean enableDetection = false; // 默认关闭,避免启动错误
// 模型与推理
private ModelManager modelManager;
@@ -98,7 +98,12 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
private void initDetectors() throws Exception {
if (!enableDetection) return;
modelManager = new ModelManager();
- URL json = getClass().getResource("/models/models.json");
+ URL json = getClass().getResource("/libs/models/models.json");
+ if (json == null) {
+ log.warn("模型配置文件未找到,禁用检测功能");
+ enableDetection = false;
+ return;
+ }
modelManager.load(json);
// 单模型: detector = modelManager.get("person-helmet");
@@ -130,10 +135,14 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
grabber.setOption("loglevel", "error"); // 稳定后压低日志
if (cameraDto.getUrl().toLowerCase().startsWith("rtsp://")) {
- grabber.setOption("rtsp_transport", "tcp"); // 你要测 UDP 再改
+ // 尝试TCP传输,如果失败则尝试UDP
+ grabber.setOption("rtsp_transport", "tcp");
grabber.setOption("allowed_media_types", "video");
grabber.setOption("max_delay", "500000");
grabber.setOption("user_agent", "Lavf/60");
+ // 添加更多RTSP选项以提高兼容性
+ grabber.setOption("rtsp_flags", "prefer_tcp");
+ grabber.setOption("timeout", "10000000"); // 10秒超时
} else if (cameraDto.getUrl().toLowerCase().startsWith("rtmp://")) {
grabber.setOption("rtmp_buffer", "1000");
} else if ("desktop".equalsIgnoreCase(cameraDto.getUrl())) {
diff --git a/ruoyi-video/src/main/resources/models/detection-config.json b/ruoyi-video/src/main/resources/models/detection-config.json
new file mode 100644
index 0000000..9ab8e10
--- /dev/null
+++ b/ruoyi-video/src/main/resources/models/detection-config.json
@@ -0,0 +1,33 @@
+{
+ "models": [
+ {
+ "name": "yolo",
+ "type": "object_detection",
+ "modelPath": "models/yolo.onnx",
+ "configPath": "models/yolo.cfg",
+ "weightsPath": "models/yolo.weights",
+ "classNames": [
+ "person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck",
+ "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
+ "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra",
+ "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
+ "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
+ "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
+ "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange",
+ "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa",
+ "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse",
+ "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink",
+ "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier",
+ "toothbrush"
+ ],
+ "inputSize": {
+ "width": 640,
+ "height": 640
+ },
+ "threshold": 0.5,
+ "nmsThreshold": 0.4
+ }
+ ],
+ "enabled": true,
+ "defaultModel": "yolo"
+}
\ No newline at end of file