From 1744aecd5702fabb4c51c9babdf455a0e4ebc139 Mon Sep 17 00:00:00 2001 From: Diego Montes Date: Mon, 27 Sep 2021 22:17:05 -0400 Subject: [PATCH 1/2] fix isascii for python3.6 --- utils/general.py | 5 +++++ utils/plots.py | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/utils/general.py b/utils/general.py index 8421981147f7..42b6ee2f5d88 100755 --- a/utils/general.py +++ b/utils/general.py @@ -160,6 +160,11 @@ def is_pip(): # Is file in a pip package? return 'site-packages' in Path(__file__).resolve().parts +def is_ascii(s=''): + # Is string composed of all ASCII (no UTF) characters? + s = str(s) # convert list, tuple, None, etc. to str + return len(s.encode().decode('ascii', 'ignore')) == len(s) + def is_chinese(s='人工智能'): # Is string composed of any Chinese characters? diff --git a/utils/plots.py b/utils/plots.py index 491c5704d67b..2f98d5b7e630 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -17,7 +17,7 @@ import torch from PIL import Image, ImageDraw, ImageFont -from utils.general import user_config_dir, is_chinese, xywh2xyxy, xyxy2xywh +from utils.general import user_config_dir, is_ascii, is_chinese, xywh2xyxy, xyxy2xywh from utils.metrics import fitness # Settings @@ -68,7 +68,7 @@ class Annotator: # 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'): assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.' - self.pil = pil or not example.isascii() or is_chinese(example) + self.pil = pil or not is_ascii(example) or is_chinese(example) if self.pil: # use PIL self.im = im if isinstance(im, Image.Image) else Image.fromarray(im) self.draw = ImageDraw.Draw(self.im) @@ -80,7 +80,7 @@ def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=Fa def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)): # Add one xyxy box to image with label - if self.pil or not label.isascii(): + if self.pil or not is_ascii(label): self.draw.rectangle(box, width=self.lw, outline=color) # box if label: w, h = self.font.getsize(label) # text width, height From 108dbad0d7661472ccaf4da82b2df5b7cf008993 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Mon, 27 Sep 2021 20:12:11 -0700 Subject: [PATCH 2/2] update comment with python 3.7 note --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 42b6ee2f5d88..28301f8573bb 100755 --- a/utils/general.py +++ b/utils/general.py @@ -161,7 +161,7 @@ def is_pip(): return 'site-packages' in Path(__file__).resolve().parts def is_ascii(s=''): - # Is string composed of all ASCII (no UTF) characters? + # Is string composed of all ASCII (no UTF) characters? (note str().isascii() introduced in python 3.7) s = str(s) # convert list, tuple, None, etc. to str return len(s.encode().decode('ascii', 'ignore')) == len(s)