From 1ad8ba81b2fac0beda05b51d47c9a6e128b4481e Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Thu, 28 Nov 2024 21:23:50 -0500 Subject: [PATCH 1/6] 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 ca37a14b..4a0fff13 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_ultralytics @@ -27,8 +28,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 73151979a2be0b27e544f074216c4a8615fc9a9d Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Thu, 28 Nov 2024 21:49:35 -0500 Subject: [PATCH 2/6] formatting attempt fix --- modules/detect_target/detect_target_ultralytics.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index d50c86f7..76767f90 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -7,7 +7,6 @@ import cv2 import ultralytics - from . import base_detect_target from .. import image_and_time from .. import detections_and_time @@ -116,7 +115,6 @@ def run( 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 From c5fc4315ba00f2f891e2b081d51dd2af394d47a9 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Thu, 28 Nov 2024 22:47:43 -0500 Subject: [PATCH 3/6] reviewed changes --- .../detect_target/detect_target_factory.py | 14 +------------ .../detect_target_ultralytics.py | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/modules/detect_target/detect_target_factory.py b/modules/detect_target/detect_target_factory.py index 4a0fff13..b8b41b02 100644 --- a/modules/detect_target/detect_target_factory.py +++ b/modules/detect_target/detect_target_factory.py @@ -30,22 +30,10 @@ 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). """ - # 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: diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index 76767f90..30c4b50b 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -6,6 +6,7 @@ import cv2 import ultralytics +import torch from . import base_detect_target from .. import image_and_time @@ -58,7 +59,11 @@ def run( """ image = data.image start_time = time.time() - + # Fall back to CPU if no GPU is available + if device != "cpu" and not torch.cuda.is_available(): + self.__local_logger.warning("CUDA not available. Falling back to CPU.") + device = "cpu" + predictions = self.__model.predict( source=image, half=self.__enable_half_precision, @@ -111,12 +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 From 100919c678fbb848335f61fe8e21753a2a9e4ab6 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Thu, 28 Nov 2024 22:56:17 -0500 Subject: [PATCH 4/6] formatting fix --- modules/detect_target/detect_target_factory.py | 2 -- modules/detect_target/detect_target_ultralytics.py | 9 ++++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/modules/detect_target/detect_target_factory.py b/modules/detect_target/detect_target_factory.py index b8b41b02..a5610600 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_ultralytics @@ -33,7 +32,6 @@ def create_detect_target( Returns: Tuple containing success status and the instantiated detection object (if successful). """ - 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 30c4b50b..72309e2b 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -3,7 +3,6 @@ """ import time - import cv2 import ultralytics import torch @@ -38,7 +37,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: @@ -60,10 +59,10 @@ def run( image = data.image start_time = time.time() # Fall back to CPU if no GPU is available - if device != "cpu" and not torch.cuda.is_available(): + if self.__device != "cpu" and not torch.cuda.is_available(): self.__local_logger.warning("CUDA not available. Falling back to CPU.") - device = "cpu" - + self.__device = "cpu" + predictions = self.__model.predict( source=image, half=self.__enable_half_precision, From 915d3673451361e25ed0db8cdc8f84970bd4cb3d Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Thu, 28 Nov 2024 23:36:47 -0500 Subject: [PATCH 5/6] formatting fixes again --- modules/detect_target/detect_target_factory.py | 4 +--- modules/detect_target/detect_target_ultralytics.py | 7 ++++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/detect_target/detect_target_factory.py b/modules/detect_target/detect_target_factory.py index a5610600..b1c82412 100644 --- a/modules/detect_target/detect_target_factory.py +++ b/modules/detect_target/detect_target_factory.py @@ -30,9 +30,8 @@ def create_detect_target( Factory function to create a detection target object. Returns: - Tuple containing success status and the instantiated detection object (if successful). + Success, detect target object. """ - match detect_target_option: case DetectTargetOption.ML_ULTRALYTICS: return True, detect_target_ultralytics.DetectTargetUltralytics( @@ -43,5 +42,4 @@ def create_detect_target( show_annotations, save_name, ) - return False, None diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index 72309e2b..42e5ea66 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -3,9 +3,10 @@ """ import time + import cv2 -import ultralytics import torch +import ultralytics from . import base_detect_target from .. import image_and_time @@ -45,6 +46,10 @@ def __init__( self.__filename_prefix = "" if save_name != "": self.__filename_prefix = save_name + "_" + str(int(time.time())) + "_" + # Fall back to CPU if no GPU is available + 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 From 4dc6f9906d9ac0fea895e29793f59c8c19d59999 Mon Sep 17 00:00:00 2001 From: arpanroy18 Date: Fri, 29 Nov 2024 00:15:57 -0500 Subject: [PATCH 6/6] removed ds_store files --- .DS_Store | Bin 8196 -> 0 bytes modules/.DS_Store | Bin 8196 -> 0 bytes .../detect_target/detect_target_ultralytics.py | 5 ----- 3 files changed, 5 deletions(-) delete mode 100644 .DS_Store delete mode 100644 modules/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 6252cb041e472d99eb707649732d283220ce0275..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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 diff --git a/modules/.DS_Store b/modules/.DS_Store deleted file mode 100644 index 1f2a1991ffbc5082cf9952e80f1a5082d71403bd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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? diff --git a/modules/detect_target/detect_target_ultralytics.py b/modules/detect_target/detect_target_ultralytics.py index 42e5ea66..e2db9aae 100644 --- a/modules/detect_target/detect_target_ultralytics.py +++ b/modules/detect_target/detect_target_ultralytics.py @@ -46,7 +46,6 @@ def __init__( self.__filename_prefix = "" if save_name != "": self.__filename_prefix = save_name + "_" + str(int(time.time())) + "_" - # Fall back to CPU if no GPU is available if self.__device != "cpu" and not torch.cuda.is_available(): self.__local_logger.warning("CUDA not available. Falling back to CPU.") self.__device = "cpu" @@ -63,10 +62,6 @@ def run( """ image = data.image start_time = time.time() - # Fall back to CPU if no GPU is available - if self.__device != "cpu" and not torch.cuda.is_available(): - self.__local_logger.warning("CUDA not available. Falling back to CPU.") - self.__device = "cpu" predictions = self.__model.predict( source=image,