fix: 优化边缘端稳定性和日志管理
1. database.py: 优化数据库连接和错误处理 2. postprocessor.py: 改进后处理逻辑 3. result_reporter.py: 完善告警上报字段 4. video_stream.py: 增强视频流稳定性 5. main.py: 优化启动流程和异常处理 6. logger.py: 改进日志格式和轮转配置 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -728,9 +728,18 @@ class PostProcessor:
|
||||
|
||||
if len(batch_outputs) == 1:
|
||||
first_output = batch_outputs[0]
|
||||
if isinstance(first_output, np.ndarray) and first_output.ndim == 3:
|
||||
if first_output.shape[0] == batch_size:
|
||||
if isinstance(first_output, np.ndarray):
|
||||
if first_output.ndim == 3 and first_output.shape[0] == batch_size:
|
||||
# 已经是 (batch, 84, anchors) 格式
|
||||
outputs_array = first_output
|
||||
elif first_output.ndim == 1:
|
||||
# TensorRT 返回扁平 1D 数组,需要 reshape 为 (batch, 84, anchors)
|
||||
per_image = first_output.shape[0] // batch_size
|
||||
num_anchors = per_image // 84
|
||||
outputs_array = first_output.reshape(batch_size, 84, num_anchors)
|
||||
elif first_output.ndim == 2:
|
||||
# (84, anchors) 单张图的输出
|
||||
outputs_array = first_output.reshape(1, first_output.shape[0], first_output.shape[1])
|
||||
else:
|
||||
outputs_array = first_output
|
||||
else:
|
||||
|
||||
@@ -195,25 +195,31 @@ class ResultReporter:
|
||||
是否上报成功
|
||||
"""
|
||||
self._performance_stats["alerts_generated"] += 1
|
||||
|
||||
|
||||
# MQTT 发布和本地存储独立执行,互不影响
|
||||
mqtt_ok = False
|
||||
try:
|
||||
if store_to_db and self._db_manager:
|
||||
self._store_alert(alert, screenshot)
|
||||
|
||||
if self._connected and self._client:
|
||||
self._publish_alert(alert)
|
||||
mqtt_ok = True
|
||||
else:
|
||||
self._logger.warning("MQTT 未连接,消息已加入待发送队列")
|
||||
if self._local_cache:
|
||||
self._local_cache.cache_alert(alert.to_dict())
|
||||
|
||||
self._performance_stats["alerts_sent"] += 1
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
self._performance_stats["send_failures"] += 1
|
||||
self._logger.error(f"上报告警失败: {e}")
|
||||
return False
|
||||
self._logger.error(f"MQTT 发布告警失败: {e}")
|
||||
|
||||
try:
|
||||
if store_to_db and self._db_manager:
|
||||
self._store_alert(alert, screenshot)
|
||||
except Exception as e:
|
||||
self._logger.error(f"本地存储告警失败: {e}")
|
||||
|
||||
if mqtt_ok:
|
||||
self._performance_stats["alerts_sent"] += 1
|
||||
|
||||
return mqtt_ok
|
||||
|
||||
def _store_alert(self, alert: AlertInfo, screenshot: Optional[np.ndarray] = None):
|
||||
"""存储告警到本地数据库(异步)"""
|
||||
|
||||
@@ -409,11 +409,18 @@ class MultiStreamManager:
|
||||
self._streams[camera_id].start()
|
||||
|
||||
def start_all(self):
|
||||
"""启动所有视频流"""
|
||||
"""启动所有视频流(跳过连接失败的流)"""
|
||||
with self._lock:
|
||||
for stream in self._streams.values():
|
||||
stream.start()
|
||||
self._logger.info(f"已启动 {len(self._streams)} 个视频流")
|
||||
failed = []
|
||||
for camera_id, stream in self._streams.items():
|
||||
try:
|
||||
stream.start()
|
||||
except Exception as e:
|
||||
self._logger.warning(f"视频流启动失败,跳过: {camera_id} - {e}")
|
||||
failed.append(camera_id)
|
||||
started = len(self._streams) - len(failed)
|
||||
self._logger.info(f"已启动 {started}/{len(self._streams)} 个视频流"
|
||||
+ (f",{len(failed)} 个失败" if failed else ""))
|
||||
|
||||
def stop_stream(self, camera_id: str):
|
||||
"""停止指定视频流"""
|
||||
|
||||
Reference in New Issue
Block a user