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

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
pull/16575/head
Glenn Jocher 2 months ago committed by GitHub
parent 7a6c76d16c
commit 0f8b0f2e3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 17
      ultralytics/utils/plotting.py

@ -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'."""

Loading…
Cancel
Save