glenn-jocher commited on
Commit
de534e9
1 Parent(s): a4e8f78

Fix 3 for Arial.ttf redownloads with hub inference (#4629)

Browse files

Fix 3 for Arial.ttf redownloads with hub inference, follow-on to #4628.

Files changed (1) hide show
  1. utils/plots.py +17 -13
utils/plots.py CHANGED
@@ -48,7 +48,22 @@ class Colors:
48
  colors = Colors() # create instance for 'from utils.plots import colors'
49
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  class Annotator:
 
 
52
  # YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
53
  def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
54
  assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
@@ -56,22 +71,11 @@ class Annotator:
56
  if self.pil: # use PIL
57
  self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
58
  self.draw = ImageDraw.Draw(self.im)
59
- s = sum(self.im.size) / 2 # mean shape
60
- f = font_size or max(round(s * 0.035), 12)
61
- font = Path(font) # font handling
62
- font = font if font.exists() else (ROOT / font.name)
63
- try:
64
- self.font = ImageFont.truetype(str(font) if font.exists() else font.name, size=f)
65
- except Exception as e: # download if missing
66
- url = "https://ultralytics.com/assets/" + font.name
67
- print(f'Downloading {url} to {font}...')
68
- torch.hub.download_url_to_file(url, str(font))
69
- self.font = ImageFont.truetype(font, size=f)
70
  self.fh = self.font.getsize('a')[1] - 3 # font height
71
  else: # use cv2
72
  self.im = im
73
- s = sum(im.shape) / 2 # mean shape
74
- self.lw = line_width or max(round(s * 0.003), 2) # line width
75
 
76
  def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
77
  # Add one xyxy box to image with label
 
48
  colors = Colors() # create instance for 'from utils.plots import colors'
49
 
50
 
51
+ def check_font(font='Arial.ttf', size=10):
52
+ # Return a PIL TrueType Font, downloading to ROOT dir if necessary
53
+ font = Path(font)
54
+ font = font if font.exists() else (ROOT / font.name)
55
+ try:
56
+ return ImageFont.truetype(str(font) if font.exists() else font.name, size)
57
+ except Exception as e: # download if missing
58
+ url = "https://ultralytics.com/assets/" + font.name
59
+ print(f'Downloading {url} to {font}...')
60
+ torch.hub.download_url_to_file(url, str(font))
61
+ return ImageFont.truetype(str(font), size)
62
+
63
+
64
  class Annotator:
65
+ check_font() # download TTF if necessary
66
+
67
  # YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
68
  def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
69
  assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
 
71
  if self.pil: # use PIL
72
  self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
73
  self.draw = ImageDraw.Draw(self.im)
74
+ self.font = check_font(font, size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
 
 
 
 
 
 
 
 
 
 
75
  self.fh = self.font.getsize('a')[1] - 3 # font height
76
  else: # use cv2
77
  self.im = im
78
+ self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
 
79
 
80
  def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
81
  # Add one xyxy box to image with label