Fix `results.show()` for Jupyter notebooks (#16783)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
pull/16782/head
Glenn Jocher 2 months ago committed by GitHub
parent 84fd8f6e49
commit 4212ffc795
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      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 IS_JUPYTER, LOGGER, TryExcept, ops, plt_settings, threaded
from ultralytics.utils.checks import check_font, check_requirements, check_version, is_ascii
from ultralytics.utils import IS_COLAB, IS_KAGGLE, LOGGER, TryExcept, ops, plt_settings, threaded
from ultralytics.utils.checks import check_font, check_version, is_ascii
from ultralytics.utils.files import increment_path
@ -525,16 +525,12 @@ class Annotator:
def show(self, title=None):
"""Show the annotated image."""
im = Image.fromarray(np.asarray(self.im)[..., ::-1]) # Convert numpy array to PIL Image with RGB to BGR
if IS_JUPYTER:
check_requirements("ipython")
if IS_COLAB or IS_KAGGLE: # can not use IS_JUPYTER as will run for all ipython environments
try:
from IPython.display import display
display(im)
display(im) # noqa - display() function only available in ipython environments
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"):

Loading…
Cancel
Save