Fix `isascii()` method calls for python 3.6 (#4958)
Browse files* fix isascii for python3.6
* update comment with python 3.7 note
Co-authored-by: Glenn Jocher <[email protected]>
- utils/general.py +5 -0
- utils/plots.py +3 -3
utils/general.py
CHANGED
@@ -160,6 +160,11 @@ def is_pip():
|
|
160 |
# Is file in a pip package?
|
161 |
return 'site-packages' in Path(__file__).resolve().parts
|
162 |
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
def is_chinese(s='人工智能'):
|
165 |
# Is string composed of any Chinese characters?
|
|
|
160 |
# Is file in a pip package?
|
161 |
return 'site-packages' in Path(__file__).resolve().parts
|
162 |
|
163 |
+
def is_ascii(s=''):
|
164 |
+
# Is string composed of all ASCII (no UTF) characters? (note str().isascii() introduced in python 3.7)
|
165 |
+
s = str(s) # convert list, tuple, None, etc. to str
|
166 |
+
return len(s.encode().decode('ascii', 'ignore')) == len(s)
|
167 |
+
|
168 |
|
169 |
def is_chinese(s='人工智能'):
|
170 |
# Is string composed of any Chinese characters?
|
utils/plots.py
CHANGED
@@ -17,7 +17,7 @@ import seaborn as sn
|
|
17 |
import torch
|
18 |
from PIL import Image, ImageDraw, ImageFont
|
19 |
|
20 |
-
from utils.general import user_config_dir, is_chinese, xywh2xyxy, xyxy2xywh
|
21 |
from utils.metrics import fitness
|
22 |
|
23 |
# Settings
|
@@ -68,7 +68,7 @@ class Annotator:
|
|
68 |
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
69 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
|
70 |
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
|
71 |
-
self.pil = pil or not example
|
72 |
if self.pil: # use PIL
|
73 |
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
74 |
self.draw = ImageDraw.Draw(self.im)
|
@@ -80,7 +80,7 @@ class Annotator:
|
|
80 |
|
81 |
def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
|
82 |
# Add one xyxy box to image with label
|
83 |
-
if self.pil or not label
|
84 |
self.draw.rectangle(box, width=self.lw, outline=color) # box
|
85 |
if label:
|
86 |
w, h = self.font.getsize(label) # text width, height
|
|
|
17 |
import torch
|
18 |
from PIL import Image, ImageDraw, ImageFont
|
19 |
|
20 |
+
from utils.general import user_config_dir, is_ascii, is_chinese, xywh2xyxy, xyxy2xywh
|
21 |
from utils.metrics import fitness
|
22 |
|
23 |
# Settings
|
|
|
68 |
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
69 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
|
70 |
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
|
71 |
+
self.pil = pil or not is_ascii(example) or is_chinese(example)
|
72 |
if self.pil: # use PIL
|
73 |
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
74 |
self.draw = ImageDraw.Draw(self.im)
|
|
|
80 |
|
81 |
def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
|
82 |
# Add one xyxy box to image with label
|
83 |
+
if self.pil or not is_ascii(label):
|
84 |
self.draw.rectangle(box, width=self.lw, outline=color) # box
|
85 |
if label:
|
86 |
w, h = self.font.getsize(label) # text width, height
|