Skip to content
This repository was archived by the owner on Nov 13, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added modules/.DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion modules/detect_target/detect_target_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ 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.

Returns:
Tuple containing success status and the instantiated detection object (if successful).
"""

match detect_target_option:
case DetectTargetOption.ML_ULTRALYTICS:
return True, detect_target_ultralytics.DetectTargetUltralytics(
Expand Down
15 changes: 12 additions & 3 deletions modules/detect_target/detect_target_ultralytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"""

import time

import cv2
import ultralytics
import torch

from . import base_detect_target
from .. import image_and_time
Expand Down Expand Up @@ -37,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:
Expand All @@ -58,6 +58,10 @@ 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,
Expand Down Expand Up @@ -111,6 +115,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