Create draw_bboxes.py
Browse files- draw_bboxes.py +117 -0
draw_bboxes.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
import torchvision.transforms as T
|
6 |
+
import cv2
|
7 |
+
import requests
|
8 |
+
|
9 |
+
|
10 |
+
def is_overlapping(rect1, rect2):
|
11 |
+
x1, y1, x2, y2 = rect1
|
12 |
+
x3, y3, x4, y4 = rect2
|
13 |
+
return not (x2 < x3 or x1 > x4 or y2 < y3 or y1 > y4)
|
14 |
+
|
15 |
+
|
16 |
+
def draw_entity_boxes_on_image(image, entities):
|
17 |
+
"""_summary_
|
18 |
+
|
19 |
+
Args:
|
20 |
+
image (_type_): image or image path
|
21 |
+
collect_entity_location (_type_): _description_
|
22 |
+
"""
|
23 |
+
if isinstance(image, Image.Image):
|
24 |
+
image_h = image.height
|
25 |
+
image_w = image.width
|
26 |
+
image = np.array(image)[:, :, [2, 1, 0]]
|
27 |
+
elif isinstance(image, str):
|
28 |
+
if os.path.exists(image):
|
29 |
+
pil_img = Image.open(image).convert("RGB")
|
30 |
+
image = np.array(pil_img)[:, :, [2, 1, 0]]
|
31 |
+
image_h = pil_img.height
|
32 |
+
image_w = pil_img.width
|
33 |
+
else:
|
34 |
+
raise ValueError(f"invaild image path, {image}")
|
35 |
+
elif isinstance(image, torch.Tensor):
|
36 |
+
# pdb.set_trace()
|
37 |
+
image_tensor = image.cpu()
|
38 |
+
reverse_norm_mean = torch.tensor([0.48145466, 0.4578275, 0.40821073])[:, None, None]
|
39 |
+
reverse_norm_std = torch.tensor([0.26862954, 0.26130258, 0.27577711])[:, None, None]
|
40 |
+
image_tensor = image_tensor * reverse_norm_std + reverse_norm_mean
|
41 |
+
pil_img = T.ToPILImage()(image_tensor)
|
42 |
+
image_h = pil_img.height
|
43 |
+
image_w = pil_img.width
|
44 |
+
image = np.array(pil_img)[:, :, [2, 1, 0]]
|
45 |
+
else:
|
46 |
+
raise ValueError(f"invaild image format, {type(image)} for {image}")
|
47 |
+
|
48 |
+
if len(entities) == 0:
|
49 |
+
return image
|
50 |
+
|
51 |
+
new_image = image.copy()
|
52 |
+
previous_bboxes = []
|
53 |
+
# size of text
|
54 |
+
text_size = 2
|
55 |
+
# thickness of text
|
56 |
+
text_line = 1 # int(max(1 * min(image_h, image_w) / 512, 1))
|
57 |
+
box_line = 3
|
58 |
+
(c_width, text_height), _ = cv2.getTextSize("F", cv2.FONT_HERSHEY_COMPLEX, text_size, text_line)
|
59 |
+
base_height = int(text_height * 0.675)
|
60 |
+
text_offset_original = text_height - base_height
|
61 |
+
text_spaces = 3
|
62 |
+
|
63 |
+
for entity_name, (start, end), bboxes in entities:
|
64 |
+
for (x1_norm, y1_norm, x2_norm, y2_norm) in bboxes:
|
65 |
+
orig_x1, orig_y1, orig_x2, orig_y2 = int(x1_norm * image_w), int(y1_norm * image_h), int(x2_norm * image_w), int(y2_norm * image_h)
|
66 |
+
# draw bbox
|
67 |
+
# random color
|
68 |
+
color = tuple(np.random.randint(0, 255, size=3).tolist())
|
69 |
+
new_image = cv2.rectangle(new_image, (orig_x1, orig_y1), (orig_x2, orig_y2), color, box_line)
|
70 |
+
|
71 |
+
l_o, r_o = box_line // 2 + box_line % 2, box_line // 2 + box_line % 2 + 1
|
72 |
+
|
73 |
+
x1 = orig_x1 - l_o
|
74 |
+
y1 = orig_y1 - l_o
|
75 |
+
|
76 |
+
if y1 < text_height + text_offset_original + 2 * text_spaces:
|
77 |
+
y1 = orig_y1 + r_o + text_height + text_offset_original + 2 * text_spaces
|
78 |
+
x1 = orig_x1 + r_o
|
79 |
+
|
80 |
+
# add text background
|
81 |
+
(text_width, text_height), _ = cv2.getTextSize(f" {entity_name}", cv2.FONT_HERSHEY_COMPLEX, text_size, text_line)
|
82 |
+
text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2 = x1, y1 - (text_height + text_offset_original + 2 * text_spaces), x1 + text_width, y1
|
83 |
+
|
84 |
+
for prev_bbox in previous_bboxes:
|
85 |
+
while is_overlapping((text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2), prev_bbox):
|
86 |
+
text_bg_y1 += (text_height + text_offset_original + 2 * text_spaces)
|
87 |
+
text_bg_y2 += (text_height + text_offset_original + 2 * text_spaces)
|
88 |
+
y1 += (text_height + text_offset_original + 2 * text_spaces)
|
89 |
+
|
90 |
+
if text_bg_y2 >= image_h:
|
91 |
+
text_bg_y1 = max(0, image_h - (text_height + text_offset_original + 2 * text_spaces))
|
92 |
+
text_bg_y2 = image_h
|
93 |
+
y1 = image_h
|
94 |
+
break
|
95 |
+
|
96 |
+
alpha = 0.5
|
97 |
+
for i in range(text_bg_y1, text_bg_y2):
|
98 |
+
for j in range(text_bg_x1, text_bg_x2):
|
99 |
+
if i < image_h and j < image_w:
|
100 |
+
if j < text_bg_x1 + 1.35 * c_width:
|
101 |
+
# original color
|
102 |
+
bg_color = color
|
103 |
+
else:
|
104 |
+
# white
|
105 |
+
bg_color = [255, 255, 255]
|
106 |
+
new_image[i, j] = (alpha * new_image[i, j] + (1 - alpha) * np.array(bg_color)).astype(np.uint8)
|
107 |
+
|
108 |
+
cv2.putText(
|
109 |
+
new_image, f" {entity_name}", (x1, y1 - text_offset_original - 1 * text_spaces), cv2.FONT_HERSHEY_COMPLEX, text_size, (0, 0, 0), text_line, cv2.LINE_AA
|
110 |
+
)
|
111 |
+
# previous_locations.append((x1, y1))
|
112 |
+
previous_bboxes.append((text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2))
|
113 |
+
|
114 |
+
pil_image = Image.fromarray(new_image[:, :, [2, 1, 0]])
|
115 |
+
pil_image.save("new_snowman.jpg")
|
116 |
+
|
117 |
+
return new_image
|