fix: 只有人数变化才触发大模型分析(节省 API)

This commit is contained in:
2026-04-16 16:39:05 +08:00
parent ea6460d1e2
commit c9e30aae65

View File

@@ -22,6 +22,7 @@ class LocalAnalyzer:
self.prev_frame = None
self.background_model = None
self.human_cascade = None
self.prev_human_count = 0 # 记录前一帧人数
# 初始化人体检测器
self._init_human_detector()
@@ -33,6 +34,7 @@ class LocalAnalyzer:
'human_min_neighbors': 3, # 人体检测最小邻居数
'brightness_change_threshold': 30, # 亮度变化阈值
'trigger_model_threshold': 0.08, # 触发大模型的阈值
'human_count_change_threshold': 1, # 人数变化阈值
}
# 统计
@@ -105,14 +107,37 @@ class LocalAnalyzer:
# 2. 人体检测
human_result = self._detect_human(current_frame)
metrics['human_count'] = human_result['count']
if human_result['count'] > 0:
# 记录人数变化
human_count_change = human_result['count'] - self.prev_human_count
metrics['human_count_change'] = human_count_change
if human_count_change > 0:
events.append({
'event_type': '人物活动',
'description': f'检测到 {human_result["count"]} ',
'confidence': '',
'description': f'检测到 {human_count_change} 人进入,当前共 {human_result["count"]}',
'confidence': '',
'source': 'local'
})
self.human_count += 1
self.human_count += human_count_change
elif human_count_change < 0:
events.append({
'event_type': '人物活动',
'description': f'检测到 {abs(human_count_change)} 人离开,当前剩 {human_result["count"]}',
'confidence': '',
'source': 'local'
})
elif human_result['count'] > 0:
# 人数没变但有人
events.append({
'event_type': '人物活动',
'description': f'检测到 {human_result["count"]} 个人(无变化)',
'confidence': '',
'source': 'local'
})
# 更新前一帧人数(在 should_call_model 中更新)
# self.prev_human_count = human_result['count']
# 3. 亮度检测
brightness_result = self._detect_brightness_change(current_gray, prev_image_path)
@@ -280,21 +305,26 @@ class LocalAnalyzer:
def _should_call_model(self, metrics, events):
"""判断是否需要调用大模型"""
# 条件1运动面积超过阈值
if metrics.get('motion_ratio', 0) > self.config['trigger_model_threshold']:
# 条件1人数变化(最重要)
current_human_count = metrics.get('human_count', 0)
human_count_change = abs(current_human_count - self.prev_human_count)
# 更新前一帧人数
self.prev_human_count = current_human_count
if human_count_change >= self.config['human_count_change_threshold']:
print(f"[LocalAnalyzer] Human count changed: {self.prev_human_count} -> {current_human_count}, triggering model")
return True
# 条件2检测到人
if metrics.get('human_count', 0) > 0:
# 条件2运动面积超过阈值(排除有人但不动的情况)
# 只有在没有人变化时才用这个条件
if metrics.get('motion_ratio', 0) > self.config['trigger_model_threshold'] * 2:
print(f"[LocalAnalyzer] Large motion detected: {metrics.get('motion_ratio', 0):.2%}")
return True
# 条件3亮度大幅变化
# 条件3亮度大幅变化(灯开关等)
if abs(metrics.get('brightness_change', 0)) > self.config['brightness_change_threshold'] * 2:
return True
# 条件4有多个事件类型
event_types = set(e['event_type'] for e in events)
if len(event_types) >= 2:
print(f"[LocalAnalyzer] Brightness changed: {metrics.get('brightness_change', 0)}")
return True
return False
@@ -313,6 +343,7 @@ class LocalAnalyzer:
"""重置状态"""
self.prev_frame = None
self.background_model = None
self.prev_human_count = 0
self.frame_count = 0
self.motion_count = 0
self.human_count = 0