From 26562b96c7e2fdbfb081f061f5c0f2eebe7e7788 Mon Sep 17 00:00:00 2001 From: Jasmeet Singh Date: Tue, 27 Feb 2024 00:24:44 +0530 Subject: [PATCH 1/6] Added logger to object detection node and DetectorBase constructor --- .../object_detection/DetectorBase.py | 3 +- .../object_detection/ObjectDetection.py | 39 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/object_detection/object_detection/DetectorBase.py b/object_detection/object_detection/DetectorBase.py index d0b11f5..51ffa0e 100644 --- a/object_detection/object_detection/DetectorBase.py +++ b/object_detection/object_detection/DetectorBase.py @@ -19,7 +19,8 @@ class DetectorBase(ABC): - def __init__(self) -> None: + def __init__(self, logger) -> None: + self.logger = logger self.predictions = [] def create_predictions_list(self, class_ids, confidences, boxes): diff --git a/object_detection/object_detection/ObjectDetection.py b/object_detection/object_detection/ObjectDetection.py index 549954b..5e1848d 100644 --- a/object_detection/object_detection/ObjectDetection.py +++ b/object_detection/object_detection/ObjectDetection.py @@ -35,9 +35,6 @@ def __init__(self): # create an empty list that will hold the names of all available detector self.available_detectors = [] - # fill available_detectors with the detectors from Detectors dir - self.discover_detectors() - self.declare_parameters( namespace='', parameters=[ @@ -64,12 +61,13 @@ def __init__(self): self.confidence_threshold = self.get_parameter('model_params.confidence_threshold').value self.show_fps = self.get_parameter('model_params.show_fps').value - # raise an exception if specified detector was not found - if self.detector_type not in self.available_detectors: - raise ModuleNotFoundError(self.detector_type + " Detector specified in config was not found. " + - "Check the Detectors dir for available detectors.") - else: - self.load_detector() + # Fill available_detectors with the detectors from Detectors directory + self.discover_detectors() + # Load the detector specified through the parameters + self.load_detector() + + # Create a logger instance + self.logger = self.get_logger() self.img_pub = self.create_publisher(Image, self.output_img_topic, 10) self.bb_pub = None @@ -88,15 +86,22 @@ def discover_detectors(self): self.available_detectors.remove('__init__') def load_detector(self): - detector_mod = importlib.import_module(".Detectors." + self.detector_type, - "object_detection") - detector_class = getattr(detector_mod, self.detector_type) - self.detector = detector_class() + # Raise an exception if specified detector was not found + if self.detector_type not in self.available_detectors: + self.logger.error("{} Detector was set in parameters but was not found. Check the " + + "Detectors directory for list of available detectors".format(self.detector_type)) + raise ModuleNotFoundError(self.detector_type + " Detector specified in config was not found. " + + "Check the Detectors dir for available detectors.") + else: + detector_mod = importlib.import_module(".Detectors." + self.detector_type, + "object_detection") + detector_class = getattr(detector_mod, self.detector_type) + self.detector = detector_class(self.logger) - self.detector.build_model(self.model_dir_path, self.weight_file_name) - self.detector.load_classes(self.model_dir_path) + self.detector.build_model(self.model_dir_path, self.weight_file_name) + self.detector.load_classes(self.model_dir_path) - print("Your detector: {} has been loaded !".format(self.detector_type)) + self.logger.info("Your detector: {} has been loaded!".format(self.detector_type)) def detection_cb(self, img_msg): cv_image = self.bridge.imgmsg_to_cv2(img_msg, "bgr8") @@ -104,7 +109,7 @@ def detection_cb(self, img_msg): predictions = self.detector.get_predictions(cv_image=cv_image) if predictions is None: - print("Image input from topic: {} is empty".format(self.input_img_topic)) + self.logger.warning("Image input from topic: {} is empty".format(self.input_img_topic), throttle_duration_sec=1) else: for prediction in predictions: confidence = prediction['confidence'] From 83b7e777a5a986fee2ea7f1e59f11a12bffde749 Mon Sep 17 00:00:00 2001 From: Jasmeet Singh Date: Tue, 27 Feb 2024 00:25:20 +0530 Subject: [PATCH 2/6] Added ROS_LOG_DIR for logging path --- docker_scripts/run_devel.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/docker_scripts/run_devel.sh b/docker_scripts/run_devel.sh index a29ce24..7681887 100755 --- a/docker_scripts/run_devel.sh +++ b/docker_scripts/run_devel.sh @@ -66,6 +66,7 @@ docker run --gpus all --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 -v $PERCEP_WS_PATH/src/:/root/percep_ws/src \ -v $PERCEP_WS_PATH/models/:/root/percep_ws/models/ \ -v ddsconfig.xml:/ddsconfig.xml \ +--env ROS_LOG_DIR=$PERCEP_WS_PATH/logs \ --env CYCLONEDDS_URI=/ddsconfig.xml \ --env="QT_X11_NO_MITSHM=1" \ --env="DISPLAY" \ From d2f65aeef156b5f177c7104d50fbb33932af0afa Mon Sep 17 00:00:00 2001 From: Jasmeet Singh Date: Tue, 27 Feb 2024 00:49:35 +0530 Subject: [PATCH 3/6] Added load_parameters() function and updated logger --- object_detection/config/params.yaml | 7 +- .../object_detection/ObjectDetection.py | 66 +++++++++++-------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/object_detection/config/params.yaml b/object_detection/config/params.yaml index eb69547..d235775 100644 --- a/object_detection/config/params.yaml +++ b/object_detection/config/params.yaml @@ -1,12 +1,11 @@ object_detection: ros__parameters: - input_img_topic: /kitti/camera/color/left/image_raw + input_img_topic: color/image_raw output_bb_topic: object_detection/img_bb output_img_topic: object_detection/img - publish_output_img: 1 model_params: detector_type: YOLOv5 model_dir_path: /root/percep_ws/models/yolov5 weight_file_name: yolov5.onnx - confidence_threshold : 0.5 - show_fps : 1 \ No newline at end of file + confidence_threshold: 0.5 + show_fps: 1 \ No newline at end of file diff --git a/object_detection/object_detection/ObjectDetection.py b/object_detection/object_detection/ObjectDetection.py index 5e1848d..18c65e5 100644 --- a/object_detection/object_detection/ObjectDetection.py +++ b/object_detection/object_detection/ObjectDetection.py @@ -32,49 +32,63 @@ class ObjectDetection(Node): def __init__(self): super().__init__('object_detection') - # create an empty list that will hold the names of all available detector + # Create an empty list that will hold the names of all available detector self.available_detectors = [] + # Create a logger instance + self.logger = super().get_logger() + + # Declare parameters with default values self.declare_parameters( namespace='', parameters=[ - ('input_img_topic', ''), - ('output_bb_topic', ''), - ('output_img_topic', ''), - ('model_params.detector_type', ''), - ('model_params.model_dir_path', ''), - ('model_params.weight_file_name', ''), - ('model_params.confidence_threshold', 0.7), + ('input_img_topic', 'color/image_raw'), + ('output_bb_topic', 'object_detection/img_bb'), + ('output_img_topic', 'object_detection/img'), + ('model_params.detector_type', 'YOLOv5'), + ('model_params.model_dir_path', '/root/percep_ws/models/yolov5'), + ('model_params.weight_file_name', 'yolov5.onnx'), + ('model_params.confidence_threshold', 0.5), ('model_params.show_fps', 1), ] ) - - # node params - self.input_img_topic = self.get_parameter('input_img_topic').value - self.output_bb_topic = self.get_parameter('output_bb_topic').value - self.output_img_topic = self.get_parameter('output_img_topic').value - - # model params - self.detector_type = self.get_parameter('model_params.detector_type').value - self.model_dir_path = self.get_parameter('model_params.model_dir_path').value - self.weight_file_name = self.get_parameter('model_params.weight_file_name').value - self.confidence_threshold = self.get_parameter('model_params.confidence_threshold').value - self.show_fps = self.get_parameter('model_params.show_fps').value + # Load parameters set by user + self.load_parameters() # Fill available_detectors with the detectors from Detectors directory self.discover_detectors() # Load the detector specified through the parameters self.load_detector() - # Create a logger instance - self.logger = self.get_logger() - self.img_pub = self.create_publisher(Image, self.output_img_topic, 10) self.bb_pub = None self.img_sub = self.create_subscription(Image, self.input_img_topic, self.detection_cb, 10) self.bridge = CvBridge() + self.logger.info("[OBJECT DETECTION] Initialized Object Detection Node") + + def load_parameters(self): + # Node params + self.input_img_topic = self.get_parameter('input_img_topic').value + self.output_bb_topic = self.get_parameter('output_bb_topic').value + self.output_img_topic = self.get_parameter('output_img_topic').value + + self.logger.info("[OBJECT DETECTION] Input image topic set to {}".format(self.input_img_topic)) + self.logger.info("[OBJECT DETECTION] Publishig output image on topic {}".format(self.output_img_topic) + + " and bounding box data on topic {}".format(self.output_bb_topic)) + + # Model params + self.detector_type = self.get_parameter('model_params.detector_type').value + self.model_dir_path = self.get_parameter('model_params.model_dir_path').value + self.weight_file_name = self.get_parameter('model_params.weight_file_name').value + self.confidence_threshold = self.get_parameter('model_params.confidence_threshold').value + self.show_fps = self.get_parameter('model_params.show_fps').value + + self.logger.info("[OBJECT DETECTION] Detector type set to {} and".format(self.detector_type) + + " using weigth file from {}".format(self.model_dir_path + self.weight_file_name)) + self.logger.info("[OBJECT DETECTION] Confidence threshold for detection set to: {}".format(self.confidence_threshold)) + def discover_detectors(self): curr_dir = os.path.dirname(__file__) dir_contents = os.listdir(curr_dir + "/Detectors") @@ -88,7 +102,7 @@ def discover_detectors(self): def load_detector(self): # Raise an exception if specified detector was not found if self.detector_type not in self.available_detectors: - self.logger.error("{} Detector was set in parameters but was not found. Check the " + + self.logger.error("[OBJECT DETECTION] {} Detector was set in parameters but was not found. Check the " + "Detectors directory for list of available detectors".format(self.detector_type)) raise ModuleNotFoundError(self.detector_type + " Detector specified in config was not found. " + "Check the Detectors dir for available detectors.") @@ -101,7 +115,7 @@ def load_detector(self): self.detector.build_model(self.model_dir_path, self.weight_file_name) self.detector.load_classes(self.model_dir_path) - self.logger.info("Your detector: {} has been loaded!".format(self.detector_type)) + self.logger.info("[OBJECT DETECTION] Your detector: {} has been loaded!".format(self.detector_type)) def detection_cb(self, img_msg): cv_image = self.bridge.imgmsg_to_cv2(img_msg, "bgr8") @@ -109,7 +123,7 @@ def detection_cb(self, img_msg): predictions = self.detector.get_predictions(cv_image=cv_image) if predictions is None: - self.logger.warning("Image input from topic: {} is empty".format(self.input_img_topic), throttle_duration_sec=1) + self.logger.warning("[OBJECT DETECTION] Image input from topic: {} is empty".format(self.input_img_topic), throttle_duration_sec=1) else: for prediction in predictions: confidence = prediction['confidence'] From a6f96d458b920bae05c798559e31bac026a4ff7a Mon Sep 17 00:00:00 2001 From: deepansh Date: Tue, 27 Feb 2024 22:16:45 +0530 Subject: [PATCH 4/6] added log messages to plugins and fixed linting in ObjectDetection.py fixed spelling error Fixed bugs --- .../object_detection/Detectors/RetinaNet.py | 28 ++++++++--- .../object_detection/Detectors/YOLOv5.py | 27 +++++++--- .../object_detection/Detectors/YOLOv8.py | 49 ++++++++++++------- .../object_detection/ObjectDetection.py | 14 +++--- 4 files changed, 81 insertions(+), 37 deletions(-) diff --git a/object_detection/object_detection/Detectors/RetinaNet.py b/object_detection/object_detection/Detectors/RetinaNet.py index c2bf844..d873085 100755 --- a/object_detection/object_detection/Detectors/RetinaNet.py +++ b/object_detection/object_detection/Detectors/RetinaNet.py @@ -23,30 +23,43 @@ class RetinaNet(DetectorBase): - def __init__(self): - super.__init__() + def __init__(self, logger): + super().__init__(logger) + + # Create a logger instance + self.logger = super().get_logger() def build_model(self, model_dir_path, weight_file_name): model_path = os.path.join(model_dir_path, weight_file_name) try: self.model = models.load_model(model_path, backbone_name='resnet50') + self.logger.info("[RetinaNet] Model successfully loaded from: {}".format(model_path)) except Exception as e: - print("Loading the model failed with exception {}".format(e)) + self.logger.error("[RetinaNet] Loading model failed with exception: {}".format(e)) raise Exception("Error loading given model from path: {}.".format(model_path) + - "Maybe the file doesn't exist?") + " Maybe the file doesn't exist?") def load_classes(self, model_dir_path): self.class_list = [] + fpath = os.path.join(model_dir_path, 'classes.txt') - with open(model_dir_path + "/classes.txt", "r") as f: - self.class_list = [cname.strip() for cname in f.readlines()] + try: + with open(os.path.join(model_dir_path, "classes.txt"), "r") as f: + self.class_list = [cname.strip() for cname in f.readlines()] + self.logger.info("[RetinaNet] Loaded classes from {}".format(fpath)) + except FileNotFoundError: + self.logger.error("[RetinaNet] Classes file not found at path: {}".format(fpath)) + raise FileNotFoundError("Classes file not found. Make sure the file exists at the specified path.") + except Exception as e: + self.logger.error("[RetinaNet] Error loading classes with exception: {}".format(e)) + raise Exception("Error loading classes from file: {}".format(fpath)) return self.class_list def get_predictions(self, cv_image): if cv_image is None: - # TODO: show warning message (different color, maybe) + self.logger.warning("[RetinaNet] Input image is None. No predictions will be generated.") return None else: # copy to draw on @@ -71,5 +84,6 @@ def get_predictions(self, cv_image): boxes = [[int(coord/scale) for coord in box] for box in boxes] super().create_predictions_list(class_ids, confidences, boxes) + self.logger.debug("[RetinaNet] Object detection successfully performed on the input image.") return self.predictions diff --git a/object_detection/object_detection/Detectors/YOLOv5.py b/object_detection/object_detection/Detectors/YOLOv5.py index 4553004..cc5cccb 100755 --- a/object_detection/object_detection/Detectors/YOLOv5.py +++ b/object_detection/object_detection/Detectors/YOLOv5.py @@ -21,31 +21,43 @@ class YOLOv5(DetectorBase): - def __init__(self, conf_threshold=0.7): - super().__init__() + def __init__(self, logger, conf_threshold=0.7): + super().__init__(logger) self.conf_threshold = conf_threshold + # Create a logger instance + self.logger = super().get_logger() + def build_model(self, model_dir_path, weight_file_name): try: model_path = os.path.join(model_dir_path, weight_file_name) self.model = torch.hub.load('ultralytics/yolov5:v6.0', 'custom', path=model_path, force_reload=True) + self.logger.info("[YOLOv5] Model successfully loaded from: {}".format(model_path)) except Exception as e: - print("Loading model failed with exception: {}".format(e)) + self.logger.error("[YOLOv5] Loading model failed with exception: {}".format(e)) raise Exception("Error loading given model from path: {}.".format(model_path) + " Maybe the file doesn't exist?") def load_classes(self, model_dir_path): self.class_list = [] - - with open(os.path.join(model_dir_path, 'classes.txt')) as f: - self.class_list = [cname.strip() for cname in f.readlines()] + fpath = os.path.join(model_dir_path, 'classes.txt') + try: + with open(fpath) as f: + self.class_list = [cname.strip() for cname in f.readlines()] + self.logger.info("[YOLOv5] Loaded classes from {}".format(fpath)) + except FileNotFoundError: + self.logger.error("[YOLOv5] Classes file not found at path: {}".format(fpath)) + raise FileNotFoundError("Classes file not found. Make sure the file exists at the specified path.") + except Exception as e: + self.logger.error("[YOLOv5] Error loading classes with exception: {}".format(e)) + raise Exception("Error loading classes from file: {}".format(fpath)) return self.class_list def get_predictions(self, cv_image): if cv_image is None: - # TODO: show warning message (different color, maybe) + self.logger.warning("[YOLOv5] Input image is None. No predictions will be generated.") return None, None else: self.frame = cv_image @@ -61,5 +73,6 @@ def get_predictions(self, cv_image): boxes.append([int(xy) for xy in xyxy]) super().create_predictions_list(class_id, confidence, boxes) + self.logger.debug("[YOLOv5] Object detection successfully performed on the input image.") return self.predictions diff --git a/object_detection/object_detection/Detectors/YOLOv8.py b/object_detection/object_detection/Detectors/YOLOv8.py index 41ebb9f..d23afda 100755 --- a/object_detection/object_detection/Detectors/YOLOv8.py +++ b/object_detection/object_detection/Detectors/YOLOv8.py @@ -21,30 +21,40 @@ class YOLOv8(DetectorBase): - def __init__(self): + def __init__(self, logger): - super().__init__() + super().__init__(logger) def build_model(self, model_dir_path, weight_file_name): try: model_path = os.path.join(model_dir_path, weight_file_name) self.model = YOLO(model_path) + self.logger.info("[YOLOv8] Model successfully loaded from: {}".format(model_path)) except Exception as e: - print("Loading model failed with exception: {}".format(e)) - raise Exception("Error loading given model from path: {}.".format(model_path) + - " Maybe the file doesn't exist?") + self.logger.error("[YOLOv8] Failed to load model with exception: {}".format(e)) + raise Exception("Error loading the given model from path: {}.".format(model_path) + + " Make sure the file exists and the format is correct.") def load_classes(self, model_dir_path): self.class_list = [] + fpath = os.path.join(model_dir_path, "classes.txt") - with open(model_dir_path + "/classes.txt", "r") as f: - self.class_list = [cname.strip() for cname in f.readlines()] + try: + with open(fpath, "r") as f: + self.class_list = [cname.strip() for cname in f.readlines()] + self.logger.info("[YOLOv8] Loaded classes from {}".format(fpath)) + except FileNotFoundError: + self.logger.error("[YOLOv8] Classes file not found at path: {}".format(fpath)) + raise FileNotFoundError("Classes file not found. Make sure the file exists at the specified path.") + except Exception as e: + self.logger.error("[YOLOv8] Error loading classes with exception: {}".format(e)) + raise Exception("Error loading classes from file: {}".format(fpath)) return self.class_list def get_predictions(self, cv_image): if cv_image is None: - # TODO: show warning message (different color, maybe) + self.logger.warning("[YOLOv8] Input image is None. No predictions will be generated.") return None, None else: self.frame = cv_image @@ -53,14 +63,19 @@ def get_predictions(self, cv_image): boxes = [] # Perform object detection on image - result = self.model.predict(self.frame, verbose=False) # Perform object detection on image - row = result[0].boxes.cpu() - - for box in row: - class_id.append(box.cls.numpy().tolist()[0]) - confidence.append(box.conf.numpy().tolist()[0]) - boxes.append(box.xyxy.numpy().tolist()[0]) - - super().create_predictions_list(class_id, confidence, boxes) + try: + result = self.model.predict(self.frame, verbose=False) + row = result[0].boxes.cpu() + + for box in row: + class_id.append(box.cls.numpy().tolist()[0]) + confidence.append(box.conf.numpy().tolist()[0]) + boxes.append(box.xyxy.numpy().tolist()[0]) + + super().create_predictions_list(class_id, confidence, boxes) + self.logger.debug("[YOLOv8] Object detection successfully performed on the input image.") + except Exception as e: + self.logger.error("[YOLOv8] Object detection failed with exception: {}".format(e)) + raise Exception("Error performing object detection on the input image.") return self.predictions diff --git a/object_detection/object_detection/ObjectDetection.py b/object_detection/object_detection/ObjectDetection.py index 18c65e5..6954ca8 100644 --- a/object_detection/object_detection/ObjectDetection.py +++ b/object_detection/object_detection/ObjectDetection.py @@ -36,7 +36,7 @@ def __init__(self): self.available_detectors = [] # Create a logger instance - self.logger = super().get_logger() + self.logger = super().get_logger() # Declare parameters with default values self.declare_parameters( @@ -86,8 +86,8 @@ def load_parameters(self): self.show_fps = self.get_parameter('model_params.show_fps').value self.logger.info("[OBJECT DETECTION] Detector type set to {} and".format(self.detector_type) + - " using weigth file from {}".format(self.model_dir_path + self.weight_file_name)) - self.logger.info("[OBJECT DETECTION] Confidence threshold for detection set to: {}".format(self.confidence_threshold)) + " using weight file from {}".format(self.model_dir_path + self.weight_file_name)) + self.logger.info("[OBJECT DETECTION] Detection confidence threshold set to: {}".format(self.confidence_threshold)) def discover_detectors(self): curr_dir = os.path.dirname(__file__) @@ -102,8 +102,9 @@ def discover_detectors(self): def load_detector(self): # Raise an exception if specified detector was not found if self.detector_type not in self.available_detectors: - self.logger.error("[OBJECT DETECTION] {} Detector was set in parameters but was not found. Check the " + - "Detectors directory for list of available detectors".format(self.detector_type)) + self.logger.error("[OBJECT DETECTION]" + + " {} Detector was set in parameters but was not found".format(self.detector_type) + + "Check the Detectors directory for list of available detectors") raise ModuleNotFoundError(self.detector_type + " Detector specified in config was not found. " + "Check the Detectors dir for available detectors.") else: @@ -123,7 +124,8 @@ def detection_cb(self, img_msg): predictions = self.detector.get_predictions(cv_image=cv_image) if predictions is None: - self.logger.warning("[OBJECT DETECTION] Image input from topic: {} is empty".format(self.input_img_topic), throttle_duration_sec=1) + self.logger.warning("[OBJECT DETECTION] " + + "Image input from topic: {} is empty".format(self.input_img_topic), throttle_duration_sec=1) else: for prediction in predictions: confidence = prediction['confidence'] From 777c07471a820a9760c01a9f02f6ecfee1c8cc61 Mon Sep 17 00:00:00 2001 From: Jasmeet Singh Date: Mon, 4 Mar 2024 01:53:17 +0530 Subject: [PATCH 5/6] Corrected ROS_LOG_DIR env path --- docker_scripts/run_devel.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docker_scripts/run_devel.sh b/docker_scripts/run_devel.sh index 7681887..758fde4 100755 --- a/docker_scripts/run_devel.sh +++ b/docker_scripts/run_devel.sh @@ -63,10 +63,9 @@ fi docker run --gpus all --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 \ -it --rm --privileged --net=host --ipc=host \ --name $CONTAINER_NAME \ --v $PERCEP_WS_PATH/src/:/root/percep_ws/src \ --v $PERCEP_WS_PATH/models/:/root/percep_ws/models/ \ +-v $PERCEP_WS_PATH/:/root/percep_ws/ \ -v ddsconfig.xml:/ddsconfig.xml \ ---env ROS_LOG_DIR=$PERCEP_WS_PATH/logs \ +--env ROS_LOG_DIR=/root/percep_ws/src/logs \ --env CYCLONEDDS_URI=/ddsconfig.xml \ --env="QT_X11_NO_MITSHM=1" \ --env="DISPLAY" \ From 5cecae87f28f15d15ded528add3df8c9fcbb1f1d Mon Sep 17 00:00:00 2001 From: Jasmeet Singh Date: Mon, 4 Mar 2024 01:57:22 +0530 Subject: [PATCH 6/6] Added separate set_logger function for DetectorBase and Detector Plugin --- object_detection/object_detection/DetectorBase.py | 6 ++++-- object_detection/object_detection/Detectors/RetinaNet.py | 7 ++----- object_detection/object_detection/Detectors/YOLOv5.py | 7 ++----- object_detection/object_detection/Detectors/YOLOv8.py | 4 ++-- object_detection/object_detection/ObjectDetection.py | 8 ++++++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/object_detection/object_detection/DetectorBase.py b/object_detection/object_detection/DetectorBase.py index 51ffa0e..dd70347 100644 --- a/object_detection/object_detection/DetectorBase.py +++ b/object_detection/object_detection/DetectorBase.py @@ -19,10 +19,12 @@ class DetectorBase(ABC): - def __init__(self, logger) -> None: - self.logger = logger + def __init__(self) -> None: self.predictions = [] + def set_logger(self, logger) -> None: + self.logger = logger + def create_predictions_list(self, class_ids, confidences, boxes): self.predictions = [] for i in range(len(class_ids)): diff --git a/object_detection/object_detection/Detectors/RetinaNet.py b/object_detection/object_detection/Detectors/RetinaNet.py index d873085..ffdb7c2 100755 --- a/object_detection/object_detection/Detectors/RetinaNet.py +++ b/object_detection/object_detection/Detectors/RetinaNet.py @@ -23,11 +23,8 @@ class RetinaNet(DetectorBase): - def __init__(self, logger): - super().__init__(logger) - - # Create a logger instance - self.logger = super().get_logger() + def __init__(self): + super().__init__() def build_model(self, model_dir_path, weight_file_name): model_path = os.path.join(model_dir_path, weight_file_name) diff --git a/object_detection/object_detection/Detectors/YOLOv5.py b/object_detection/object_detection/Detectors/YOLOv5.py index cc5cccb..eda8371 100755 --- a/object_detection/object_detection/Detectors/YOLOv5.py +++ b/object_detection/object_detection/Detectors/YOLOv5.py @@ -21,13 +21,10 @@ class YOLOv5(DetectorBase): - def __init__(self, logger, conf_threshold=0.7): - super().__init__(logger) + def __init__(self, conf_threshold=0.7): + super().__init__() self.conf_threshold = conf_threshold - # Create a logger instance - self.logger = super().get_logger() - def build_model(self, model_dir_path, weight_file_name): try: model_path = os.path.join(model_dir_path, weight_file_name) diff --git a/object_detection/object_detection/Detectors/YOLOv8.py b/object_detection/object_detection/Detectors/YOLOv8.py index d23afda..d67a4c1 100755 --- a/object_detection/object_detection/Detectors/YOLOv8.py +++ b/object_detection/object_detection/Detectors/YOLOv8.py @@ -21,9 +21,9 @@ class YOLOv8(DetectorBase): - def __init__(self, logger): + def __init__(self): - super().__init__(logger) + super().__init__() def build_model(self, model_dir_path, weight_file_name): try: diff --git a/object_detection/object_detection/ObjectDetection.py b/object_detection/object_detection/ObjectDetection.py index 6954ca8..060e725 100644 --- a/object_detection/object_detection/ObjectDetection.py +++ b/object_detection/object_detection/ObjectDetection.py @@ -36,7 +36,7 @@ def __init__(self): self.available_detectors = [] # Create a logger instance - self.logger = super().get_logger() + self.logger = self.get_logger() # Declare parameters with default values self.declare_parameters( @@ -111,8 +111,12 @@ def load_detector(self): detector_mod = importlib.import_module(".Detectors." + self.detector_type, "object_detection") detector_class = getattr(detector_mod, self.detector_type) - self.detector = detector_class(self.logger) + self.detector = detector_class() + # Set the logger for the detector plugins + self.detector.set_logger(self.logger) + + # Load the model and the classes file for the detector plugin self.detector.build_model(self.model_dir_path, self.weight_file_name) self.detector.load_classes(self.model_dir_path)