Skip to content

Commit

Permalink
Fixed Classify offsets (ultralytics#9155)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher authored and Clay Januhowski committed Sep 8, 2022
1 parent 3a708d2 commit 0b11fbb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion classify/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0b11fbb

Please sign in to comment.