From e0013e974d13d48adfef6d7efd7669a386951073 Mon Sep 17 00:00:00 2001 From: Siddh Patel <47033295+siddhp1@users.noreply.github.com> Date: Fri, 29 Nov 2024 20:39:54 -0500 Subject: [PATCH] Fix constant ordering --- modules/detect_target/run.py | 72 +++++++++++++++++++++ tests/unit/test_detect_target_brightspot.py | 15 +++-- 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 modules/detect_target/run.py diff --git a/modules/detect_target/run.py b/modules/detect_target/run.py new file mode 100644 index 00000000..c309eede --- /dev/null +++ b/modules/detect_target/run.py @@ -0,0 +1,72 @@ +""" +Run bright spot detection on multiple images. +""" + +import pathlib +import cv2 +from modules.detect_target import detect_target_brightspot +from modules import image_and_time +from modules.common.modules.logger import logger + +# Paths +IMAGE_DIR = pathlib.Path("tests/brightspot_example") +IMAGE_FILES = [ + "ir1.png", + "ir2.png", + "ir3.png", + "ir4.png", + "ir5.png", + "ir6.png", + "ir7.png", +] # List of image files +SAVE_DIR = pathlib.Path("output") + + +def main() -> None: + """ + Run bright spot detection on multiple images. + """ + # Initialize logger + result, local_logger = logger.Logger.create("brightspot_logger", False) + if not result or local_logger is None: + print("Failed to create logger.") + return + + # Ensure the save directory exists + SAVE_DIR.mkdir(parents=True, exist_ok=True) + + for image_file in IMAGE_FILES: + image_path = IMAGE_DIR / image_file + + # Initialize the detector + detector = detect_target_brightspot.DetectTargetBrightspot( + local_logger=local_logger, + show_annotations=True, + save_name=image_file, + ) + + # Load the image + image = cv2.imread(str(image_path)) + if image is None: + print(f"Failed to load image from {image_path}") + continue + + # Create ImageAndTime object + result, image_data = image_and_time.ImageAndTime.create(image) + if not result or image_data is None: + print(f"Failed to create ImageAndTime object for {image_path}") + continue + + # Run the detector + success, detections = detector.run(image_data) + if not success or detections is None: + print(f"Detection failed or returned no detections for {image_path}") + continue + + print(f"Detection successful for {image_path}. Detections:") + for detection in detections.detections: + print(f"Label: {detection.label}, Confidence: {detection.confidence}") + + +if __name__ == "__main__": + main() diff --git a/tests/unit/test_detect_target_brightspot.py b/tests/unit/test_detect_target_brightspot.py index c5c34ba3..a8f1a3c6 100644 --- a/tests/unit/test_detect_target_brightspot.py +++ b/tests/unit/test_detect_target_brightspot.py @@ -14,21 +14,24 @@ from modules.detect_target import detect_target_brightspot -NUMBER_OF_IMAGES_DETECTIONS = 5 -NUMBER_OF_IMAGES_NO_DETECTIONS = 2 TEST_PATH = pathlib.Path("tests", "brightspot_example") + +NUMBER_OF_IMAGES_DETECTIONS = 5 IMAGE_DETECTIONS_FILES = [ pathlib.Path(f"ir_detections_{i}.png") for i in range(0, NUMBER_OF_IMAGES_DETECTIONS) ] -IMAGE_NO_DETECTIONS_FILES = [ - pathlib.Path(f"ir_no_detections_{i}.png") for i in range(0, NUMBER_OF_IMAGES_NO_DETECTIONS) -] EXPECTED_DETECTIONS_PATHS = [ pathlib.Path(TEST_PATH, f"bounding_box_ir_detections_{i}.txt") for i in range(0, NUMBER_OF_IMAGES_DETECTIONS) ] DETECTION_TEST_CASES = list(zip(IMAGE_DETECTIONS_FILES, EXPECTED_DETECTIONS_PATHS)) +NUMBER_OF_IMAGES_NO_DETECTIONS = 2 +IMAGE_NO_DETECTIONS_FILES = [ + pathlib.Path(f"ir_no_detections_{i}.png") for i in range(0, NUMBER_OF_IMAGES_NO_DETECTIONS) +] +NO_DETECTION_TEST_CASES = IMAGE_NO_DETECTIONS_FILES + BOUNDING_BOX_PRECISION_TOLERANCE = 3 CONFIDENCE_PRECISION_TOLERANCE = 6 @@ -143,7 +146,7 @@ def image_ir_detections(request: pytest.FixtureRequest) -> tuple[image_and_time. yield ir_image, detections # type: ignore -@pytest.fixture(params=IMAGE_NO_DETECTIONS_FILES) +@pytest.fixture(params=NO_DETECTION_TEST_CASES) def image_ir_no_detections(request: pytest.FixtureRequest) -> image_and_time.ImageAndTime: # type: ignore """ Load image with no detections.