-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
353 lines (289 loc) · 11.9 KB
/
Copy pathmain.py
File metadata and controls
353 lines (289 loc) · 11.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
"""
LivePilotAI Day 5 - Main Application Entry Point
Advanced OBS integration and intelligent streaming director system
"""
import tkinter as tk
from tkinter import ttk, messagebox
import sys
import os
import json
import time
import logging
import threading
import asyncio
from pathlib import Path
from typing import Dict, Any, Optional
import uvicorn
from src.api.server import create_app
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('livepilotai_day5.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Add src directory to path for imports
src_path = Path(__file__).parent / 'src'
sys.path.insert(0, str(src_path))
# Global modules will be imported lazily
MainPanel = None
PreviewWindow = None
OBSManager = None
EmotionDetector = None
ConfigManager = None
class SplashScreen:
def __init__(self):
self.root = tk.Tk()
self.root.overrideredirect(True)
# Center the window
width = 400
height = 300
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
x = (screen_width - width) // 2
y = (screen_height - height) // 2
self.root.geometry(f'{width}x{height}+{x}+{y}')
# Style
self.root.configure(bg='#1e1e1e')
# Title
tk.Label(self.root, text="LivePilotAI", font=("Arial", 24, "bold"),
bg='#1e1e1e', fg="#ffffff").pack(pady=(80, 20))
# Subtitle
tk.Label(self.root, text="Day 5 Build", font=("Arial", 12),
bg='#1e1e1e', fg="#aaaaaa").pack(pady=(0, 40))
# Loading Status
self.status_label = tk.Label(self.root, text="Initializing...",
font=("Arial", 10), bg='#1e1e1e', fg="#888888")
self.status_label.pack(side=tk.BOTTOM, pady=20)
# Progress Bar
self.progress = ttk.Progressbar(self.root, length=300, mode='indeterminate')
self.progress.pack(side=tk.BOTTOM, pady=(0, 20))
self.progress.start(10)
def update_status(self, text):
self.status_label.configure(text=text)
self.root.update()
def destroy(self):
self.root.destroy()
def load_modules(splash):
global MainPanel, PreviewWindow, OBSManager, EmotionDetector, ConfigManager
global SystemStatusManager, create_obs_status_panel, create_ai_status_panel
global create_system_status_panel, StatusLevel, show_settings_dialog
global SceneController, EmotionMapper, OBSWebSocketClient
global RealTimeEmotionDetector, CameraManager, FaceDetector, AIDirector
try:
splash.update_status("Loading Core Configuration...")
import src.core as core
ConfigManager = core.ConfigManager
splash.update_status("Loading AI Engine (TensorFlow/MediaPipe)...")
# Pre-import heavy modules
import src.ai_engine.emotion_detector as ed
EmotionDetector = ed.EmotionDetector
import src.ai_engine.modules.real_time_detector as rtd
RealTimeEmotionDetector = rtd.RealTimeEmotionDetector
import src.ai_engine.modules.camera_manager as cam
CameraManager = cam.CameraManager
import src.ai_engine.modules.face_detector as fd
FaceDetector = fd.FaceDetector
import src.ai_engine.modules.ai_director as aid
AIDirector = aid.AIDirector
# Load Voice Commander
import src.ai_engine.modules.voice_commander as vc
global VoiceCommander
VoiceCommander = vc.VoiceCommander
splash.update_status("Loading UI Components...")
import src.ui as ui
MainPanel = ui.main_panel.MainPanel
PreviewWindow = ui.PreviewWindow
show_settings_dialog = ui.show_settings_dialog
SystemStatusManager = ui.SystemStatusManager
create_obs_status_panel = ui.create_obs_status_panel
create_ai_status_panel = ui.create_ai_status_panel
create_system_status_panel = ui.create_system_status_panel
StatusLevel = ui.StatusLevel
splash.update_status("Loading OBS Integration...")
import src.obs_integration.obs_manager as om
OBSManager = om.OBSManager
import src.obs_integration.scene_controller as sc
SceneController = sc.SceneController
import src.obs_integration.emotion_mapper as em
EmotionMapper = em.EmotionMapper
import src.obs_integration.websocket_client as owc
OBSWebSocketClient = owc.OBSWebSocketClient
time.sleep(0.5) # Slight delay to let user see "Done"
except ImportError as e:
logger.error(f"Failed to import required modules: {e}")
messagebox.showerror("Initialization Error", f"Failed to load modules:\n{e}")
sys.exit(1)
class LivePilotAIApp:
"""
Main application class for LivePilotAI Day 5
"""
def __init__(self):
self.root = None
self.main_panel = None
self.preview_window = None
self.status_manager = None
# Core components
self.obs_manager = None
self.scene_controller = None
self.emotion_mapper = None
self.emotion_detector = None
self.camera_manager = None
self.real_time_detector = None
self.ai_director = None
self.voice_commander = None
# Configuration
self.config_file = "livepilotai_config.json"
self.settings = self._load_default_settings()
# Runtime state
self.is_running = False
self.preview_thread = None
self.emotion_thread = None
self.api_server = None
self.api_thread = None
self.logger = logging.getLogger("LivePilotAI")
def _load_default_settings(self) -> Dict[str, Any]:
"""Load default application settings"""
return {
"window_title": "LivePilotAI - Intelligent Streaming Director",
"window_size": "1200x800",
"theme": "modern",
"auto_connect_obs": False
}
def initialize(self, root: tk.Tk):
"""Initialize the application components"""
self.root = root
self.root.title(self.settings["window_title"])
self.root.geometry(self.settings["window_size"])
# Configure grid weight
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
# Setup clean exit
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
try:
self._init_components()
self._setup_ui()
self._start_api_server()
self.logger = logging.getLogger("LivePilotAI")
self.logger.info("Application initialized successfully")
except Exception as e:
logger.exception("Initialization failed")
messagebox.showerror("Error", f"Application initialization failed:\n{str(e)}")
sys.exit(1)
def _init_components(self):
"""Initialize backend components"""
# 1. Config
self.config_manager = ConfigManager()
self.app_config = self.config_manager.load_config()
# 2. OBS Integration
self.obs_manager = OBSManager()
self.scene_controller = SceneController(self.obs_manager)
# Fix: EmotionMapper expects config_path string, not config_manager object
# Since config_loader handles paths internally or we can pass None for defaults
self.emotion_mapper = EmotionMapper()
# 3. AI Engine
# 使用異步初始化包裝
self.emotion_detector = EmotionDetector(
model_path=self.app_config.ai_models.emotion_model_path
)
self.camera_manager = CameraManager()
self.real_time_detector = RealTimeEmotionDetector(
config=None,
emotion_detector=self.emotion_detector,
camera_manager=self.camera_manager
)
self.ai_director = AIDirector(
self.real_time_detector,
self.scene_controller,
self.emotion_mapper
)
self.voice_commander = VoiceCommander(self._handle_voice_command)
def _handle_voice_command(self, text: str):
"""Handle voice commands from global context"""
# Pass to main panel if it exists
if self.main_panel:
self.main_panel.handle_voice_command(text)
def _setup_ui(self):
"""Setup the user interface"""
# Main container
main_container = ttk.Frame(self.root)
main_container.grid(row=0, column=0, sticky="nsew")
main_container.columnconfigure(0, weight=1)
main_container.rowconfigure(0, weight=1)
# Create Main Panel
self.main_panel = MainPanel()
# Inject dependencies into MainPanel
self.main_panel.set_components(
emotion_detector=self.emotion_detector,
camera_manager=self.camera_manager,
obs_manager=self.obs_manager,
emotion_mapper=self.emotion_mapper,
voice_commander=self.voice_commander
)
# Initialize (since we deferred it)
self.main_panel._initialize_components()
# Create Widgets (using setup_ui)
self.main_panel.setup_ui(main_container)
# Setup status indicators if needed
# self.status_manager = SystemStatusManager(self.root)
def _start_api_server(self):
"""Start the FastAPI server in a background thread"""
try:
self.logger.info("Starting API server on port 8000...")
# Use dynamic import to get the latest app instance
from src.api.server import app as fastapi_app
# Attach main app instance to FastAPI state
fastapi_app.state.main_app = self
def run_server():
uvicorn.run(
fastapi_app,
host="0.0.0.0",
port=8000,
log_level="error"
)
self.api_thread = threading.Thread(target=run_server, daemon=True)
self.api_thread.start()
self.logger.info("API server is running in background.")
except Exception as e:
self.logger.error(f"Failed to start API server: {e}")
def run(self):
"""Start the application main loop"""
self.is_running = True
self.root.mainloop()
def on_close(self):
"""Handle application shutdown"""
if messagebox.askokcancel("Quit", "Do you want to quit LivePilotAI?"):
logger.info("Shutting down application...")
self.is_running = False
# Stop AI components
if self.camera_manager:
self.camera_manager.stop()
if self.voice_commander:
self.voice_commander.stop()
# Stop monitoring threads
# (Add thread cleanup code here)
# Disconnect OBS
if self.obs_manager:
self.obs_manager.disconnect()
# API server runs as daemon, it will exit automatically when main thread dies
self.root.destroy()
sys.exit(0)
def main():
"""Application entry point"""
# 1. Show Splash Screen
splash = SplashScreen()
# 2. Load Modules in separate thread/process simulation
# In a real app we might use threading, but for imports we just run them
# while updating the UI. Tkinter is single threaded so we use root.update()
load_modules(splash)
# 3. Destroy Splash and Launch Main App
splash.destroy()
root = tk.Tk()
app = LivePilotAIApp()
app.initialize(root)
app.run()
if __name__ == "__main__":
main()