Auto-download Arial.ttf on init (#4606)
Browse files* Auto-download Arial.ttf on init
* Fix ROOT
- utils/__init__.py +16 -0
- utils/plots.py +4 -5
utils/__init__.py
CHANGED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from PIL import ImageFont
|
5 |
+
|
6 |
+
FILE = Path(__file__).absolute()
|
7 |
+
ROOT = FILE.parents[1] # yolov5/ dir
|
8 |
+
|
9 |
+
# Check YOLOv5 Annotator font
|
10 |
+
font = 'Arial.ttf'
|
11 |
+
try:
|
12 |
+
ImageFont.truetype(font)
|
13 |
+
except Exception as e: # download if missing
|
14 |
+
url = "https://ultralytics.com/assets/" + font
|
15 |
+
print(f'Downloading {url} to {ROOT / font}...')
|
16 |
+
torch.hub.download_url_to_file(url, str(ROOT / font))
|
utils/plots.py
CHANGED
@@ -48,7 +48,7 @@ colors = Colors() # create instance for 'from utils.plots import colors'
|
|
48 |
class Annotator:
|
49 |
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
50 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
|
51 |
-
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to
|
52 |
self.pil = pil
|
53 |
if self.pil: # use PIL
|
54 |
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
@@ -57,11 +57,10 @@ class Annotator:
|
|
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
|
61 |
-
|
62 |
-
url
|
63 |
torch.hub.download_url_to_file(url, font)
|
64 |
-
print(f'Annotator font successfully downloaded from {url} to {font}')
|
65 |
self.font = ImageFont.truetype(font, size=f)
|
66 |
self.fh = self.font.getsize('a')[1] - 3 # font height
|
67 |
else: # use cv2
|
|
|
48 |
class Annotator:
|
49 |
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
50 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
|
51 |
+
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
|
52 |
self.pil = pil
|
53 |
if self.pil: # use PIL
|
54 |
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
|
|
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
|