Enable result.show() for Jupyter notebooks (#16573)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn Jocher 2024-09-30 19:55:24 +02:00 committed by GitHub
parent 7a6c76d16c
commit 0f8b0f2e3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,8 +13,8 @@ import torch
from PIL import Image, ImageDraw, ImageFont
from PIL import __version__ as pil_version
from ultralytics.utils import LOGGER, TryExcept, ops, plt_settings, threaded
from ultralytics.utils.checks import check_font, check_version, is_ascii
from ultralytics.utils import IS_JUPYTER, LOGGER, TryExcept, ops, plt_settings, threaded
from ultralytics.utils.checks import check_font, check_requirements, check_version, is_ascii
from ultralytics.utils.files import increment_path
@ -524,7 +524,18 @@ class Annotator:
def show(self, title=None):
"""Show the annotated image."""
Image.fromarray(np.asarray(self.im)[..., ::-1]).show(title)
im = Image.fromarray(np.asarray(self.im)[..., ::-1]) # Convert numpy array to PIL Image with RGB to BGR
if IS_JUPYTER:
check_requirements("ipython")
try:
from IPython.display import display
display(im)
except ImportError as e:
LOGGER.warning(f"Unable to display image in Jupyter notebooks: {e}")
else:
# Convert numpy array to PIL Image and show
im.show(title=title)
def save(self, filename="image.jpg"):
"""Save the annotated image to 'filename'."""