diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 979ba2d8..3c83bc4f 100644 --- a/ultralytics/utils/plotting.py +++ b/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