glenn-jocher commited on
Commit
a4e8f78
·
unverified ·
1 Parent(s): ba0f808

Fix 2 for Arial.ttf redownloads with hub inference (#4628)

Browse files
Files changed (2) hide show
  1. utils/__init__.py +19 -19
  2. utils/plots.py +8 -3
utils/__init__.py CHANGED
@@ -1,19 +1,19 @@
1
- import sys
2
- from pathlib import Path
3
-
4
- import torch
5
- from PIL import ImageFont
6
-
7
- FILE = Path(__file__).absolute()
8
- ROOT = FILE.parents[1] # yolov5/ dir
9
- if str(ROOT) not in sys.path:
10
- sys.path.append(str(ROOT)) # add ROOT to PATH
11
-
12
- # Check YOLOv5 Annotator font
13
- font = 'Arial.ttf'
14
- try:
15
- ImageFont.truetype(font)
16
- except Exception as e: # download if missing
17
- url = "https://ultralytics.com/assets/" + font
18
- print(f'Downloading {url} to {ROOT / font}...')
19
- torch.hub.download_url_to_file(url, str(ROOT / font))
 
1
+ # import sys
2
+ # from pathlib import Path
3
+ #
4
+ # import torch
5
+ # from PIL import ImageFont
6
+ #
7
+ # FILE = Path(__file__).absolute()
8
+ # ROOT = FILE.parents[1] # yolov5/ dir
9
+ # if str(ROOT) not in sys.path:
10
+ # sys.path.append(str(ROOT)) # add ROOT to PATH
11
+ #
12
+ # # Check YOLOv5 Annotator font
13
+ # font = 'Arial.ttf'
14
+ # try:
15
+ # ImageFont.truetype(font)
16
+ # except Exception as e: # download if missing
17
+ # url = "https://ultralytics.com/assets/" + font
18
+ # print(f'Downloading {url} to {ROOT / font}...')
19
+ # torch.hub.download_url_to_file(url, str(ROOT / font))
utils/plots.py CHANGED
@@ -23,6 +23,9 @@ from utils.metrics import fitness
23
  matplotlib.rc('font', **{'size': 11})
24
  matplotlib.use('Agg') # for writing to files only
25
 
 
 
 
26
 
27
  class Colors:
28
  # Ultralytics color palette https://ultralytics.com/
@@ -55,12 +58,14 @@ class Annotator:
55
  self.draw = ImageDraw.Draw(self.im)
56
  s = sum(self.im.size) / 2 # mean shape
57
  f = font_size or max(round(s * 0.035), 12)
 
 
58
  try:
59
- self.font = ImageFont.truetype(font, size=f)
60
  except Exception as e: # download if missing
61
- url = "https://ultralytics.com/assets/" + font
62
  print(f'Downloading {url} to {font}...')
63
- torch.hub.download_url_to_file(url, font)
64
  self.font = ImageFont.truetype(font, size=f)
65
  self.fh = self.font.getsize('a')[1] - 3 # font height
66
  else: # use cv2
 
23
  matplotlib.rc('font', **{'size': 11})
24
  matplotlib.use('Agg') # for writing to files only
25
 
26
+ FILE = Path(__file__).absolute()
27
+ ROOT = FILE.parents[1] # yolov5/ dir
28
+
29
 
30
  class Colors:
31
  # Ultralytics color palette https://ultralytics.com/
 
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