microsoft / CameraTraps

PyTorch Wildlife: a Collaborative Deep Learning Framework for Conservation.
https://cameratraps.readthedocs.io/en/latest/
MIT License
757 stars 239 forks source link

pw_utils.save_detection_images() cannot save into a non-empty folder #475

Closed AP-DP closed 5 months ago

AP-DP commented 5 months ago

Search before asking

Bug

pw_utils.save_detection_images() results in an error if you add images to an existing folder repeatedly (it works the first time, but fails the second time once the folder is no longer empty). This leads to an error where the target folder is considered to be a file. There are no issues if you create a new folder for each output.

File "{local folder path}.virtenv\lib\site-packages\PytorchWildlife\utils\post_process.py", line 35, in save_detection_images with sv.ImageSink(target_dir_path=output_dir, overwrite=True) as sink:
File "{local folder path}.virtenv\lib\site-packages\supervision\utils\image.py", line 77, in enter os.makedirs(self.target_dir_path) File "C:\Python39\lib\os.py", line 225, in makedirs mkdir(name, mode) FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'uploads\{unique_folder_id}_detections'

Environment

Python 3.9.4 PytorchWildlife 1.0.2.5 OS: Windows 10

Minimal Reproducible Example

def detect_animal_in_image(image_folder, file_name): file_path = get_file_path(image_folder, file_name) img = get_image_from_filepath(file_path) result = detection_model.single_image_detection(transform(img), img.shape, file_path) return result

def output_annotated_images(results, annotated_image_folder):

Output annotated images

pw_utils.save_detection_images(results, annotated_image_folder)

Below results in issue

def process_single_file_by_type(file_folder, file_name, destination_folder): file_path = get_file_path(file_folder, file_name) file_type = detect_file_type(file_path) if file_type == IMAGE_TYPE: results = detect_animal_in_image(file_folder, file_name) output_annotated_images(results, destination_folder) elif file_type == VIDEO_TYPE: detect_animal_in_video(file_folder, destination_folder) else: raise Exception("Invalid file format detected")

Below does not result in issue, but each image is placed in an individual folder

def process_single_file_by_type(file_folder, file_name, destination_folder): file_path = get_file_path(file_folder, file_name) destination_path = get_file_path(destination_folder, file_name) file_type = detect_file_type(file_path) if file_type == IMAGE_TYPE: results = detect_animal_in_image(file_folder, file_name) output_annotated_images(results, destination_path) elif file_type == VIDEO_TYPE: detect_animal_in_video(file_folder, destination_path) else: raise Exception("Invalid file format detected")

Additional

I'll continue the project with unique folders for the time being, but I'll adjust the structure if this is changed in the future.

Are you willing to submit a PR?

zhmiao commented 5 months ago

Hello @AP-DP , thank you very much for reporting! I am going to take a look at the issue and get back to you as soon as possible!

zhmiao commented 5 months ago

Hello @AP-DP, we have fixed the issue and now you have an argument to choose whether to overwrite existing folders in the newest PR, and the overwrite argument is default to False. You can pip install PytorchWildlife --upgrade for the newest version for this argument. I am closing this issue, and feel free to reopen if the issue persist in your case! Thank you again for reporting to us!