|
|
@ -54,7 +54,6 @@ class Annotator:
|
|
|
|
non_ascii = not is_ascii(example) # non-latin labels, i.e. asian, arabic, cyrillic
|
|
|
|
non_ascii = not is_ascii(example) # non-latin labels, i.e. asian, arabic, cyrillic
|
|
|
|
self.pil = pil or non_ascii
|
|
|
|
self.pil = pil or non_ascii
|
|
|
|
if self.pil: # use PIL
|
|
|
|
if self.pil: # use PIL
|
|
|
|
self.pil_9_2_0_check = check_version(pil_version, '9.2.0') # deprecation check
|
|
|
|
|
|
|
|
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
|
|
|
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
|
|
|
self.draw = ImageDraw.Draw(self.im)
|
|
|
|
self.draw = ImageDraw.Draw(self.im)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -63,6 +62,9 @@ class Annotator:
|
|
|
|
self.font = ImageFont.truetype(str(font), size)
|
|
|
|
self.font = ImageFont.truetype(str(font), size)
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
|
self.font = ImageFont.load_default()
|
|
|
|
self.font = ImageFont.load_default()
|
|
|
|
|
|
|
|
# Deprecation fix for w, h = getsize(string) -> _, _, w, h = getbox(string)
|
|
|
|
|
|
|
|
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
|
|
|
|
else: # use cv2
|
|
|
|
self.im = im
|
|
|
|
self.im = im
|
|
|
|
self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
|
|
|
|
self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
|
|
|
@ -80,10 +82,7 @@ class Annotator:
|
|
|
|
if self.pil or not is_ascii(label):
|
|
|
|
if self.pil or not is_ascii(label):
|
|
|
|
self.draw.rectangle(box, width=self.lw, outline=color) # box
|
|
|
|
self.draw.rectangle(box, width=self.lw, outline=color) # box
|
|
|
|
if label:
|
|
|
|
if label:
|
|
|
|
if self.pil_9_2_0_check:
|
|
|
|
w, h = self.font.getsize(label) # text width, height
|
|
|
|
_, _, w, h = self.font.getbbox(label) # text width, height (New)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
w, h = self.font.getsize(label) # text width, height (Old, deprecated in 9.2.0)
|
|
|
|
|
|
|
|
outside = box[1] - h >= 0 # label fits outside box
|
|
|
|
outside = box[1] - h >= 0 # label fits outside box
|
|
|
|
self.draw.rectangle(
|
|
|
|
self.draw.rectangle(
|
|
|
|
(box[0], box[1] - h if outside else box[1], box[0] + w + 1,
|
|
|
|
(box[0], box[1] - h if outside else box[1], box[0] + w + 1,
|
|
|
|