From 1c9d0138e95759fb4c9e9baad1ff66167975b263 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Thu, 28 Nov 2024 21:23:50 -0500 Subject: [PATCH 1/2] showing annotated images for detect target message bug fix --- .DS_Store | Bin 0 -> 8196 bytes modules/.DS_Store | Bin 0 -> 8196 bytes .../detect_target/detect_target_factory.py | 20 +++++++++++++++++- .../detect_target_ultralytics.py | 10 ++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 .DS_Store create mode 100644 modules/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6252cb041e472d99eb707649732d283220ce0275 GIT binary patch literal 8196 zcmeHM!EVz)5S?vP;wZEP38Zj9vc$E7G*p!;E@|2xDglQE!2wWk5;rlpc8Ki;R2Ah6 z|G+PB!L|(hCR1S!iU1@hV_Pp8MH#=*OLqwt#_1B21L}Z|{td!B5QusdC zTvWC{RTz-3v`M;;(CW+I!3oW@c@VKdGiL;@8_iXmb+?whO**-Yd# zmNA?}3@4F{ERqaG$kBl_r8Ats2)YowSf+S3Sj}3GRSWkWRV`z z9&OVuJ*777s@mdiM!P^RYz`<;^>{wwQ))hQz3wC!C9D139l1^r*6TmS!eU|RTG1#P zW#e^w=ug_A6-GhJ8o%UU&wStQ&iYpOxnoZ{m0R1s7g~;Ik0rqA+faG+!twh4q~%9m zKakjVe8VUirB0=KeB9XFST)z{jgwXLc(YMQ|KY~TNy)f<@4=(}&O7(e^FK@8NhC)g zEyus-?HBas>p5zVJ=gc(HFt?JS$( z=lR-KgwqTM8D>{xG-rR$8|N9UE37%#>L=pn-}YtX4UpZl(}?zNsD> z;qIDhLHZ{>X60N9xjDs^s(oUc0*huAKvl-h1@( KUk1=0HopPulTyk6 literal 0 HcmV?d00001 diff --git a/modules/.DS_Store b/modules/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1f2a1991ffbc5082cf9952e80f1a5082d71403bd GIT binary patch literal 8196 zcmeHM!EVz)5S?vH>k<`((xP4}S>jriP*A0cO98h2x5rHPOPTbmTrHhuwXGwUn3b z6->WPV0w*YFwA;|H+}@YB+l|ikU@zO{1&GZLxlue>~e}<+ET8u|MiB*m$zO}0OR-$lHSDlV9Zwzmqh;P{* z6aPoh*O?iSlkzo*0}UM-+*j-qiOWf;yO@!8701wx?3S3E)Uf?`&j1Y%vxlg>>KsU( zVT5JzqtDTf9`Ox3$_T%69Q`KuzlFVipM|Ps*C4b+eQ6| u%7ytxhKdLpNykAY9S6Pq!w`KJtSr+k=*Uovp#1xb0Q>$I;B7P42>bySSY4I? literal 0 HcmV?d00001 diff --git a/modules/detect_target/detect_target_factory.py b/modules/detect_target/detect_target_factory.py index ed292db3..dab263cb 100644 --- a/modules/detect_target/detect_target_factory.py +++ b/modules/detect_target/detect_target_factory.py @@ -3,6 +3,7 @@ """ import enum +import torch from . import base_detect_target from . import detect_target_brightspot @@ -29,8 +30,25 @@ def create_detect_target( save_name: str, ) -> tuple[bool, base_detect_target.BaseDetectTarget | None]: """ - Construct detect target class at runtime. + Factory function to create a detection target object. + + Parameters: + detect_target_option: Enumeration value to specify the type of detection. + device: Target device for inference ("cpu" or CUDA device index). + model_path: Path to the model file. + override_full: Force full precision floating point calculations. + local_logger: Logger instance for logging events. + show_annotations: Whether to display annotated images. + save_name: Prefix for saving logs or annotated images. + + Returns: + Tuple containing success status and the instantiated detection object (if successful). """ + # Fall back to CPU if no GPU is available + if device != "cpu" and not torch.cuda.is_available(): + local_logger.warning("CUDA not available. Falling back to CPU.") + device = "cpu" + match detect_target_option: case DetectTargetOption.ML_ULTRALYTICS: return True, detect_target_ultralytics.DetectTargetUltralytics( diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index 4f9bddc1..d50c86f7 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -7,6 +7,7 @@ import cv2 import ultralytics + from . import base_detect_target from .. import image_and_time from .. import detections_and_time @@ -111,6 +112,13 @@ def run( self.__counter += 1 if self.__show_annotations: - cv2.imshow("Annotated", image_annotated) # type: ignore + if image_annotated is None: + self.__local_logger.error("Annotated image is invalid.") + return False, detections + + + # Display the annotated image in a named window + cv2.imshow("Annotated", image_annotated) + cv2.waitKey(1) # Short delay to process GUI events return True, detections From af3bc3a23e422079f54f89037acbf02d07d60461 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Thu, 23 Jan 2025 13:04:18 -0500 Subject: [PATCH 2/2] black linter fixes --- modules/common | 2 +- .../detect_target/detect_target_factory.py | 19 ++-------------- .../detect_target_ultralytics.py | 22 ++++++++++--------- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/modules/common b/modules/common index c7ab98a7..a256a497 160000 --- a/modules/common +++ b/modules/common @@ -1 +1 @@ -Subproject commit c7ab98a75be0f78c2c17084d30c7fce5708897ff +Subproject commit a256a49778d1154e03683c3b5e2fe6cb215d00e7 diff --git a/modules/detect_target/detect_target_factory.py b/modules/detect_target/detect_target_factory.py index dab263cb..c52f4618 100644 --- a/modules/detect_target/detect_target_factory.py +++ b/modules/detect_target/detect_target_factory.py @@ -3,7 +3,6 @@ """ import enum -import torch from . import base_detect_target from . import detect_target_brightspot @@ -32,23 +31,9 @@ def create_detect_target( """ Factory function to create a detection target object. - Parameters: - detect_target_option: Enumeration value to specify the type of detection. - device: Target device for inference ("cpu" or CUDA device index). - model_path: Path to the model file. - override_full: Force full precision floating point calculations. - local_logger: Logger instance for logging events. - show_annotations: Whether to display annotated images. - save_name: Prefix for saving logs or annotated images. - - Returns: - Tuple containing success status and the instantiated detection object (if successful). + Return: + Success, detect target object. """ - # Fall back to CPU if no GPU is available - if device != "cpu" and not torch.cuda.is_available(): - local_logger.warning("CUDA not available. Falling back to CPU.") - device = "cpu" - match detect_target_option: case DetectTargetOption.ML_ULTRALYTICS: return True, detect_target_ultralytics.DetectTargetUltralytics( diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index d50c86f7..61b85dce 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -5,9 +5,9 @@ import time import cv2 +import torch import ultralytics - from . import base_detect_target from .. import image_and_time from .. import detections_and_time @@ -38,7 +38,7 @@ def __init__( self.__device = device self.__model = ultralytics.YOLO(model_path) self.__counter = 0 - self.__enable_half_precision = not self.__device == "cpu" + self.__enable_half_precision = self.__device != "cpu" self.__local_logger = local_logger self.__show_annotations = show_annotations if override_full: @@ -47,6 +47,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]": @@ -112,13 +116,11 @@ def run( self.__counter += 1 if self.__show_annotations: - if image_annotated is None: - self.__local_logger.error("Annotated image is invalid.") - return False, detections - - - # Display the annotated image in a named window - cv2.imshow("Annotated", image_annotated) - cv2.waitKey(1) # Short delay to process GUI events + 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