Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ from bounding_box import bounding_box as bb

Then, just add the bounding box on an image.
```python
bb.add(image, left, top, right, bottom, label, color)
bb.add(image, left, top, right, bottom, label, color, size)
```

This method takes 5 mandatory parameters:
Expand All @@ -70,6 +70,7 @@ If `label` is specified and `color` is not, then a color depending
on the `label` is randomly chosen.
If neither `color` and `label` is specified then the bounding box
color is defaulted to `green`.
- `size`: A integer representing the label's font size. Defaults to 15px.

## Examples
The script to plot exemples of this **README** is available
Expand Down
9 changes: 5 additions & 4 deletions bounding_box/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@

_FONT_PATH = _os.path.join(_LOC, "Ubuntu-B.ttf")
_FONT_HEIGHT = 15
_FONT = ImageFont.truetype(_FONT_PATH, _FONT_HEIGHT)

def _rgb_to_bgr(color):
return list(reversed(color))

def _color_image(image, font_color, background_color):
return background_color + (font_color - background_color) * image / 255

def _get_label_image(text, font_color_tuple_bgr, background_color_tuple_bgr):
def _get_label_image(text, font_color_tuple_bgr, background_color_tuple_bgr, size=_FONT_HEIGHT):
_FONT = ImageFont.truetype(_FONT_PATH, size)

text_image = _FONT.getmask(text)
shape = list(reversed(text_image.size))
bw_image = np.array(text_image).reshape(shape)
Expand All @@ -58,7 +59,7 @@ def _get_label_image(text, font_color_tuple_bgr, background_color_tuple_bgr):

return np.concatenate(image).transpose(1, 2, 0)

def add(image, left, top, right, bottom, label=None, color=None):
def add(image, left, top, right, bottom, label=None, color=None, size=_FONT_HEIGHT):
if type(image) is not _np.ndarray:
raise TypeError("'image' parameter must be a numpy.ndarray")
try:
Expand Down Expand Up @@ -92,7 +93,7 @@ def add(image, left, top, right, bottom, label=None, color=None):
if label:
_, image_width, _ = image.shape

label_image = _get_label_image(label, color_text, color)
label_image = _get_label_image(label, color_text, color, size)
label_height, label_width, _ = label_image.shape

rectangle_height, rectangle_width = 1 + label_height, 1 + label_width
Expand Down
7 changes: 7 additions & 0 deletions docs/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,12 @@ def main():
bb.add(image, 864, 51, 959, 266, "Male", "blue")
show_and_save("POBB", image, out_path)

in_path = os.path.join("docs", "images", "globe.jpg")
out_path = os.path.join("docs", "images", "globe_bb.png")
image = cv2.imread(in_path, cv2.IMREAD_COLOR)
bb.add(image, 721, 312, 721 + 92, 312 + 32, "Big Text", "olive", 30)
bb.add(image, 824, 893, 824 + 125, 893 + 89, "Small Text", "red", 15)
show_and_save("Globe and a Magnifying Glass", image, out_path)

if __name__ == "__main__":
main()
Binary file added docs/images/globe.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/globe_bb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.