Skip to content

Commit

Permalink
Fix 2 for Arial.ttf redownloads with hub inference (ultralytics#4628)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher authored Aug 31, 2021
1 parent e9f4767 commit da0be6c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
38 changes: 19 additions & 19 deletions utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import sys
from pathlib import Path

import torch
from PIL import ImageFont

FILE = Path(__file__).absolute()
ROOT = FILE.parents[1] # yolov5/ dir
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH

# Check YOLOv5 Annotator font
font = 'Arial.ttf'
try:
ImageFont.truetype(font)
except Exception as e: # download if missing
url = "https://ultralytics.com/assets/" + font
print(f'Downloading {url} to {ROOT / font}...')
torch.hub.download_url_to_file(url, str(ROOT / font))
# import sys
# from pathlib import Path
#
# import torch
# from PIL import ImageFont
#
# FILE = Path(__file__).absolute()
# ROOT = FILE.parents[1] # yolov5/ dir
# if str(ROOT) not in sys.path:
# sys.path.append(str(ROOT)) # add ROOT to PATH
#
# # Check YOLOv5 Annotator font
# font = 'Arial.ttf'
# try:
# ImageFont.truetype(font)
# except Exception as e: # download if missing
# url = "https://ultralytics.com/assets/" + font
# print(f'Downloading {url} to {ROOT / font}...')
# torch.hub.download_url_to_file(url, str(ROOT / font))
11 changes: 8 additions & 3 deletions utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
matplotlib.rc('font', **{'size': 11})
matplotlib.use('Agg') # for writing to files only

FILE = Path(__file__).absolute()
ROOT = FILE.parents[1] # yolov5/ dir


class Colors:
# Ultralytics color palette https://ultralytics.com/
Expand Down Expand Up @@ -55,12 +58,14 @@ def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=Tr
self.draw = ImageDraw.Draw(self.im)
s = sum(self.im.size) / 2 # mean shape
f = font_size or max(round(s * 0.035), 12)
font = Path(font) # font handling
font = font if font.exists() else (ROOT / font.name)
try:
self.font = ImageFont.truetype(font, size=f)
self.font = ImageFont.truetype(str(font) if font.exists() else font.name, size=f)
except Exception as e: # download if missing
url = "https://ultralytics.com/assets/" + font
url = "https://ultralytics.com/assets/" + font.name
print(f'Downloading {url} to {font}...')
torch.hub.download_url_to_file(url, font)
torch.hub.download_url_to_file(url, str(font))
self.font = ImageFont.truetype(font, size=f)
self.fh = self.font.getsize('a')[1] - 3 # font height
else: # use cv2
Expand Down

0 comments on commit da0be6c

Please sign in to comment.