|
@@ -17,6 +17,7 @@ import time
|
|
|
import argparse
|
|
import argparse
|
|
|
import logging
|
|
import logging
|
|
|
import threading
|
|
import threading
|
|
|
|
|
+import signal
|
|
|
from typing import Optional
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import cv2
|
|
import cv2
|
|
@@ -66,6 +67,18 @@ def setup_logging():
|
|
|
setup_logging()
|
|
setup_logging()
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
+# 全局停止标志(用于信号处理)
|
|
|
|
|
+_shutdown_requested = False
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def _signal_handler(signum, frame):
|
|
|
|
|
+ """信号处理函数"""
|
|
|
|
|
+ global _shutdown_requested
|
|
|
|
|
+ sig_name = signal.Signals(signum).name
|
|
|
|
|
+ logger.info(f"接收到信号 {sig_name},准备优雅退出...")
|
|
|
|
|
+ print(f"\n[信号] 接收到 {sig_name},准备停止...")
|
|
|
|
|
+ _shutdown_requested = True
|
|
|
|
|
+
|
|
|
|
|
|
|
|
class DualCameraSystem:
|
|
class DualCameraSystem:
|
|
|
"""
|
|
"""
|
|
@@ -680,8 +693,14 @@ def main():
|
|
|
|
|
|
|
|
def run_multi_group_mode(args):
|
|
def run_multi_group_mode(args):
|
|
|
"""多组摄像头模式"""
|
|
"""多组摄像头模式"""
|
|
|
|
|
+ global _shutdown_requested
|
|
|
from multi_group_system import MultiGroupSystem
|
|
from multi_group_system import MultiGroupSystem
|
|
|
|
|
|
|
|
|
|
+ # 注册信号处理
|
|
|
|
|
+ signal.signal(signal.SIGINT, _signal_handler)
|
|
|
|
|
+ signal.signal(signal.SIGTERM, _signal_handler)
|
|
|
|
|
+ _shutdown_requested = False
|
|
|
|
|
+
|
|
|
print("\n" + "=" * 60)
|
|
print("\n" + "=" * 60)
|
|
|
print("多组摄像头联动抓拍系统")
|
|
print("多组摄像头联动抓拍系统")
|
|
|
print("=" * 60)
|
|
print("=" * 60)
|
|
@@ -722,12 +741,14 @@ def run_multi_group_mode(args):
|
|
|
print(f"\n多组摄像头系统运行中 ({len(system.groups)} 个组)")
|
|
print(f"\n多组摄像头系统运行中 ({len(system.groups)} 个组)")
|
|
|
print("按 Ctrl+C 停止\n")
|
|
print("按 Ctrl+C 停止\n")
|
|
|
|
|
|
|
|
- # 等待
|
|
|
|
|
- system.wait()
|
|
|
|
|
|
|
+ # 等待(检查停止标志)
|
|
|
|
|
+ while system.running and not _shutdown_requested:
|
|
|
|
|
+ time.sleep(1)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
except KeyboardInterrupt:
|
|
|
print("\n接收到停止信号")
|
|
print("\n接收到停止信号")
|
|
|
finally:
|
|
finally:
|
|
|
|
|
+ print("正在停止系统...")
|
|
|
system.stop()
|
|
system.stop()
|
|
|
|
|
|
|
|
return 0
|
|
return 0
|
|
@@ -735,6 +756,13 @@ def run_multi_group_mode(args):
|
|
|
|
|
|
|
|
def run_single_group_mode(args):
|
|
def run_single_group_mode(args):
|
|
|
"""单组摄像头模式(保持向后兼容)"""
|
|
"""单组摄像头模式(保持向后兼容)"""
|
|
|
|
|
+ global _shutdown_requested
|
|
|
|
|
+
|
|
|
|
|
+ # 注册信号处理
|
|
|
|
|
+ signal.signal(signal.SIGINT, _signal_handler)
|
|
|
|
|
+ signal.signal(signal.SIGTERM, _signal_handler)
|
|
|
|
|
+ _shutdown_requested = False
|
|
|
|
|
+
|
|
|
# 构建配置
|
|
# 构建配置
|
|
|
config = {}
|
|
config = {}
|
|
|
|
|
|
|
@@ -785,7 +813,7 @@ def run_single_group_mode(args):
|
|
|
return 1
|
|
return 1
|
|
|
|
|
|
|
|
print("系统运行中,按Ctrl+C停止")
|
|
print("系统运行中,按Ctrl+C停止")
|
|
|
- while True:
|
|
|
|
|
|
|
+ while not _shutdown_requested:
|
|
|
time.sleep(1)
|
|
time.sleep(1)
|
|
|
results = system.get_results()
|
|
results = system.get_results()
|
|
|
if results:
|
|
if results:
|
|
@@ -797,6 +825,7 @@ def run_single_group_mode(args):
|
|
|
print("\n接收到停止信号")
|
|
print("\n接收到停止信号")
|
|
|
|
|
|
|
|
finally:
|
|
finally:
|
|
|
|
|
+ print("正在停止系统...")
|
|
|
system.cleanup()
|
|
system.cleanup()
|
|
|
|
|
|
|
|
return 0
|
|
return 0
|