修复问题

This commit is contained in:
2025-09-28 11:51:04 +08:00
parent 937118b2b3
commit a6fb31fdd4

View File

@@ -369,8 +369,11 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
long lastDetectionTime = 0;
List<Detection> currentDetections = Collections.emptyList(); // 当前显示的检测结果
// 避免递归和栈溢出的关键:不使用递归调用,使用循环
// 避免过深的方法调用链
// 检测计数器和性能统计
int totalDetections = 0;
int successfulDetections = 0;
long totalDetectionTime = 0;
for (; running && grabberStatus && recorderStatus; ) {
try {
if (transferFlag) {
@@ -428,10 +431,52 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
if (detectionMat != null && !detectionMat.empty()) {
// 执行检测
try {
// 打印检测开始信息
log.info("===== 开始执行检测 [{}] =====", new java.text.SimpleDateFormat("HH:mm:ss.SSS").format(new java.util.Date()));
totalDetections++;
// 记录检测开始时间
long detectionStartTime = System.currentTimeMillis();
// 执行检测
currentDetections = detector.detect(detectionMat);
// 计算检测耗时
long detectionTime = System.currentTimeMillis() - detectionStartTime;
totalDetectionTime += detectionTime;
successfulDetections++;
// 更新最后检测时间
lastDetectionTime = currentTime;
latestDetections.set(currentDetections);
// 打印检测结果
StringBuilder detectionInfo = new StringBuilder();
detectionInfo.append("\n===== 检测结果 =====\n");
detectionInfo.append("检测耗时: ").append(detectionTime).append("ms\n");
detectionInfo.append("检测目标数: ").append(currentDetections == null ? 0 : currentDetections.size()).append("\n");
if (currentDetections != null && !currentDetections.isEmpty()) {
detectionInfo.append("检测到的目标:\n");
for (int i = 0; i < currentDetections.size(); i++) {
Detection det = currentDetections.get(i);
detectionInfo.append(" ").append(i+1).append(". ");
detectionInfo.append("类型: ").append(det.cls());
detectionInfo.append(", 置信度: ").append(String.format("%.2f", det.conf()));
}
} else {
detectionInfo.append("未检测到目标\n");
}
// 打印检测统计
detectionInfo.append("\n检测统计:\n");
detectionInfo.append("总检测次数: ").append(totalDetections).append("\n");
detectionInfo.append("成功检测次数: ").append(successfulDetections).append("\n");
detectionInfo.append("平均检测时间: ").append(successfulDetections > 0 ? (totalDetectionTime / successfulDetections) : 0).append("ms\n");
detectionInfo.append("===== 检测结束 =====\n");
log.info(detectionInfo.toString());
// 窗口巡检回调
if (windowMode && detectionListener != null &&
currentJobId != null && currentDeviceId != null) {
@@ -443,7 +488,7 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
} catch (Exception ignore) {}
}
} catch (Exception e) {
log.debug("检测异常: {}", e.getMessage());
log.error("检测异常: {}", e.getMessage(), e);
} finally {
// 确保释放Mat
try {
@@ -453,7 +498,7 @@ public class MediaTransferFlvByJavacv extends MediaTransfer implements Runnable
}
}
} catch (Exception e) {
log.debug("检测流程异常: {}", e.getMessage());
log.error("检测流程异常: {}", e.getMessage(), e);
}
}