10000 Improved non-latin `Annotator()` plotting (#7488) · brinkqiang2python/yolov5@c9042dc · GitHub
[go: up one dir, main page]

Skip to content

Commit c9042dc

Browse files
Improved non-latin Annotator() plotting (ultralytics#7488)
* Improved non-latin labels Annotator plotting May resolve ultralytics#7460 * Update train.py * Update train.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add progress arg Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d876caa commit c9042dc

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

train.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
from utils.downloads import attempt_download< 10000 /span>
4949
from utils.general import (LOGGER, check_dataset, check_file, check_git_status, check_img_size, check_requirements,
5050
check_suffix, check_yaml, colorstr, get_latest_run, increment_path, init_seeds,
51-
intersect_dicts, labels_to_class_weights, labels_to_image_weights, methods, one_cycle,
52-
print_args, print_mutation, strip_optimizer)
51+
intersect_dicts, is_ascii, labels_to_class_weights, labels_to_image_weights, methods,
52+
one_cycle, print_args, print_mutation, strip_optimizer)
5353
from utils.loggers import Loggers
5454
from utils.loggers.wandb.wandb_utils import check_wandb_resume
5555
from utils.loss import ComputeLoss
5656
from utils.metrics import fitness
57-
from utils.plots import plot_evolve, plot_labels
57+
from utils.plots import check_font, plot_evolve, plot_labels
5858
from utils.torch_utils import EarlyStopping, ModelEMA, de_parallel, select_device, torch_distributed_zero_first
5959

6060
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
@@ -105,6 +105,8 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
105105
init_seeds(1 + RANK)
106106
with torch_distributed_zero_first(LOCAL_RANK):
107107
data_dict = data_dict or check_dataset(data) # check if None
108+
if not is_ascii(data_dict['names']): # non-latin labels, i.e. asian, arabic, cyrillic
109+
check_font('Arial.Unicode.ttf', progress=True)
108110
train_path, val_path = data_dict['train'], data_dict['val']
109111
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
110112
names = ['item'] if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names

utils/general.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,13 @@ def check_file(file, suffix=''):
424424
return files[0] # return file
425425

426426

427-
def check_font(font=FONT):
427+
def check_font(font=FONT, progress=False):
428428
# Download font to CONFIG_DIR if necessary
429429
font = Path(font)
430430
if not font.exists() and not (CONFIG_DIR / font.name).exists():
431431
url = "https://ultralytics.com/assets/" + font.name
432432
LOGGER.info(f'Downloading {url} to {CONFIG_DIR / font.name}...')
433-
torch.hub.download_url_to_file(url, str(font), progress=False)
433+
torch.hub.download_url_to_file(url, str(font), progress=progress)
434434

435435

436436
def check_dataset(data, autodownload=True):

utils/plots.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from PIL import Image, ImageDraw, ImageFont
2020

2121
from utils.general import (CONFIG_DIR, FONT, LOGGER, Timeout, check_font, check_requirements, clip_coords,
22-
increment_path, is_ascii, is_chinese, try_except, xywh2xyxy, xyxy2xywh)
22+
increment_path, is_ascii, try_except, xywh2xyxy, xyxy2xywh)
2323
from utils.metrics import fitness
2424

2525
# Settings
@@ -72,11 +72,12 @@ class Annotator:
7272
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
7373
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
7474
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
75-
self.pil = pil or not is_ascii(example) or is_chinese(example)
75+
non_ascii = not is_ascii(example) # non-latin labels, i.e. asian, arabic, cyrillic
76+
self.pil = pil or non_ascii
7677
if self.pil: # use PIL
7778
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
7879
self.draw = ImageDraw.Draw(self.im)
79-
self.font = check_pil_font(font='Arial.Unicode.ttf' if is_chinese(example) else font,
80+
self.font = check_pil_font(font='Arial.Unicode.ttf' if non_ascii else font,
8081
size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
8182
else: # use cv2
8283
self.im = im

0 commit comments

Comments
 (0)
0