优化配置参数
This commit is contained in:
parent
ca10456b2b
commit
eb6be2abe5
17
config.yaml
17
config.yaml
@ -34,7 +34,8 @@ camera:
|
||||
width: 1280
|
||||
height: 720
|
||||
fps: 30
|
||||
retry_interval: 2 # 打开摄像头失败后的重试间隔(秒)
|
||||
retry_interval: 2 # 打开摄像头失败后的重试间隔(秒)
|
||||
max_failures: 5 # 触发重新初始化的失败次数阈值
|
||||
|
||||
# 人脸检测配置
|
||||
face_detection:
|
||||
@ -44,17 +45,21 @@ face_detection:
|
||||
face_present_duration: 2.0 # 持续出现时长(秒)才触发识别
|
||||
max_yaw: 20.0 # 最大允许的偏航角(度),超过此角度视为侧脸
|
||||
max_pitch: 20.0 # 最大允许的俯仰角(度),超过此角度视为抬头或低头
|
||||
no_face_threshold: 30 # 人脸消失计数器阈值(帧),连续N帧没有检测到人脸才清空
|
||||
|
||||
# 人脸识别配置
|
||||
face_recognition:
|
||||
# similarity_threshold: 0.85 # 相似度阈值(低于此值视为陌生人)
|
||||
recognition_cooldown: 20.0 # 同一人识别冷却时间(秒)
|
||||
|
||||
|
||||
# 角色映射配置
|
||||
role_mapping:
|
||||
stranger_threshold: 0.98 # 人脸识别阈值
|
||||
# visitor_threshold: 0.70 # 访客识别阈值
|
||||
# 低于visitor_threshold视为陌生人
|
||||
|
||||
# 性能控制配置
|
||||
performance:
|
||||
target_fps: 30 # 目标帧率
|
||||
cleanup_interval: 60 # 清理过期记录的间隔(秒)
|
||||
websocket_loop_delay: 0.01 # WebSocket消息循环延迟(秒)
|
||||
|
||||
# 日志配置
|
||||
logging:
|
||||
@ -62,6 +67,7 @@ logging:
|
||||
dir: "logs" # 日志目录
|
||||
max_bytes: 10485760 # 10MB
|
||||
backup_count: 5
|
||||
keep_days: 7 # 日志保留天数
|
||||
|
||||
|
||||
# 中文字体设置
|
||||
@ -72,6 +78,7 @@ display:
|
||||
stream:
|
||||
# enabled: true # 开关推流功能
|
||||
enabled: false # 开关推流功能
|
||||
max_retries: 5 # 最大重试次数
|
||||
rtmp_url: "rtsp://127.0.0.1/live/video6"
|
||||
|
||||
ffmpeg:
|
||||
|
||||
35
face_rec.py
35
face_rec.py
@ -117,7 +117,7 @@ class FaceRecognitionSystem:
|
||||
self.ffmpeg_process = None
|
||||
self.stream_enabled = self.config.get('stream', {}).get('enabled', False)
|
||||
self.stream_retry_count = 0 # 推流重试计数
|
||||
self.stream_max_retries = 5 # 最大重试次数
|
||||
self.stream_max_retries = self.config.get('stream', {}).get('max_retries', 5) # 最大重试次数
|
||||
self.stream_last_retry_time = None # 上次重试时间
|
||||
self.stream_retry_cooldown = 10 # 重试冷却时间(秒)
|
||||
|
||||
@ -131,8 +131,8 @@ class FaceRecognitionSystem:
|
||||
# 添加摄像头状态跟踪
|
||||
self.camera_failure_count = 0 # 连续失败次数
|
||||
self.camera_last_retry_time = None # 上次重试时间
|
||||
self.camera_retry_cooldown = 3 # 重试冷却时间(秒)
|
||||
self.camera_max_failures = 5 # 触发重新初始化的失败次数阈值
|
||||
self.camera_retry_cooldown = self.config['camera'].get('retry_interval', 2) # 重试冷却时间(秒)
|
||||
self.camera_max_failures = self.config['camera'].get('max_failures', 5) # 触发重新初始化的失败次数阈值
|
||||
|
||||
# 缓存屏幕尺寸,避免每帧重新获取
|
||||
self._cache_screen_size()
|
||||
@ -670,8 +670,10 @@ class FaceRecognitionSystem:
|
||||
|
||||
self.logger.info(f"日志文件: {log_file}")
|
||||
|
||||
def _cleanup_old_logs(self, log_dir: str, keep_days: int = 7):
|
||||
def _cleanup_old_logs(self, log_dir: str, keep_days: int = None):
|
||||
"""清理旧的日志文件(保留最近N天)"""
|
||||
if keep_days is None:
|
||||
keep_days = self.config.get('logging', {}).get('keep_days', 7)
|
||||
try:
|
||||
from datetime import datetime, timedelta
|
||||
cutoff_date = (datetime.now() - timedelta(days=keep_days)).strftime('%Y%m%d')
|
||||
@ -1436,6 +1438,9 @@ class FaceRecognitionSystem:
|
||||
|
||||
async def handle_websocket_messages(self):
|
||||
"""处理WebSocket接收的消息"""
|
||||
# 从配置读取WebSocket循环延迟
|
||||
ws_loop_delay = self.config.get('performance', {}).get('websocket_loop_delay', 0.01)
|
||||
|
||||
try:
|
||||
while self.ws_connected:
|
||||
try:
|
||||
@ -1471,7 +1476,7 @@ class FaceRecognitionSystem:
|
||||
self.ws_connected = False
|
||||
break
|
||||
|
||||
await asyncio.sleep(0.01) # 短暂延迟,避免 CPU 空转
|
||||
await asyncio.sleep(ws_loop_delay) # 短暂延迟,避免 CPU 空转
|
||||
except Exception as e:
|
||||
self.logger.error(f"WebSocket消息处理任务异常: {e}")
|
||||
self.ws_connected = False
|
||||
@ -1569,6 +1574,14 @@ class FaceRecognitionSystem:
|
||||
quality_threshold = self.config['face_detection']['quality_threshold']
|
||||
face_duration = self.config['face_detection']['face_present_duration']
|
||||
|
||||
# 从配置读取性能控制参数
|
||||
perf_config = self.config.get('performance', {})
|
||||
target_fps = perf_config.get('target_fps', 30)
|
||||
cleanup_interval = perf_config.get('cleanup_interval', 60)
|
||||
target_frame_interval_ms = 1000 // target_fps # 每帧间隔毫秒
|
||||
|
||||
self.logger.info(f"目标帧率: {target_fps} FPS")
|
||||
|
||||
self.logger.info("开始处理视频流")
|
||||
|
||||
# 创建显示窗口并设置为全屏
|
||||
@ -1583,9 +1596,7 @@ class FaceRecognitionSystem:
|
||||
stream_check_counter = 0
|
||||
stream_check_interval = 150 # 每150帧(约5秒)检查一次
|
||||
|
||||
# 帧率控制
|
||||
target_fps = 30
|
||||
target_frame_interval_ms = 1000 // target_fps # 每帧间隔毫秒
|
||||
# 帧率控制初始化
|
||||
last_frame_time = time.time() * 1000
|
||||
|
||||
# 清理定时控制
|
||||
@ -1593,18 +1604,18 @@ class FaceRecognitionSystem:
|
||||
|
||||
# 添加人脸消失计数器
|
||||
no_face_counter = 0
|
||||
no_face_threshold = 30 # 连续30帧(约1秒)没有检测到人脸才清空
|
||||
no_face_threshold = self.config['face_detection'].get('no_face_threshold', 30) # 连续N帧没有检测到人脸才清空
|
||||
|
||||
try:
|
||||
while True:
|
||||
current_time = time.time()
|
||||
|
||||
# 每60秒清理一次过期记录,防止内存无限增长
|
||||
if current_time - last_cleanup_time >= 60:
|
||||
# 每cleanup_interval秒清理一次过期记录,防止内存无限增长
|
||||
if current_time - last_cleanup_time >= cleanup_interval:
|
||||
self.cleanup_expired_records()
|
||||
last_cleanup_time = current_time
|
||||
|
||||
# 帧率控制:控制到约30fps
|
||||
# 帧率控制:控制到约target_fps
|
||||
elapsed = time.time() * 1000 - last_frame_time
|
||||
if elapsed < target_frame_interval_ms:
|
||||
await asyncio.sleep((target_frame_interval_ms - elapsed) / 1000)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user