Skip to content

Commit

Permalink
reset head
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher committed Feb 19, 2021
1 parent db28ce6 commit 47faf95
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import seaborn as sns
import torch
import yaml
from PIL import Image, ImageDraw
from PIL import Image, ImageDraw, ImageFont
from scipy.signal import butter, filtfilt

from utils.general import xywh2xyxy, xyxy2xywh
Expand Down Expand Up @@ -68,6 +68,20 @@ def plot_one_box(x, img, color=None, label=None, line_thickness=None):
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)


def plot_one_box_PIL(box, img, color=None, label=None, line_thickness=None):
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
line_thickness = line_thickness or max(int(min(img.size) / 200), 2)
draw.rectangle(box, width=line_thickness, outline=tuple(color)) # plot
if label:
fontsize = max(round(max(img.size) / 40), 12)
font = ImageFont.truetype("Arial.ttf", fontsize)
txt_width, txt_height = font.getsize(label)
draw.rectangle([box[0], box[1] - txt_height + 4, box[0] + txt_width, box[1]], fill=tuple(color))
draw.text((box[0], box[1] - txt_height + 1), label, fill=(255, 255, 255), font=font)
return np.asarray(img)


def plot_wh_methods(): # from utils.plots import *; plot_wh_methods()
# Compares the two methods for width-height anchor multiplication
# https://github.com/ultralytics/yolov3/issues/168
Expand Down

4 comments on commit 47faf95

@iceisfun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From Docker arial.ttf is missing

While modifying plot_one_box_PIL would it be possible to make it so the label is always inside the bounds of the image

Detections on cameras going off the edge of the frame have their label going off the edge of the frame too, its a small issue but you seem to have txt_width and txt_height, if they overflow the image the label could be drawn inside the image?

root@container:/usr/src/app# ls -al /usr/share/fonts/
total 16
drwxr-xr-x 3 root root 4096 Nov 26 08:51 .
drwxr-xr-x 1 root root 4096 Dec 23 05:09 ..
-rw-r--r-- 1 root root 36 Nov 26 08:52 .uuid
drwxr-xr-x 3 root root 4096 Nov 26 08:51 truetype
root@container:/usr/src/app# ls -al /usr/share/fonts/truetype/
total 16
drwxr-xr-x 3 root root 4096 Nov 26 08:51 .
drwxr-xr-x 3 root root 4096 Nov 26 08:51 ..
-rw-r--r-- 1 root root 36 Nov 26 08:52 .uuid
drwxr-xr-x 2 root root 4096 Nov 26 08:51 dejavu

OSError: cannot open resource
cannot open resource
Traceback (most recent call last):
File "/usr/src/app/MultiModel.py", line 267, in Mark
plot_one_box_PIL(xyxy, img, label=f'{ClassName} {int(Percent * 1000)/10}%', color=Color, line_thickness=2)
File "/usr/src/app/utils/plots.py", line 78, in plot_one_box_PIL
font = ImageFont.truetype("Arial.ttf", fontsize)
File "/opt/conda/lib/python3.8/site-packages/PIL/ImageFont.py", line 852, in truetype
return freetype(font)
File "/opt/conda/lib/python3.8/site-packages/PIL/ImageFont.py", line 849, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/opt/conda/lib/python3.8/site-packages/PIL/ImageFont.py", line 209, in init
self.font = core.getfont(
OSError: cannot open resource

@glenn-jocher
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iceisfun interesting. Yes I think the ttf font availability is a bit of problem with the PIL plotting. We may need a try except statement here that falls back to the PIL default font on load failures. https://pillow.readthedocs.io/en/3.0.x/reference/ImageFont.html#PIL.ImageFont.load_default

We created the PIL plotting function mainly as a placeholder now. It looks a little nicer when plotting results, but it's also slower than cv2, so for now we've kept the cv2 plots as default.

About the off-edge detections, I assume you mean the text/text boxes are off image and not the actual detection boxes (which are clipped) right? Is this only a PIL plotting issue or are the cv2 plots also affected?

@iceisfun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just use the cv2 plots and yes the text boxes are off the edge of the image, it should probably be something like

textx + textw > imgw:
textx = imgw - textw

so when the text overflows the image it just slides to the left

@glenn-jocher
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iceisfun good suggestion! Can you submit a PR with your fix please?

Please sign in to comment.