Commit
•
ce7fa81
1
Parent(s):
dad8660
Avoid out-of-image class labels (#4902)
Browse files* Avoid out-of-image class labels
* Update plots.py
* Cleanup
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
- utils/plots.py +15 -11
utils/plots.py
CHANGED
@@ -73,7 +73,6 @@ class Annotator:
|
|
73 |
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
74 |
self.draw = ImageDraw.Draw(self.im)
|
75 |
self.font = check_font(font, size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
|
76 |
-
self.fh = self.font.getsize('a')[1] - 3 # font height
|
77 |
else: # use cv2
|
78 |
self.im = im
|
79 |
self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
|
@@ -83,20 +82,25 @@ class Annotator:
|
|
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
|
87 |
-
|
|
|
|
|
|
|
|
|
88 |
# self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls') # for PIL>8.0
|
89 |
-
self.draw.text((box[0], box[1] - h), label, fill=txt_color, font=self.font)
|
90 |
else: # cv2
|
91 |
-
|
92 |
-
cv2.rectangle(self.im,
|
93 |
if label:
|
94 |
tf = max(self.lw - 1, 1) # font thickness
|
95 |
-
w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0]
|
96 |
-
|
97 |
-
|
98 |
-
cv2.
|
99 |
-
|
|
|
100 |
|
101 |
def rectangle(self, xy, fill=None, outline=None, width=1):
|
102 |
# Add rectangle to image (PIL-only)
|
|
|
73 |
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
|
74 |
self.draw = ImageDraw.Draw(self.im)
|
75 |
self.font = check_font(font, size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
|
|
|
76 |
else: # use cv2
|
77 |
self.im = im
|
78 |
self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
|
|
|
82 |
if self.pil or not is_ascii(label):
|
83 |
self.draw.rectangle(box, width=self.lw, outline=color) # box
|
84 |
if label:
|
85 |
+
w, h = self.font.getsize(label) # text width, height
|
86 |
+
outside = box[1] - h >= 0 # label fits outside box
|
87 |
+
self.draw.rectangle([box[0],
|
88 |
+
box[1] - h if outside else box[1],
|
89 |
+
box[0] + w + 1,
|
90 |
+
box[1] + 1 if outside else box[1] + h + 1], fill=color)
|
91 |
# self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls') # for PIL>8.0
|
92 |
+
self.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)
|
93 |
else: # cv2
|
94 |
+
p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
|
95 |
+
cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)
|
96 |
if label:
|
97 |
tf = max(self.lw - 1, 1) # font thickness
|
98 |
+
w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0] # text width, height
|
99 |
+
outside = p1[1] - h - 3 >= 0 # label fits outside box
|
100 |
+
p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
|
101 |
+
cv2.rectangle(self.im, p1, p2, color, -1, cv2.LINE_AA) # filled
|
102 |
+
cv2.putText(self.im, label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2), 0, self.lw / 3, txt_color,
|
103 |
+
thickness=tf, lineType=cv2.LINE_AA)
|
104 |
|
105 |
def rectangle(self, xy, fill=None, outline=None, width=1):
|
106 |
# Add rectangle to image (PIL-only)
|