diff --git a/supervision/utils/conversion.py b/supervision/utils/conversion.py index b3c63a86..1a73a1b4 100644 --- a/supervision/utils/conversion.py +++ b/supervision/utils/conversion.py @@ -13,6 +13,8 @@ def convert_for_annotation_method(annotate_func): Decorates `BaseAnnotator.annotate` implementations, converts scene to an image type used internally by the annotators, converts back when annotation is complete. + + Assumes the annotators modify the scene in-place. """ @wraps(annotate_func) @@ -21,9 +23,10 @@ def convert_for_annotation_method(annotate_func): return annotate_func(self, scene, *args, **kwargs) if isinstance(scene, Image.Image): - scene = pillow_to_cv2(scene) - annotated = annotate_func(self, scene, *args, **kwargs) - return cv2_to_pillow(image=annotated) + scene_np = pillow_to_cv2(scene) + annotated_np = annotate_func(self, scene_np, *args, **kwargs) + scene.paste(cv2_to_pillow(annotated_np)) + return scene raise ValueError(f"Unsupported image type: {type(scene)}") @@ -34,6 +37,8 @@ def convert_for_image_processing(image_processing_fun): """ Decorates image processing functions that accept np.ndarray, converting `image` to np.ndarray, converts back when processing is complete. + + Assumes the annotators do NOT modify the scene in-place. """ @wraps(image_processing_fun) @@ -44,7 +49,7 @@ def convert_for_image_processing(image_processing_fun): if isinstance(image, Image.Image): scene = pillow_to_cv2(image) annotated = image_processing_fun(scene, *args, **kwargs) - return cv2_to_pillow(image=annotated) + return cv2_to_pillow(annotated) raise ValueError(f"Unsupported image type: {type(image)}") @@ -55,14 +60,17 @@ def convert_for_rich_text_annotation(annotate_func): """ Decorates image processing functions that accept np.ndarray, converting `image` to PIL image, converts back when processing is complete. + + Assumes the annotators modify the scene in-place. """ @wraps(annotate_func) def wrapper(self, scene: ImageType, *args, **kwargs): if isinstance(scene, np.ndarray): - scene = cv2_to_pillow(scene) - annotated = annotate_func(self, scene, *args, **kwargs) - return pillow_to_cv2(image=annotated) + scene_pil = cv2_to_pillow(scene) + annotated_pil = annotate_func(self, scene_pil, *args, **kwargs) + np.copyto(scene, pillow_to_cv2(annotated_pil)) + return scene if isinstance(scene, Image.Image): return annotate_func(self, scene, *args, **kwargs) @@ -88,7 +96,7 @@ def images_to_cv2(images: List[ImageType]) -> List[np.ndarray]: result = [] for image in images: if issubclass(type(image), Image.Image): - image = pillow_to_cv2(image=image) + image = pillow_to_cv2(image) result.append(image) return result