feat(video): 添加视频分析配置和RTSP服务器启动脚本

- 在application.yml中添加视频分析相关配置项,包括AI检测开关、RTSP连接参数
- 新增detection-config.json模型配置文件,支持YOLO目标检测模型
- 优化RTSP流媒体传输配置,添加TCP/UDP传输协议支持和超时重试机制
-修复模型配置文件路径问题,从/resources/models/调整为/libs/models/
- 更新JavaCV和OpenCV依赖版本,提升视频处理性能
- 添加Windows和Linux平台的RTSP测试服务器启动脚本
- 默认关闭AI检测功能,避免启动时出现错误- 增强RTSP连接稳定性,添加更多兼容性选项
This commit is contained in:
2025-09-27 18:52:40 +08:00
parent 68e8afd4ce
commit 3c3f2fe25b
9 changed files with 128 additions and 29 deletions

20
bin/start-rtsp-server.bat Normal file
View File

@@ -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

24
bin/start-rtsp-server.sh Normal file
View File

@@ -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

View File

@@ -42,7 +42,7 @@
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.9</version> <!-- 版本号需与项目兼容 -->
<version>1.5.10</version> <!-- 版本号需与项目兼容 -->
</dependency>
<!-- SpringBoot的依赖配置-->
<dependency>

View File

@@ -63,9 +63,8 @@
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.9</version> <!-- 版本号需与项目兼容 -->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>

View File

@@ -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

View File

@@ -18,11 +18,6 @@
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.9</version> <!-- 版本号需与项目兼容 -->
</dependency>
<!-- 通用工具-->
<dependency>
<groupId>com.ruoyi</groupId>
@@ -35,17 +30,17 @@
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.5</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.bytedeco</groupId>-->
<!-- <artifactId>javacv</artifactId>-->
<!-- <version>1.5.5</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.3.2-1.5.5</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.bytedeco</groupId>-->
<!-- <artifactId>ffmpeg-platform</artifactId>-->
<!-- <version>4.3.2-1.5.5</version>-->
<!-- </dependency>-->
<dependency>
<groupId>cn.hutool</groupId>
@@ -94,12 +89,12 @@
<artifactId>jackson-databind</artifactId>
<version>2.17.1</version>
</dependency>
<!-- JavaCV 封装 -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.10</version>
</dependency>
<!-- &lt;!&ndash; JavaCV 封装 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.bytedeco</groupId>-->
<!-- <artifactId>javacv-platform</artifactId>-->
<!-- <version>1.5.10</version>-->
<!-- </dependency>-->
<!-- OpenCV 平台包(含 DNN 模块) -->
<dependency>
@@ -107,6 +102,11 @@
<artifactId>opencv-platform</artifactId>
<version>4.9.0-1.5.10</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>6.1.1-1.5.10</version>
</dependency>
</dependencies>

View File

@@ -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);
}

View File

@@ -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())) {

View File

@@ -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"
}