import logging import os import sys from datetime import datetime from time import sleep import signal import configSer import logSet import stabilize.win import tcp_Ser import upload.DataReporter import videoDetection import videoPush from upload import mqttHelper tcp_service = None video_processor = None reporter = None def signal_handler(sig, frame): global tcp_service,video_processor,reporter print(f"收到退出信号 sig={sig},程序准备退出") tcp_service.stop() tcp_service.join() video_processor.stop() videoPush.stop() reporter.stop() print(f"===释放完毕,退出===") sys.exit(0) def read_config_path() ->str : config_name = "config.json" # determine if application is a script file or frozen exe if getattr(sys, 'frozen', False): application_path = os.path.dirname(sys.executable) elif __file__: application_path = os.path.dirname(__file__) config_path = os.path.join(application_path, config_name) print(f"命令路径:{os.getcwd()}, config_path_abs路径:{config_path}") return config_path signal.signal(signal.SIGINT, signal_handler) # 捕获 Ctrl+C 信号 if __name__ == '__main__': logSet.log_init() if __debug__: print("Debug 模式") else: print("release 模式") # 确保imgs目录存在 imgs_dir = "imgs" if not os.path.exists(imgs_dir): os.makedirs(imgs_dir) # 读取配置文件 config_path_abs=read_config_path() config_obj= configSer.ConfigOperate(config_path_abs) json_str = config_obj.config_info.to_json(indent=4) logging.info(f"当前配置:{json_str}") tcp_service = tcp_Ser.TcpSer("0.0.0.0", config_obj.config_info.server.port) tcp_service.daemon=True tcp_service.start() reporter = upload.DataReporter.DataReporter(data_fps=config_obj.config_info.fps.data,video_fps=config_obj.config_info.fps.video) reporter.register_handler(tcp_service.broadcast_message,"tcp") if config_obj.config_info.upload.enable: mq_config=config_obj.config_info.upload.mqtt upload.DataReporter.mq_client = upload.mqttHelper.MQTTClient(broker=mq_config.broker, port=mq_config.port, topic=mq_config.topic, username=mq_config.username, password=mq_config.password, client_id=mq_config.client_id+"_"+datetime.now().strftime("%y%m%d_%H%M%S"), ) try: upload.DataReporter.start_mqtt_connection_thread() reporter.register_handler(upload.DataReporter.mq_client.publish,"mqtt") except Exception as e: logging.warn("mqtt通讯异常", e) sleep(2) reporter.start() # 启动video videoDetection.configObj=config_obj videoDetection.reporter=reporter video_processor = videoDetection.VideoProcessor() # 添加订阅者processor tcp_service.add_subscribe(video_processor) # 推流 videoPush.video_server_run() #数据平滑窗口 if config_obj.config_info.win.enable: method=config_obj.config_info.win.method size=config_obj.config_info.win.size threshold=config_obj.config_info.win.threshold img_threshold=config_obj.config_info.win.imgThreshold video_processor.adaptive_smoother=stabilize.win.Win.create_window(method,size, threshold,img_threshold) else: video_processor.adaptive_smoother=None # 启动 video_processor.video_mode(config_obj.config_info.capture) logging("==over==")