Update val_batch*.jpg for Chinese fonts (#6526)
* Update plots for Chinese fonts * make is_chinese() non-str safe * Add global FONT * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update general.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
cb40c9afda
commit
c3e599cfda
2 files changed
+52
-42
No files matched your search
+10
-13
@@ -17,12 +17,11 @@ import seaborn as sn
|
||||
import torch
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from utils.general import (LOGGER, Timeout, check_requirements, clip_coords, increment_path, is_ascii, is_chinese,
|
||||
try_except, user_config_dir, xywh2xyxy, xyxy2xywh)
|
||||
from utils.general import (CONFIG_DIR, FONT, LOGGER, Timeout, check_font, check_requirements, clip_coords,
|
||||
increment_path, is_ascii, is_chinese, try_except, xywh2xyxy, xyxy2xywh)
|
||||
from utils.metrics import fitness
|
||||
|
||||
# Settings
|
||||
CONFIG_DIR = user_config_dir() # Ultralytics settings dir
|
||||
RANK = int(os.getenv('RANK', -1))
|
||||
matplotlib.rc('font', **{'size': 11})
|
||||
matplotlib.use('Agg') # for writing to files only
|
||||
@@ -49,16 +48,14 @@ class Colors:
|
||||
colors = Colors() # create instance for 'from utils.plots import colors'
|
||||
|
||||
|
||||
def check_font(font='Arial.ttf', size=10):
|
||||
def check_pil_font(font=FONT, size=10):
|
||||
# Return a PIL TrueType Font, downloading to CONFIG_DIR if necessary
|
||||
font = Path(font)
|
||||
font = font if font.exists() else (CONFIG_DIR / font.name)
|
||||
try:
|
||||
return ImageFont.truetype(str(font) if font.exists() else font.name, size)
|
||||
except Exception as e: # download if missing
|
||||
url = "https://ultralytics.com/assets/" + font.name
|
||||
LOGGER.info(f'Downloading {url} to {font}...')
|
||||
torch.hub.download_url_to_file(url, str(font), progress=False)
|
||||
check_font(font)
|
||||
try:
|
||||
return ImageFont.truetype(str(font), size)
|
||||
except TypeError:
|
||||
@@ -67,7 +64,7 @@ def check_font(font='Arial.ttf', size=10):
|
||||
|
||||
class Annotator:
|
||||
if RANK in (-1, 0):
|
||||
check_font() # download TTF if necessary
|
||||
check_pil_font() # download TTF if necessary
|
||||
|
||||
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
||||
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
|
||||
@@ -76,8 +73,8 @@ class Annotator:
|
||||
if self.pil: # use PIL
|
||||
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
||||
self.draw = ImageDraw.Draw(self.im)
|
||||
self.font = check_font(font='Arial.Unicode.ttf' if is_chinese(example) else font,
|
||||
size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
|
||||
self.font = check_pil_font(font='Arial.Unicode.ttf' if is_chinese(example) else font,
|
||||
size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
|
||||
else: # use cv2
|
||||
self.im = im
|
||||
self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
|
||||
@@ -89,10 +86,10 @@ class Annotator:
|
||||
if label:
|
||||
w, h = self.font.getsize(label) # text width, height
|
||||
outside = box[1] - h >= 0 # label fits outside box
|
||||
self.draw.rectangle([box[0],
|
||||
self.draw.rectangle((box[0],
|
||||
box[1] - h if outside else box[1],
|
||||
box[0] + w + 1,
|
||||
box[1] + 1 if outside else box[1] + h + 1], fill=color)
|
||||
box[1] + 1 if outside else box[1] + h + 1), fill=color)
|
||||
# self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls') # for PIL>8.0
|
||||
self.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)
|
||||
else: # cv2
|
||||
@@ -210,7 +207,7 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max
|
||||
|
||||
# Annotate
|
||||
fs = int((h + w) * ns * 0.01) # font size
|
||||
annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs, pil=True)
|
||||
annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs, pil=True, example=names)
|
||||
for i in range(i + 1):
|
||||
x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin
|
||||
annotator.rectangle([x, y, x + w, y + h], None, (255, 255, 255), width=2) # borders
|
||||
|
||||
Reference in new issue
Block a user