From b2ba957b8bba72fb3ed9668b9e2c0d4c8ff53768 Mon Sep 17 00:00:00 2001 From: Zhongqi Miao Date: Mon, 15 Apr 2024 14:04:56 -0700 Subject: [PATCH 1/3] Add sink overwrite argument addressing https://github.com/microsoft/CameraTraps/issues/475 --- PW_FT_classification/src/utils/utils.py | 6 ++++-- PytorchWildlife/utils/post_process.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/PW_FT_classification/src/utils/utils.py b/PW_FT_classification/src/utils/utils.py index dac065574..4044228c6 100644 --- a/PW_FT_classification/src/utils/utils.py +++ b/PW_FT_classification/src/utils/utils.py @@ -5,7 +5,7 @@ from PIL import Image import numpy as np -def save_crop_images(results, output_dir, original_csv_path): +def save_crop_images(results, output_dir, original_csv_path, overwrite=False): """ Save cropped images based on the detection bounding boxes. @@ -16,6 +16,8 @@ def save_crop_images(results, output_dir, original_csv_path): Directory to save the cropped images. original_csv_path (str): Path to the original CSV file. + overwrite (bool): + Whether overwriting existing image folders. Default to False. Return: new_csv_path (str): Path to the new CSV file. @@ -29,7 +31,7 @@ def save_crop_images(results, output_dir, original_csv_path): new_records = [] os.makedirs(output_dir, exist_ok=True) - with sv.ImageSink(target_dir_path=output_dir, overwrite=False) as sink: + with sv.ImageSink(target_dir_path=output_dir, overwrite=overwrite) as sink: for entry in results: # Process the data if the name of the file is in the dataframe if os.path.basename(entry["img_id"]) in original_df['path'].values: diff --git a/PytorchWildlife/utils/post_process.py b/PytorchWildlife/utils/post_process.py index 0770bcc02..26187b54b 100644 --- a/PytorchWildlife/utils/post_process.py +++ b/PytorchWildlife/utils/post_process.py @@ -21,7 +21,7 @@ # !!! Output paths need to be optimized !!! -def save_detection_images(results, output_dir): +def save_detection_images(results, output_dir, overwrite=False): """ Save detected images with bounding boxes and labels annotated. @@ -30,11 +30,13 @@ def save_detection_images(results, output_dir): Detection results containing image ID, detections, and labels. output_dir (str): Directory to save the annotated images. + overwrite (bool): + Whether overwriting existing image folders. Default to False. """ box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2) os.makedirs(output_dir, exist_ok=True) - with sv.ImageSink(target_dir_path=output_dir, overwrite=True) as sink: + with sv.ImageSink(target_dir_path=output_dir, overwrite=overwrite) as sink: if isinstance(results, list): for entry in results: annotated_img = box_annotator.annotate( @@ -57,7 +59,7 @@ def save_detection_images(results, output_dir): # !!! Output paths need to be optimized !!! -def save_crop_images(results, output_dir): +def save_crop_images(results, output_dir, overwrite=False): """ Save cropped images based on the detection bounding boxes. @@ -66,10 +68,12 @@ def save_crop_images(results, output_dir): Detection results containing image ID and detections. output_dir (str): Directory to save the cropped images. + overwrite (bool): + Whether overwriting existing image folders. Default to False. """ assert isinstance(results, list) os.makedirs(output_dir, exist_ok=True) - with sv.ImageSink(target_dir_path=output_dir, overwrite=True) as sink: + with sv.ImageSink(target_dir_path=output_dir, overwrite=overwrite) as sink: for entry in results: for i, (xyxy, _, _, cat, _) in enumerate(entry["detections"]): cropped_img = sv.crop_image( From 3c0151de3229fd915a5f9ad94427f2c5c9539cf9 Mon Sep 17 00:00:00 2001 From: Zhongqi Miao Date: Mon, 15 Apr 2024 14:08:09 -0700 Subject: [PATCH 2/3] Add sink overwrite argument to demo image saving functions --- demo/image_demo.py | 6 +++--- demo/image_detection_colabdemo.ipynb | 6 +++--- demo/image_detection_demo.ipynb | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/demo/image_demo.py b/demo/image_demo.py index 0c097230e..58eb741ba 100644 --- a/demo/image_demo.py +++ b/demo/image_demo.py @@ -43,7 +43,7 @@ results = detection_model.single_image_detection(transform(img), img.shape, tgt_img_path) # Saving the detection results -pw_utils.save_detection_images(results, os.path.join(".","demo_output")) +pw_utils.save_detection_images(results, os.path.join(".","demo_output"), overwrite=False) #%% Batch detection """ Batch-detection demo """ @@ -68,11 +68,11 @@ #%% Output to annotated images # Saving the batch detection results as annotated images -pw_utils.save_detection_images(results, "batch_output") +pw_utils.save_detection_images(results, "batch_output", overwrite=False) #%% Output to cropped images # Saving the detected objects as cropped images -pw_utils.save_crop_images(results, "crop_output") +pw_utils.save_crop_images(results, "crop_output", overwrite=False) #%% Output to JSON results # Saving the detection results in JSON format diff --git a/demo/image_detection_colabdemo.ipynb b/demo/image_detection_colabdemo.ipynb index acc278e00..55f2531d3 100644 --- a/demo/image_detection_colabdemo.ipynb +++ b/demo/image_detection_colabdemo.ipynb @@ -925,7 +925,7 @@ "transform = pw_trans.MegaDetector_v5_Transform(target_size=detection_model.IMAGE_SIZE,\n", " stride=detection_model.STRIDE)\n", "results = detection_model.single_image_detection(transform(img), img.shape, temp_file_path)\n", - "pw_utils.save_detection_images(results, \"./demo_output\")" + "pw_utils.save_detection_images(results, \"./demo_output\", overwrite=False)" ] }, { @@ -997,7 +997,7 @@ }, "outputs": [], "source": [ - "pw_utils.save_detection_images(results, \"batch_output\")" + "pw_utils.save_detection_images(results, \"batch_output\", overwrite=False)" ] }, { @@ -1020,7 +1020,7 @@ }, "outputs": [], "source": [ - "pw_utils.save_crop_images(results, \"crop_output\")" + "pw_utils.save_crop_images(results, \"crop_output\", overwrite=False)" ] }, { diff --git a/demo/image_detection_demo.ipynb b/demo/image_detection_demo.ipynb index 5af15e077..4d7f5e2ff 100644 --- a/demo/image_detection_demo.ipynb +++ b/demo/image_detection_demo.ipynb @@ -88,7 +88,7 @@ "transform = pw_trans.MegaDetector_v5_Transform(target_size=detection_model.IMAGE_SIZE,\n", " stride=detection_model.STRIDE)\n", "results = detection_model.single_image_detection(transform(img), img.shape, tgt_img_path)\n", - "pw_utils.save_detection_images(results, os.path.join(\".\",\"demo_output\"))" + "pw_utils.save_detection_images(results, os.path.join(\".\",\"demo_output\"), overwrite=False)" ] }, { @@ -138,7 +138,7 @@ "metadata": {}, "outputs": [], "source": [ - "pw_utils.save_detection_images(results, \"batch_output\")" + "pw_utils.save_detection_images(results, \"batch_output\", overwrite=False)" ] }, { @@ -157,7 +157,7 @@ "metadata": {}, "outputs": [], "source": [ - "pw_utils.save_crop_images(results, \"crop_output\")" + "pw_utils.save_crop_images(results, \"crop_output\", overwrite=False)" ] }, { From 302a4edd65257f5dc846057e24b2ee47dc4e35e5 Mon Sep 17 00:00:00 2001 From: Zhongqi Miao Date: Mon, 15 Apr 2024 14:08:43 -0700 Subject: [PATCH 3/3] bump version to 1.0.2.12 --- setup.py | 2 +- version.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index f85b07061..ea7186d1b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup( name='PytorchWildlife', - version='1.0.2.11', + version='1.0.2.12', packages=find_packages(), url='https://github.com/microsoft/CameraTraps/', license='MIT', diff --git a/version.txt b/version.txt index bd516237c..e74ee55b6 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.2.11 \ No newline at end of file +1.0.2.12 \ No newline at end of file