-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_examples.py
More file actions
207 lines (166 loc) · 6.88 KB
/
Copy pathconfig_examples.py
File metadata and controls
207 lines (166 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
参数配置使用示例
演示如何在Lambda函数中使用参数管理器
"""
import os
import sys
from pathlib import Path
# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from src.utils.parameter_manager import get_config_manager, get_config
def example_video_processor():
"""视频处理器中的配置使用示例"""
print("📹 视频处理器配置示例")
print("-" * 40)
# 方法1: 使用全局配置管理器
config_manager = get_config_manager('prod')
video_config = config_manager.get_video_processing_config()
print(f"每段帧数: {video_config['frames_per_segment']}")
print(f"帧间隔: {video_config['frame_interval']}秒")
print(f"分段时长: {video_config['segment_duration']}秒 (自动计算)")
print(f"支持格式: {video_config['supported_formats']}")
# 方法2: 直接获取单个配置
frames_per_segment = get_config('video/frames_per_segment', 30)
frame_interval = get_config('video/frame_interval', 2)
# 在实际处理中使用
segment_duration = frames_per_segment * frame_interval
print(f"\n实际使用:")
print(f"将视频分割为 {segment_duration} 秒的段")
print(f"每段包含 {frames_per_segment} 帧")
print(f"每 {frame_interval} 秒提取一帧")
def example_ai_analyzer():
"""AI分析器中的配置使用示例"""
print("\n🤖 AI分析器配置示例")
print("-" * 40)
config_manager = get_config_manager('prod')
ai_config = config_manager.get_ai_config()
print(f"模型ID: {ai_config['model_id']}")
print(f"最大Token: {ai_config['max_tokens']}")
print(f"温度: {ai_config['temperature']}")
print(f"置信度阈值: {ai_config['confidence_threshold']}")
print(f"最大重试: {ai_config['max_retries']}")
# 模拟AI分析调用
def analyze_frame(frame_data):
"""模拟帧分析"""
# 使用配置中的参数
model_params = {
'model_id': ai_config['model_id'],
'max_tokens': ai_config['max_tokens'],
'temperature': ai_config['temperature'],
'top_p': ai_config['top_p']
}
# 模拟分析结果
confidence = 0.85
hands_on_wheel = confidence > ai_config['confidence_threshold']
return {
'hands_on_wheel': hands_on_wheel,
'confidence': confidence,
'model_params': model_params
}
# 示例调用
result = analyze_frame("frame_data")
print(f"\n分析结果示例:")
print(f"手在方向盘上: {result['hands_on_wheel']}")
print(f"置信度: {result['confidence']}")
def example_lambda_config():
"""Lambda配置使用示例"""
print("\n🔧 Lambda配置示例")
print("-" * 40)
config_manager = get_config_manager('prod')
lambda_config = config_manager.get_lambda_config()
print(f"并发限制: {lambda_config['concurrency_limit']}")
print(f"超时时间: {lambda_config['timeout']}秒")
print(f"视频处理器内存: {lambda_config['video_processor_memory']}MB")
print(f"AI分析器内存: {lambda_config['ai_analyzer_memory']}MB")
# 在CDK中使用这些配置
cdk_config_example = f"""
# 在CDK堆栈中使用配置
lambda_function = lambda_.Function(
self, "VideoProcessor",
memory_size={lambda_config['video_processor_memory']},
timeout=Duration.seconds({lambda_config['timeout']}),
reserved_concurrent_executions={lambda_config['concurrency_limit']},
# ... 其他配置
)
"""
print(f"\nCDK使用示例:")
print(cdk_config_example)
def example_environment_specific():
"""不同环境配置示例"""
print("\n🌍 不同环境配置示例")
print("-" * 40)
environments = ['dev', 'test', 'prod']
for env in environments:
config_manager = get_config_manager(env)
# 获取关键配置
concurrency = config_manager.get_config('lambda/concurrency_limit', 20)
frames_per_segment = config_manager.get_config('video/frames_per_segment', 30)
log_level = config_manager.get_config('monitoring/log_level', 'INFO')
print(f"{env.upper()} 环境:")
print(f" 并发数: {concurrency}")
print(f" 每段帧数: {frames_per_segment}")
print(f" 日志级别: {log_level}")
def example_dynamic_config_update():
"""动态配置更新示例"""
print("\n🔄 动态配置更新示例")
print("-" * 40)
config_manager = get_config_manager('dev')
# 获取当前配置
current_interval = config_manager.get_config('video/frame_interval', 2)
print(f"当前帧间隔: {current_interval}秒")
# 更新配置
new_interval = 3
if config_manager.update_config('video/frame_interval', str(new_interval)):
print(f"✅ 已更新帧间隔为: {new_interval}秒")
# 重新计算分段时长
frames_per_segment = config_manager.get_config('video/frames_per_segment', 30)
new_segment_duration = frames_per_segment * new_interval
print(f"新的分段时长: {new_segment_duration}秒")
else:
print("❌ 配置更新失败")
def example_config_validation():
"""配置验证示例"""
print("\n🔍 配置验证示例")
print("-" * 40)
config_manager = get_config_manager('prod')
validation_result = config_manager.validate_config()
print(f"配置有效: {validation_result['valid']}")
if validation_result['errors']:
print("错误:")
for error in validation_result['errors']:
print(f" ❌ {error}")
if validation_result['warnings']:
print("警告:")
for warning in validation_result['warnings']:
print(f" ⚠️ {warning}")
if validation_result['recommendations']:
print("建议:")
for rec in validation_result['recommendations']:
print(f" 💡 {rec}")
def main():
"""主函数 - 运行所有示例"""
print("🎯 驾驶员手部检测系统 - 参数配置使用示例")
print("=" * 60)
try:
example_video_processor()
example_ai_analyzer()
example_lambda_config()
example_environment_specific()
example_dynamic_config_update()
example_config_validation()
print("\n" + "=" * 60)
print("✅ 所有示例运行完成")
print("\n💡 提示:")
print("1. 在实际Lambda函数中,只需导入 get_config 函数")
print("2. 配置会自动从Parameter Store读取")
print("3. 支持缓存,避免重复读取")
print("4. 可以在AWS控制台直接修改配置")
except Exception as e:
print(f"❌ 示例运行失败: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()