microsoft / CameraTraps

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

I can't save the crop image #522

Closed Cyingyinging closed 1 month ago

Cyingyinging commented 3 months ago

Search before asking

Bug

when i run the demo, I find a question : I can not save the crop image. The problem is: File "D:\anaconda3\envs\island_project\lib\site-packages\PytorchWildlife\utils\post_process.py", line 77, in save_crop_images assert isinstance(results, list) AssertionError. So, i checked error image

Environment

No response

Minimal Reproducible Example

import numpy as np from PIL import Image import torch from torch.utils.data import DataLoader from PytorchWildlife.models import detection as pw_detection from PytorchWildlife.data import transforms as pw_trans from PytorchWildlife.data import datasets as pw_data from PytorchWildlife import utils as pw_utils import os

Setting the device to use for computations ('cuda' indicates GPU)

DEVICE = "cuda" if torch.cuda.is_available() else "cpu" detection_model = pw_detection.MegaDetectorV5(device=DEVICE, pretrained=True) tgt_img_path = os.path.join(".","10050028_1.JPG") img = np.array(Image.open(tgt_img_path).convert("RGB")) transform = pw_trans.MegaDetector_v5_Transform(target_size=detection_model.IMAGE_SIZE, stride=detection_model.STRIDE) results = detection_model.single_image_detection(transform(img), img.shape, tgt_img_path) pw_utils.save_detection_images(results, os.path.join(".","demo_output"), overwrite=False) pw_utils.save_crop_images(results, os.path.join(".","crop_output"))

Additional

No response

Are you willing to submit a PR?

Cyingyinging commented 3 months ago

I made changes in post_process.py.It can run:

!!! Output paths need to be optimized !!!

def save_crop_images(results, output_dir, overwrite=False): """ Save cropped images based on the detection bounding boxes.

Args:
    results (list):
        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=overwrite) as sink:
    # for entry in results:
    #     print(entry)
    for i, (xyxy, _, _, cat, _) in enumerate(results["detections"]):
        cropped_img = sv.crop_image(
            image=np.array(Image.open(results["img_id"]).convert("RGB")), xyxy=xyxy
        )
        sink.save_image(
            image=cv2.cvtColor(cropped_img, cv2.COLOR_RGB2BGR),
            image_name="{}_{}_{}".format(
                int(cat), i, results["img_id"].rsplit(os.sep, 1)[1]
            ),
        )
danielaruizl1 commented 2 months ago

Hi @Cyingyinging! thank you for reaching out. We are currently updating the code to make the save_crop_images function compatible with single image inputs, in addition to the existing functionality for a list of images. We will notify you as soon as the updated version is available for use.

zhmiao commented 1 month ago

@Cyingyinging Issue resolved in the latest updates. Please feel free to reopen if the issue still persists. Thanks!