Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

@joaodiogocosta joaodiogocosta Sep 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glenn-jocher this line breaks any system that doesn't allow writing on this specific directory.

For example, AWS Lambda Functions only allow writing to /tmp.

This is an issue because when pulling git pull ...ultralytics/yolov5 and running it inside a Lambda function, it will fail with:

[ERROR] OSError: [Errno 30] Read-only file system: '/var/task/yolov5/tmpgg9rbwwq'
Traceback (most recent call last):
  File "/var/task/pyworker/models/mod/find_objects.py", line 15, in __init__
    self.model = object_detector()
  File "/var/task/pyworker/models/ml_models.py", line 9, in object_detector
    return torch.hub.load('yolov5', 'custom', path=model_path, source='local')
  File "/var/lang/lib/python3.8/site-packages/torch/hub.py", line 364, in load
    model = _load_local(repo_or_dir, model, *args, **kwargs)
  File "/var/lang/lib/python3.8/site-packages/torch/hub.py", line 393, in _load_local
    model = entry(*args, **kwargs)
  File "/var/task/yolov5/hubconf.py", line 70, in custom
    return _create(path, autoshape=autoshape, verbose=verbose, device=device)
  File "/var/task/yolov5/hubconf.py", line 30, in _create
    from models.yolo import Model
  File "/var/task/yolov5/models/yolo.py", line 17, in <module>
    from models.common import *
  File "/var/task/yolov5/models/common.py", line 23, in <module>
    from utils.plots import Annotator, colors
  File "/var/task/yolov5/utils/plots.py", line 64, in <module>
    class Annotator:
  File "/var/task/yolov5/utils/plots.py", line 65, in Annotator
    check_font()  # download TTF if necessary
  File "/var/task/yolov5/utils/plots.py", line 60, in check_font
    torch.hub.download_url_to_file(url, str(font))
  File "/var/lang/lib/python3.8/site-packages/torch/hub.py", line 433, in download_url_to_file
    f = tempfile.NamedTemporaryFile(delete=False, dir=dst_dir)
  File "/var/lang/lib/python3.8/tempfile.py", line 540, in NamedTemporaryFile
    (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "/var/lang/lib/python3.8/tempfile.py", line 250, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)

Is there a way to configure the directory to which Arial.ttf is written?



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