From cf7ab4bcdaa9c68f7f47ca47da1f4ffb258abaf8 Mon Sep 17 00:00:00 2001 From: zhouyang Date: Wed, 12 Jul 2023 16:12:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=A7=86=E9=A2=91=E8=BE=93?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xznsh_flow/capture_queue.py | 80 +++++++++++++++++++++++++++++++++++++ xznsh_flow/cfg.json | 1 + xznsh_flow/log.py | 23 +++++++++++ 3 files changed, 104 insertions(+) create mode 100644 xznsh_flow/capture_queue.py create mode 100644 xznsh_flow/cfg.json create mode 100644 xznsh_flow/log.py diff --git a/xznsh_flow/capture_queue.py b/xznsh_flow/capture_queue.py new file mode 100644 index 0000000..a3b452c --- /dev/null +++ b/xznsh_flow/capture_queue.py @@ -0,0 +1,80 @@ +import json +import cv2 +from queue import Queue +from threading import Thread +from log import logger + +with open('cfg.json', 'r') as f: + cfg_dict = json.load(f) + +CAMERA_QUEUE = {camera_name: Queue(1) for camera_name in cfg_dict['camera']} + + +def camera_start(camera_name, camera_source): + """ + 启动摄像头 + Args: + camera_name: + camera_source: + + Returns: + + """ + for line in range(1, 4): + try: + capture_obj = cv2.VideoCapture(camera_source) + result, _ = capture_obj.read() + if not result: + raise Exception + except Exception as e: + logger.error(f'{camera_name} {camera_source}:第{line}次启动失败') + continue + + logger.info(f'{camera_name} {camera_source}:第{line}次启动成功') + return True, capture_obj + return False, None + + +def camera_add_queue(camera_name, camera_source, camera_queue): + """ + 启动摄像头,添加帧到队列 + Args: + camera_name: + camera_source: + camera_queue: + + Returns: + + """ + logger.info(f'{camera_name}线程启动') + start_flag, capture_obj = camera_start(camera_name, camera_source) + + if not start_flag: + return + logger.info(f'开始添加帧到队列') + + while True: + result, frame_picture = capture_obj.read() + if not result: + continue + camera_queue.put_nowait(frame_picture) + + +def camera_mul_thread(): + """ + 摄像头对应拉起线程 + Returns: + + """ + thread_pool = [] + for camera_name in cfg_dict['camera']: + camera_th = Thread(target=camera_add_queue, + args=(camera_name, cfg_dict['camera']['camera_name'], CAMERA_QUEUE[camera_name])) + thread_pool.append(camera_th) + + for camera_thread in thread_pool: + camera_thread.start() + + +if __name__ == '__main__': + camera_mul_thread() diff --git a/xznsh_flow/cfg.json b/xznsh_flow/cfg.json new file mode 100644 index 0000000..afa6a0b --- /dev/null +++ b/xznsh_flow/cfg.json @@ -0,0 +1 @@ +{"log_path": "xznsh.log", "frame": 0.05, "camera": {"camera_01": "rtsp://admin:@192.168.10.18", "camera_02": "rtsp://admin:@192.168.10.12"}} \ No newline at end of file diff --git a/xznsh_flow/log.py b/xznsh_flow/log.py new file mode 100644 index 0000000..4c16366 --- /dev/null +++ b/xznsh_flow/log.py @@ -0,0 +1,23 @@ +import json +import logging +from logging.handlers import RotatingFileHandler + +with open('cfg.json', 'r') as f: + cfg_dict = json.load(f) + +logger = logging.getLogger(__name__) +logger.setLevel(level=logging.DEBUG) + +formatter = '%(asctime)s - %(filename)s - [line]:%(lineno)d - %(levelname)s - %(message)s' + +size_rotate_file = RotatingFileHandler( + filename=cfg_dict['log_path'], maxBytes=10 * 1024 * 1024, backupCount=10) +size_rotate_file.setFormatter(logging.Formatter(formatter)) +size_rotate_file.setLevel(logging.INFO) + +console_handler = logging.StreamHandler() +console_handler.setLevel(level=logging.DEBUG) +console_handler.setFormatter(logging.Formatter(formatter)) + +logger.addHandler(size_rotate_file) +logger.addHandler(console_handler)