Skip to content

Commit cd9e7cc

Browse files
authored
Added logs to detect target (#217)
* Added logger to detect target script * Added logger to more detect target files * Reverted changed to abstract class * Added logger to detect target worker * Added logger to detect target * Rounded time taken logs to 3 decimal places * Added logger to factory and worker * Linted python * Changed time formatting in logs * updated common * Formatted python * Added logger to detect target script * Added logger to more detect target files * Reverted changed to abstract class * Added logger to detect target worker * Rounded time taken logs to 3 decimal places * Added logger to factory and worker * Linted python * Changed time formatting in logs * updated common * Added logger to detect target * Formatted python * Fixed logger implementation * Fixed model attributes * Fixed logger attribute * Fixed logger implementation (again) * Formatted python * common * common * updated common * Added detect target logger to main_2024 * Removed detect target logger from the worker properties, logger already in local_logger argument * Updated detect target logs * Formatted python * Removed text file logging * Added string repr for detectionInWorld * Added repr for Detections class * Formatted python * Changed python formatting and test logger implementation * formatted python
1 parent fe018af commit cd9e7cc

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

modules/detect_target/detect_target_factory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from . import base_detect_target
88
from . import detect_target_ultralytics
9+
from ..common.modules.logger import logger
910

1011

1112
class DetectTargetOption(enum.Enum):
@@ -21,6 +22,7 @@ def create_detect_target(
2122
device: "str | int",
2223
model_path: str,
2324
override_full: bool,
25+
local_logger: logger.Logger,
2426
show_annotations: bool,
2527
save_name: str,
2628
) -> tuple[bool, base_detect_target.BaseDetectTarget | None]:
@@ -33,6 +35,7 @@ def create_detect_target(
3335
device,
3436
model_path,
3537
override_full,
38+
local_logger,
3639
show_annotations,
3740
save_name,
3841
)

modules/detect_target/detect_target_ultralytics.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from . import base_detect_target
1111
from .. import image_and_time
1212
from .. import detections_and_time
13+
from ..common.modules.logger import logger
1314

1415

1516
class DetectTargetUltralytics(base_detect_target.BaseDetectTarget):
@@ -22,6 +23,7 @@ def __init__(
2223
device: "str | int",
2324
model_path: str,
2425
override_full: bool,
26+
local_logger: logger.Logger,
2527
show_annotations: bool = False,
2628
save_name: str = "",
2729
) -> None:
@@ -36,6 +38,7 @@ def __init__(
3638
self.__model = ultralytics.YOLO(model_path)
3739
self.__counter = 0
3840
self.__enable_half_precision = not self.__device == "cpu"
41+
self.__local_logger = local_logger
3942
self.__show_annotations = show_annotations
4043
if override_full:
4144
self.__enable_half_precision = False
@@ -54,6 +57,8 @@ def run(
5457
Return: Success and the detections.
5558
"""
5659
image = data.image
60+
start_time = time.time()
61+
5762
predictions = self.__model.predict(
5863
source=image,
5964
half=self.__enable_half_precision,
@@ -91,15 +96,15 @@ def run(
9196

9297
detections.append(detection)
9398

99+
end_time = time.time()
100+
self.__local_logger.info(
101+
f"{time.time()}: Count: {self.__counter}. Target detection took {end_time - start_time} seconds. Objects detected: {detections}."
102+
)
103+
94104
# Logging
95105
if self.__filename_prefix != "":
96106
filename = self.__filename_prefix + str(self.__counter)
97107

98-
# Object detections
99-
with open(filename + ".txt", "w", encoding="utf-8") as file:
100-
# Use internal string representation
101-
file.write(repr(detections))
102-
103108
# Annotated image
104109
cv2.imwrite(filename + ".png", image_annotated) # type: ignore
105110

modules/detect_target/detect_target_worker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def detect_target_worker(
4747
device,
4848
model_path,
4949
override_full,
50+
local_logger,
5051
show_annotations,
5152
save_name,
5253
)

modules/detection_in_world.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ def __str__(self) -> str:
5858
To string.
5959
"""
6060
return f"{self.__class__}, vertices: {self.vertices.tolist()}, centre: {self.centre}, label: {self.label}, confidence: {self.confidence}"
61+
62+
def __repr__(self) -> str:
63+
"""
64+
For collections (e.g. list).
65+
"""
66+
return str(self)

modules/detections_and_time.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def __str__(self) -> str:
5757
"""
5858
return f"cls: {self.label}, conf: {self.confidence}, bounds: {self.x_1} {self.y_1} {self.x_2} {self.y_2}"
5959

60+
def __repr__(self) -> str:
61+
"""
62+
For collections (e.g. list).
63+
"""
64+
return str(self)
65+
6066
def get_centre(self) -> "tuple[float, float]":
6167
"""
6268
Gets the xy centre of the bounding box.

tests/unit/test_detect_target_ultralytics.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from modules.detect_target import detect_target_ultralytics
1414
from modules import image_and_time
1515
from modules import detections_and_time
16+
from modules.common.modules.logger import logger
1617

1718

1819
TEST_PATH = pathlib.Path("tests", "model_example")
@@ -108,8 +109,13 @@ def detector() -> detect_target_ultralytics.DetectTargetUltralytics: # type: ig
108109
"""
109110
Construct DetectTargetUltralytics.
110111
"""
112+
result, test_logger = logger.Logger.create("test_logger", False)
113+
114+
assert result
115+
assert test_logger is not None
116+
111117
detection = detect_target_ultralytics.DetectTargetUltralytics(
112-
DEVICE, str(MODEL_PATH), OVERRIDE_FULL
118+
DEVICE, str(MODEL_PATH), OVERRIDE_FULL, test_logger
113119
)
114120
yield detection # type: ignore
115121

0 commit comments

Comments
 (0)