#!/usr/bin/env python3 """ AI监控系统启动脚本 启动RTSP视频流处理服务和静态文件服务 """ import os import sys import time import signal import subprocess import threading from pathlib import Path class AIMonitorLauncher: def __init__(self): self.processes = [] self.base_dir = Path(__file__).parent def check_requirements(self): """检查依赖是否安装""" print("正在检查依赖...") try: import cv2 import yaml import websockets import flask print("✓ 依赖检查通过") return True except ImportError as e: print(f"✗ 缺少依赖: {e}") print("请运行: pip install -r requirements.txt") return False def check_config(self): """检查配置文件""" config_file = self.base_dir / "config.yaml" if not config_file.exists(): print(f"✗ 配置文件不存在: {config_file}") return False print("✓ 配置文件检查通过") return True def start_rtsp_service(self): """启动RTSP服务""" print("正在启动RTSP视频流处理服务...") rtsp_script = self.base_dir / "rtsp_service_ws.py" try: process = subprocess.Popen( [sys.executable, str(rtsp_script)], cwd=str(self.base_dir), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, bufsize=1 ) # 启动线程监控输出 def monitor_output(): for line in iter(process.stdout.readline, ''): if line.strip(): print(f"[RTSP] {line.strip()}") monitor_thread = threading.Thread(target=monitor_output, daemon=True) monitor_thread.start() self.processes.append(("RTSP服务", process)) print(f"✓ RTSP服务已启动 (PID: {process.pid})") return True except Exception as e: print(f"✗ 启动RTSP服务失败: {e}") return False def start_static_server(self): """启动静态文件服务""" print("正在启动静态文件服务...") static_script = self.base_dir / "static_server.py" try: process = subprocess.Popen( [sys.executable, str(static_script)], cwd=str(self.base_dir), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, bufsize=1 ) # 启动线程监控输出 def monitor_output(): for line in iter(process.stdout.readline, ''): if line.strip(): print(f"[HTTP] {line.strip()}") monitor_thread = threading.Thread(target=monitor_output, daemon=True) monitor_thread.start() self.processes.append(("HTTP服务", process)) print(f"✓ HTTP服务已启动 (PID: {process.pid})") return True except Exception as e: print(f"✗ 启动HTTP服务失败: {e}") return False def signal_handler(self, signum, frame): """处理退出信号""" print("\n正在关闭服务...") self.stop_all() sys.exit(0) def stop_all(self): """停止所有服务""" for name, process in self.processes: try: process.terminate() process.wait(timeout=5) print(f"✓ {name}已停止") except subprocess.TimeoutExpired: process.kill() print(f"✗ {name}强制停止") except Exception as e: print(f"✗ 停止{name}失败: {e}") def run(self): """主运行函数""" print("=== AI监控系统启动器 ===") # 注册信号处理 signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) # 检查环境 if not self.check_requirements(): return False if not self.check_config(): return False # 启动服务 services_started = 0 if self.start_rtsp_service(): services_started += 1 time.sleep(2) # 等待RTSP服务完全启动 if self.start_static_server(): services_started += 1 if services_started == 0: print("✗ 没有服务启动成功") return False print(f"\n=== 系统启动完成 ({services_started}/2 服务) ===") print("RTSP WebSocket服务: ws://localhost:8765") print("静态文件服务: http://localhost:5000") print("按 Ctrl+C 停止所有服务") try: # 等待进程 while True: time.sleep(1) # 检查进程状态 for name, process in self.processes: if process.poll() is not None: print(f"✗ {name}异常退出,返回码: {process.returncode}") self.stop_all() return False except KeyboardInterrupt: self.signal_handler(signal.SIGINT, None) return True def main(): launcher = AIMonitorLauncher() launcher.run() if __name__ == "__main__": main()