From 11637a8807579919e912a1b09ba9e3bdcf190642 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Sat, 15 Feb 2025 17:50:24 -0500 Subject: [PATCH 1/4] fixed detect target image not showing --- modules/detect_target/detect_target_factory.py | 5 ++++- modules/detect_target/detect_target_ultralytics.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/modules/detect_target/detect_target_factory.py b/modules/detect_target/detect_target_factory.py index 376456cb..32350984 100644 --- a/modules/detect_target/detect_target_factory.py +++ b/modules/detect_target/detect_target_factory.py @@ -30,7 +30,10 @@ def create_detect_target( local_logger: logger.Logger, ) -> tuple[bool, base_detect_target.BaseDetectTarget | None]: """ - Construct detect target class at runtime. + Factory function to create a detection target object. + + Return: + Success, detect target object. """ match detect_target_option: case DetectTargetOption.ML_ULTRALYTICS: diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index d26e762c..a9b1ee30 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -5,6 +5,7 @@ import time import cv2 +import torch import ultralytics from . import base_detect_target @@ -56,7 +57,7 @@ def __init__( save_name: filename prefix for logging detections and annotated images. """ self.__device = config.device - self.__enable_half_precision = not self.__device == "cpu" + self.__enable_half_precision = self.__device != "cpu" self.__model = ultralytics.YOLO(config.model_path) if config.override_full: self.__enable_half_precision = False @@ -67,6 +68,10 @@ def __init__( if save_name != "": self.__filename_prefix = save_name + "_" + str(int(time.time())) + "_" + if self.__device != "cpu" and not torch.cuda.is_available(): + self.__local_logger.warning("CUDA not available. Falling back to CPU.") + self.__device = "cpu" + def run( self, data: image_and_time.ImageAndTime ) -> "tuple[bool, detections_and_time.DetectionsAndTime | None]": @@ -132,6 +137,11 @@ def run( self.__counter += 1 if self.__show_annotations: - cv2.imshow("Annotated", image_annotated) # type: ignore + if image_annotated is not None: + # Display the annotated image in a named window + cv2.imshow("Annotated", image_annotated) + cv2.waitKey(1) # Short delay to process GUI events + else: + self.__local_logger.warning("Annotated image is invalid.") return True, detections From 276960f717050858a6e89efe0ff6e5d62e4b2cf4 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Fri, 7 Mar 2025 20:22:57 -0500 Subject: [PATCH 2/4] make string class constant and fixed formatting --- modules/detect_target/detect_target_ultralytics.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index a9b1ee30..4a4a2ae1 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -18,6 +18,7 @@ class DetectTargetUltralyticsConfig: """ Configuration for DetectTargetUltralytics. """ + CPU_DEVICE = "cpu" def __init__( self, @@ -57,7 +58,10 @@ def __init__( save_name: filename prefix for logging detections and annotated images. """ self.__device = config.device - self.__enable_half_precision = self.__device != "cpu" + if self.__device != DetectTargetUltralyticsConfig.CPU_DEVICE and not torch.cuda.is_available(): + self.__local_logger.warning("CUDA not available. Falling back to CPU.") + self.__device = DetectTargetUltralyticsConfig.CPU_DEVICE + self.__enable_half_precision = self.__device != DetectTargetUltralyticsConfig.CPU_DEVICE self.__model = ultralytics.YOLO(config.model_path) if config.override_full: self.__enable_half_precision = False @@ -68,9 +72,7 @@ def __init__( if save_name != "": self.__filename_prefix = save_name + "_" + str(int(time.time())) + "_" - if self.__device != "cpu" and not torch.cuda.is_available(): - self.__local_logger.warning("CUDA not available. Falling back to CPU.") - self.__device = "cpu" + def run( self, data: image_and_time.ImageAndTime @@ -132,7 +134,7 @@ def run( filename = self.__filename_prefix + str(self.__counter) # Annotated image - cv2.imwrite(filename + ".png", image_annotated) # type: ignore + cv2.imwrite(filename + ".png", image_annotated) self.__counter += 1 From bdefa2fd83ab41f6eb1c2639a9eba217d172b716 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Fri, 7 Mar 2025 20:41:57 -0500 Subject: [PATCH 3/4] fixed formaating and added string class constant --- modules/detect_target/detect_target_ultralytics.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index 4a4a2ae1..c3a3d835 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -18,6 +18,7 @@ class DetectTargetUltralyticsConfig: """ Configuration for DetectTargetUltralytics. """ + CPU_DEVICE = "cpu" def __init__( @@ -58,7 +59,10 @@ def __init__( save_name: filename prefix for logging detections and annotated images. """ self.__device = config.device - if self.__device != DetectTargetUltralyticsConfig.CPU_DEVICE and not torch.cuda.is_available(): + if ( + self.__device != DetectTargetUltralyticsConfig.CPU_DEVICE + and not torch.cuda.is_available() + ): self.__local_logger.warning("CUDA not available. Falling back to CPU.") self.__device = DetectTargetUltralyticsConfig.CPU_DEVICE self.__enable_half_precision = self.__device != DetectTargetUltralyticsConfig.CPU_DEVICE @@ -72,8 +76,6 @@ def __init__( if save_name != "": self.__filename_prefix = save_name + "_" + str(int(time.time())) + "_" - - def run( self, data: image_and_time.ImageAndTime ) -> "tuple[bool, detections_and_time.DetectionsAndTime | None]": From 58ef535fcb4518288724e681bdadfd1eb2f81070 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Fri, 7 Mar 2025 20:50:52 -0500 Subject: [PATCH 4/4] fixed formatting --- modules/detect_target/detect_target_ultralytics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index c3a3d835..0df1351b 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -58,6 +58,7 @@ def __init__( show_annotations: Display annotated images. save_name: filename prefix for logging detections and annotated images. """ + self.__local_logger = local_logger self.__device = config.device if ( self.__device != DetectTargetUltralyticsConfig.CPU_DEVICE @@ -70,7 +71,6 @@ def __init__( if config.override_full: self.__enable_half_precision = False self.__counter = 0 - self.__local_logger = local_logger self.__show_annotations = show_annotations self.__filename_prefix = "" if save_name != "":