From 325a831a7d212b899d53bbf8e92ced926b46d996 Mon Sep 17 00:00:00 2001 From: Xierumeng Date: Sun, 14 Jul 2024 15:19:11 -0700 Subject: [PATCH] Fix incorrect logging directory for perception (#188) --- documentation/main_multiprocess_example.py | 4 +++- main_2024.py | 18 ++++++------------ modules/detect_target/detect_target.py | 2 ++ modules/logger/logger.py | 2 ++ modules/logger/logger_setup_main.py | 16 ++++++++++------ 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/documentation/main_multiprocess_example.py b/documentation/main_multiprocess_example.py index 079b75a8..98fa1dc2 100644 --- a/documentation/main_multiprocess_example.py +++ b/documentation/main_multiprocess_example.py @@ -43,14 +43,16 @@ def main() -> int: print("ERROR: Failed to load configuration file") return -1 + # Get Pylance to stop complaining assert config is not None # Setup main logger - result, main_logger = logger_setup_main.setup_main_logger(config) + result, main_logger, _ = logger_setup_main.setup_main_logger(config) if not result: print("ERROR: Failed to create main logger") return -1 + # Get Pylance to stop complaining assert main_logger is not None # Main is managing all worker processes and is responsible diff --git a/main_2024.py b/main_2024.py index cb36ad54..b34c522a 100644 --- a/main_2024.py +++ b/main_2024.py @@ -3,7 +3,6 @@ """ import argparse -import datetime import inspect import multiprocessing as mp import pathlib @@ -52,15 +51,18 @@ def main() -> int: print("ERROR: Failed to load configuration file") return -1 + # Get Pylance to stop complaining assert config is not None # Setup main logger - result, main_logger = logger_setup_main.setup_main_logger(config) + result, main_logger, logging_path = logger_setup_main.setup_main_logger(config) if not result: print("ERROR: Failed to create main logger") return -1 + # Get Pylance to stop complaining assert main_logger is not None + assert logging_path is not None # Get settings try: @@ -68,25 +70,17 @@ def main() -> int: # pylint: disable=invalid-name QUEUE_MAX_SIZE = config["queue_max_size"] - log_directory_path = config["logger"]["directory_path"] - log_path_format = config["logger"]["file_datetime_format"] - start_time = datetime.datetime.now().strftime(log_path_format) - VIDEO_INPUT_CAMERA_NAME = config["video_input"]["camera_name"] VIDEO_INPUT_WORKER_PERIOD = config["video_input"]["worker_period"] VIDEO_INPUT_SAVE_NAME_PREFIX = config["video_input"]["save_prefix"] - VIDEO_INPUT_SAVE_PREFIX = ( - f"{log_directory_path}/{start_time}/{VIDEO_INPUT_SAVE_NAME_PREFIX}" - ) + VIDEO_INPUT_SAVE_PREFIX = str(pathlib.Path(logging_path, VIDEO_INPUT_SAVE_NAME_PREFIX)) DETECT_TARGET_WORKER_COUNT = config["detect_target"]["worker_count"] DETECT_TARGET_DEVICE = "cpu" if args.cpu else config["detect_target"]["device"] DETECT_TARGET_MODEL_PATH = config["detect_target"]["model_path"] DETECT_TARGET_OVERRIDE_FULL_PRECISION = args.full DETECT_TARGET_SAVE_NAME_PREFIX = config["detect_target"]["save_prefix"] - DETECT_TARGET_SAVE_PREFIX = ( - f"{log_directory_path}/{start_time}/{DETECT_TARGET_SAVE_NAME_PREFIX}" - ) + DETECT_TARGET_SAVE_PREFIX = str(pathlib.Path(logging_path, DETECT_TARGET_SAVE_NAME_PREFIX)) DETECT_TARGET_SHOW_ANNOTATED = args.show_annotated FLIGHT_INTERFACE_ADDRESS = config["flight_interface"]["address"] diff --git a/modules/detect_target/detect_target.py b/modules/detect_target/detect_target.py index 5404a270..e2267dc6 100644 --- a/modules/detect_target/detect_target.py +++ b/modules/detect_target/detect_target.py @@ -85,7 +85,9 @@ def run( confidence = float(boxes.conf[i]) result, detection = detections_and_time.Detection.create(bounds, label, confidence) if result: + # Get Pylance to stop complaining assert detection is not None + detections.append(detection) # Logging diff --git a/modules/logger/logger.py b/modules/logger/logger.py index 6a27e375..9d925cfd 100644 --- a/modules/logger/logger.py +++ b/modules/logger/logger.py @@ -36,6 +36,7 @@ def create(cls, name: str, enable_log_to_file: bool) -> "tuple[bool, Logger | No print("ERROR: Failed to load configuration file") return False, None + # Get Pylance to stop complaining assert config is not None try: @@ -106,6 +107,7 @@ def message_and_metadata(message: str, frame: "types.FrameType | None") -> str: if frame is None: return message + # Get Pylance to stop complaining assert frame is not None function_name = frame.f_code.co_name diff --git a/modules/logger/logger_setup_main.py b/modules/logger/logger_setup_main.py index cbcfd211..b914328c 100644 --- a/modules/logger/logger_setup_main.py +++ b/modules/logger/logger_setup_main.py @@ -12,13 +12,14 @@ MAIN_LOGGER_NAME = "main" -def setup_main_logger(config: "dict") -> "tuple[bool, logger.Logger | None]": +def setup_main_logger(config: "dict") -> "tuple[bool, logger.Logger | None, pathlib.Path | None]": """ Setup prerequisites for logging in `main()` . config: The configuration. - Returns: Success, logger. + TODO: Remove logger path from return + Returns: Success, logger, logger path. """ # Get settings try: @@ -27,21 +28,24 @@ def setup_main_logger(config: "dict") -> "tuple[bool, logger.Logger | None]": start_time = datetime.datetime.now().strftime(log_path_format) except KeyError as exception: print(f"ERROR: Config key(s) not found: {exception}") - return False, None + return False, None, None + + logging_path = pathlib.Path(log_directory_path, start_time) # Create logging directory pathlib.Path(log_directory_path).mkdir(exist_ok=True) - pathlib.Path(log_directory_path, start_time).mkdir() + logging_path.mkdir() # Setup logger result, main_logger = logger.Logger.create(MAIN_LOGGER_NAME, True) if not result: print("ERROR: Failed to create main logger") - return False, None + return False, None, None + # Get Pylance to stop complaining assert main_logger is not None frame = inspect.currentframe() main_logger.info(f"{MAIN_LOGGER_NAME} logger initialized", frame) - return True, main_logger + return True, main_logger, logging_path