Allow Annotator PIL.Image inputs (#8397)

pull/8409/head
Glenn Jocher 11 months ago committed by GitHub
parent 0572b29445
commit 21088e3986
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 9
      ultralytics/utils/plotting.py

@ -112,12 +112,12 @@ class Annotator:
def __init__(self, im, line_width=None, font_size=None, font="Arial.ttf", pil=False, example="abc"):
"""Initialize the Annotator class with image and line width along with color palette for keypoints and limbs."""
assert im.data.contiguous, "Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images."
non_ascii = not is_ascii(example) # non-latin labels, i.e. asian, arabic, cyrillic
self.pil = pil or non_ascii
self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
input_is_pil = isinstance(im, Image.Image)
self.pil = pil or non_ascii or input_is_pil
self.lw = line_width or max(round(sum(im.size if input_is_pil else im.shape) / 2 * 0.003), 2)
if self.pil: # use PIL
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
self.im = im if input_is_pil else Image.fromarray(im)
self.draw = ImageDraw.Draw(self.im)
try:
font = check_font("Arial.Unicode.ttf" if non_ascii else font)
@ -129,6 +129,7 @@ class Annotator:
if check_version(pil_version, "9.2.0"):
self.font.getsize = lambda x: self.font.getbbox(x)[2:4] # text width, height
else: # use cv2
assert im.data.contiguous, "Image not contiguous. Apply np.ascontiguousarray(im) to Annotator input images."
self.im = im if im.flags.writeable else im.copy()
self.tf = max(self.lw - 1, 1) # font thickness
self.sf = self.lw / 3 # font scale

Loading…
Cancel
Save