diff --git a/classify/predict.py b/classify/predict.py index b33b5bcc9933..937704d0f080 100644 --- a/classify/predict.py +++ b/classify/predict.py @@ -136,7 +136,7 @@ def run( # Write results if save_img or view_img: # Add bbox to image text = '\n'.join(f'{prob[j]:.2f} {names[j]}' for j in top5i) - annotator.text((64, 64), text, txt_color=(255, 255, 255)) + annotator.text((32, 32), text, txt_color=(255, 255, 255)) # Stream results im0 = annotator.result() diff --git a/utils/plots.py b/utils/plots.py index 2aa163268336..0f322b6b5844 100644 --- a/utils/plots.py +++ b/utils/plots.py @@ -117,10 +117,12 @@ def rectangle(self, xy, fill=None, outline=None, width=1): # Add rectangle to image (PIL-only) self.draw.rectangle(xy, fill, outline, width) - def text(self, xy, text, txt_color=(255, 255, 255)): + def text(self, xy, text, txt_color=(255, 255, 255), anchor='top'): # Add text to image (PIL-only) - w, h = self.font.getsize(text) # text width, height - self.draw.text((xy[0], xy[1] - h + 1), text, fill=txt_color, font=self.font) + if anchor == 'bottom': # start y from font bottom + w, h = self.font.getsize(text) # text width, height + xy[1] += 1 - h + self.draw.text(xy, text, fill=txt_color, font=self.font) def result(self): # Return annotated image as array @@ -222,7 +224,7 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max 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 if paths: - annotator.text((x + 5, y + 5 + h), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220)) # filenames + annotator.text((x + 5, y + 5), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220)) # filenames if len(targets) > 0: ti = targets[targets[:, 0] == i] # image targets boxes = xywh2xyxy(ti[:, 2:6]).T