Charles Kabui commited on
Commit
a84ccd1
·
1 Parent(s): edf3369

tests and analysis

Browse files
data/preview.ipynb CHANGED
@@ -214,8 +214,8 @@
214
  " label_rectangle_color='black',\n",
215
  " label_text_size=14,\n",
216
  " label_text_padding=5,\n",
217
- " label_rectangle_left_padding=0,\n",
218
- " label_rectangle_top_padding=0,)\n",
219
  "\n",
220
  "def format_name(file_name: str):\n",
221
  " name_, ext_ = file_name.split('.')\n",
 
214
  " label_rectangle_color='black',\n",
215
  " label_text_size=14,\n",
216
  " label_text_padding=5,\n",
217
+ " label_rectangle_left_margin=0,\n",
218
+ " label_rectangle_top_margin=0,)\n",
219
  "\n",
220
  "def format_name(file_name: str):\n",
221
  " name_, ext_ = file_name.split('.')\n",
utils/visualize_bboxes_on_image.py CHANGED
@@ -6,8 +6,19 @@ import requests
6
  from typing import List
7
  from functools import cache
8
 
 
 
 
 
 
 
 
 
 
 
 
9
  @cache
10
- def get_font(path_or_url: str = 'https://github.com/googlefonts/roboto/raw/main/src/hinted/Roboto-Regular.ttf', size: int = 10):
11
  if urlparse(path_or_url).scheme in ["http", "https"]: # Online
12
  return ImageFont.truetype(requests.get(path_or_url, stream=True).raw, size=size)
13
  else: # Local
@@ -17,15 +28,15 @@ def visualize_bboxes_on_image(
17
  image: Image.Image,
18
  bboxes: List[List[int]],
19
  titles: List[str] = None,
20
- width = 2,
21
- bbox_color="red",
22
- label_text_color="black",
23
- label_rectangle_color="red",
24
- convert_to_x0y0x1y1 = None,
25
- label_text_padding = 2,
26
- label_rectangle_left_padding=10,
27
- label_rectangle_top_padding=10,
28
- label_text_size = 10) -> Image.Image:
29
  '''
30
  Visualize bounding boxes on an image
31
  Args:
@@ -42,7 +53,8 @@ def visualize_bboxes_on_image(
42
  label_rectangle_top_padding: Top padding of the label rectangle
43
  label_text_size: Font size of the label text
44
  Returns:
45
- Image: Image with bounding boxes'''
 
46
  image = image.copy().convert("RGB")
47
  draw = ImageDraw.Draw(image)
48
  font = get_font(size = label_text_size)
@@ -51,15 +63,43 @@ def visualize_bboxes_on_image(
51
  x0, y0, x1, y1 = convert_to_x0y0x1y1(bbox) if convert_to_x0y0x1y1 is not None else bbox
52
  draw.rectangle([x0, y0, x1, y1], outline=bbox_color, width=width)
53
  if title is not None:
54
- text_position = (x0 + label_rectangle_left_padding, y0 - label_rectangle_top_padding)
55
- text_bbox_left, text_bbox_top, text_bbox_right, text_bbox_bottom = draw.textbbox(text_position, title, font=font)
56
- draw.rectangle(
57
- (
58
- text_bbox_left - label_text_padding,
59
- text_bbox_top - label_text_padding,
60
- text_bbox_right + label_text_padding,
61
- text_bbox_bottom + label_text_padding
62
- ),
63
- fill=label_rectangle_color)
64
- draw.text(text_position, title, font=font, fill=label_text_color)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  return image
 
6
  from typing import List
7
  from functools import cache
8
 
9
+ DEFAULTS = {
10
+ 'width': 2,
11
+ 'bbox_color': "red",
12
+ 'label_text_color': "black",
13
+ 'label_rectangle_color': "red",
14
+ 'label_text_padding': 0,
15
+ 'label_rectangle_left_margin': 0,
16
+ 'label_rectangle_top_margin': 0,
17
+ 'label_text_size': 12
18
+ }
19
+
20
  @cache
21
+ def get_font(path_or_url: str = 'https://github.com/googlefonts/roboto/raw/main/src/hinted/Roboto-Regular.ttf', size: int = DEFAULTS['label_text_size']):
22
  if urlparse(path_or_url).scheme in ["http", "https"]: # Online
23
  return ImageFont.truetype(requests.get(path_or_url, stream=True).raw, size=size)
24
  else: # Local
 
28
  image: Image.Image,
29
  bboxes: List[List[int]],
30
  titles: List[str] = None,
31
+ width = DEFAULTS["width"],
32
+ bbox_color = DEFAULTS["bbox_color"],
33
+ label_text_color = DEFAULTS["label_text_color"],
34
+ label_rectangle_color = DEFAULTS["label_rectangle_color"],
35
+ label_text_padding = DEFAULTS["label_text_padding"],
36
+ label_rectangle_left_margin = DEFAULTS["label_rectangle_left_margin"],
37
+ label_rectangle_top_margin = DEFAULTS['label_rectangle_top_margin'],
38
+ label_text_size = DEFAULTS["label_text_size"],
39
+ convert_to_x0y0x1y1 = None) -> Image.Image:
40
  '''
41
  Visualize bounding boxes on an image
42
  Args:
 
53
  label_rectangle_top_padding: Top padding of the label rectangle
54
  label_text_size: Font size of the label text
55
  Returns:
56
+ Image: Image with bounding boxes
57
+ '''
58
  image = image.copy().convert("RGB")
59
  draw = ImageDraw.Draw(image)
60
  font = get_font(size = label_text_size)
 
63
  x0, y0, x1, y1 = convert_to_x0y0x1y1(bbox) if convert_to_x0y0x1y1 is not None else bbox
64
  draw.rectangle([x0, y0, x1, y1], outline=bbox_color, width=width)
65
  if title is not None:
66
+ draw_text_on_image(
67
+ draw,
68
+ [x0, y0],
69
+ title,
70
+ label_text_color,
71
+ label_rectangle_color,
72
+ label_text_padding,
73
+ label_rectangle_left_margin,
74
+ label_rectangle_top_margin,
75
+ label_text_size,
76
+ font)
77
+ return image
78
+
79
+ def draw_text_on_image(
80
+ image_or_draw: Image.Image | ImageDraw.ImageDraw,
81
+ text_position_xy: List[int],
82
+ title: str,
83
+ label_text_color = DEFAULTS["label_text_color"],
84
+ label_rectangle_color = DEFAULTS["label_rectangle_color"],
85
+ label_text_padding = DEFAULTS["label_text_padding"],
86
+ label_rectangle_left_margin = DEFAULTS["label_rectangle_left_margin"],
87
+ label_rectangle_top_margin = DEFAULTS['label_rectangle_top_margin'],
88
+ label_text_size = DEFAULTS["label_text_size"],
89
+ font: ImageFont.FreeTypeFont = None) -> Image.Image:
90
+ is_image = isinstance(image_or_draw, Image.Image)
91
+ image = image_or_draw.copy().convert("RGB") if is_image else None
92
+ font = font or get_font(size = label_text_size)
93
+ x0, y0 = text_position_xy
94
+ text_position = (x0 - label_rectangle_left_margin + label_text_padding, y0 - label_rectangle_top_margin + label_text_padding)
95
+ draw = ImageDraw.Draw(image) if is_image else image_or_draw
96
+ text_bbox_left, text_bbox_top, text_bbox_right, text_bbox_bottom = draw.textbbox(text_position, title, font=font)
97
+ xy = [
98
+ text_position[0] - label_text_padding,
99
+ text_position[1] - label_text_padding,
100
+ text_bbox_right + label_text_padding + label_text_padding,
101
+ text_bbox_bottom + label_text_padding + label_text_padding
102
+ ]
103
+ draw.rectangle(xy, fill = label_rectangle_color)
104
+ draw.text(text_position, title, font=font, fill=label_text_color)
105
  return image