问题
Running predictions with https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb.
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
Linux Ubuntu 16.04
Would like to customize label font size & bounding box thickness as my label text & bbox are too thick in image detections.

Thank you for any help! If you have done this yourself please pass along your learnings! :)
回答1:
You can change the bounding boxes thickness by changing the line_thickness
parameter in the visualize_boxes_and_labels_on_image_array
as 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)
回答2:
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 namearial.ttf
in the directory/usr/share/fonts/truetype/dejavu
- Or copy
DejaVuSans.ttf
to a file with the namearial.ttf
in yourobject_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:
After - 100 DPI with arial.ttf
回答3:
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.
来源:https://stackoverflow.com/questions/47242183/customizing-tf-object-detection-bounding-box-thickness-label-font-size