Trevor McDonald
Trevor McDonald

Reputation: 31

Customizing TF Object Detection bounding box thickness & label font size

Running predictions with https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb.

Would like to customize label font size & bounding box thickness as my label text & bbox are too thick in image detections.

Cannot see detections!

Thank you for any help! If you have done this yourself please pass along your learnings! :)

Upvotes: 3

Views: 7418

Answers (3)

LjuboM
LjuboM

Reputation: 41

To change font size:

In file models/research/object_detection/utils/visualization_utils.py starting from line 202:

Try:

  font = ImageFont.truetype('arial.ttf', 24) 
except IOError:
  font = ImageFont.load_default()

Here we just need to change number 24 to desired font size.

Upvotes: 4

Mike Wise
Mike Wise

Reputation: 22807

Ubuntu 16.04 seemingly does not ship with the arial.ttf font - and this is unfortunately what vis_util.visualize_boxes_and_labels_on_image_array uses by default and it is not configurable except by changing the python code. When it can't find that font it uses a default bitmap font, however the resolution is too low for many purposes, especially at low DPI settings.

However you can workaround this problem in a number of ways:

  • Install the Microsoft core fonts with: sudo apt-get install ttf-mscorefonts-installer (this however did not work for me - not sure why)
  • Or copy the very similar font DejaVuSans.ttf to a file with the name arial.ttf in the directory /usr/share/fonts/truetype/dejavu
  • Or copy DejaVuSans.ttf to a file with the name arial.ttf in your object_detection directory (assuming you are running your code there)

Then you can visualize your boxes and labels with DPI=100 and still read the font.

Before - 100 DPI with the default bitmap font:

enter image description here

After - 100 DPI with arial.ttf

enter image description here

Upvotes: 3

g.smoother
g.smoother

Reputation: 135

You can change the bounding boxes thickness by changing the line_thickness parameter in the visualize_boxes_and_labels_on_image_arrayas follows:

vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          np.squeeze(boxes),
          np.squeeze(classes).astype(np.int32),
          np.squeeze(scores),
          category_index,
          use_normalized_coordinates=True,
          line_thickness=8)

Upvotes: 4

Related Questions