fix: 修复 MQTT 未连接问题

ResultReporter 创建后需调用 initialize() 方法初始化 MQTT 连接

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 15:23:29 +08:00
parent 3065bb948e
commit 6c6c9b4c03

37
main.py
View File

@@ -123,6 +123,7 @@ class EdgeInferenceService:
def _init_reporter(self):
"""初始化结果上报器"""
self._reporter = ResultReporter()
self._reporter.initialize() # 初始化存储和MQTT连接
self._logger.info("结果上报器初始化成功")
def _init_algorithm_manager(self):
@@ -187,6 +188,10 @@ class EdgeInferenceService:
"""处理视频帧 - 批量处理多 ROI"""
try:
roi_configs = self._config_manager.get_roi_configs_with_bindings(camera_id)
# 每100帧打印一次状态
if self._performance_stats["total_frames_processed"] % 100 == 0:
self._logger.info(f"[{camera_id}] 已处理 {self._performance_stats['total_frames_processed']} 帧, ROI数: {len(roi_configs)}")
roi_items = []
for roi in roi_configs:
@@ -252,6 +257,11 @@ class EdgeInferenceService:
conf_threshold=self._settings.inference.conf_threshold
)
# 诊断日志:显示每个 ROI 的检测结果数量
total_detections = sum(len(r[0]) for r in batch_results)
if total_detections > 0:
self._logger.info(f"[推理] batch_size={batch_size}, 总检测数={total_detections}")
for idx, (camera_id, roi, bind, frame, _, scale_info) in enumerate(roi_items):
boxes, scores, class_ids = batch_results[idx]
@@ -319,23 +329,30 @@ class EdgeInferenceService:
if self._algorithm_manager is None:
self._logger.warning("算法管理器不可用,跳过算法处理")
return
roi_id = roi.roi_id
algo_code = bind.algo_code
algo_params = bind.params or {}
# 诊断日志:检测到目标(使用 INFO 级别确保能看到)
if len(boxes) > 0:
self._logger.info(f"[{camera_id}] ROI={roi_id[:8]} 检测到 {len(boxes)} 个目标, algo={algo_code}")
self._algorithm_manager.register_algorithm(
roi_id=roi_id,
bind_id=bind.bind_id,
algorithm_type=algo_code,
params=algo_params
)
tracks = self._build_tracks(roi, boxes, scores, class_ids, scale_info)
if not tracks:
return
# 诊断日志tracks 内容INFO 级别)
self._logger.info(f"[{camera_id}] tracks: {[t.get('class') for t in tracks]}, target_class={bind.target_class}")
alerts = self._algorithm_manager.process(
roi_id=roi_id,
bind_id=bind.bind_id,
@@ -344,7 +361,15 @@ class EdgeInferenceService:
tracks=tracks,
current_time=frame.timestamp
)
# 诊断日志:算法处理结果
if alerts:
self._logger.info(f"[{camera_id}] 算法 {algo_code} 返回 {len(alerts)} 个告警")
else:
# 获取算法状态用于诊断
algo_status = self._algorithm_manager.get_status(roi_id)
self._logger.info(f"[{camera_id}] 算法 {algo_code} 无告警, 状态: {algo_status}")
for alert in alerts:
self._performance_stats["total_alerts_generated"] += 1