Update README.md
Browse files
README.md
CHANGED
|
@@ -191,4 +191,145 @@ run_example(prompt)
|
|
| 191 |
# [('a campfire', (71, 81), [(0.171875, 0.015625, 0.484375, 0.984375)]), ('a hat', (109, 114), [(0.515625, 0.046875, 0.828125, 0.234375)]), ('scarf', (116, 121), [(0.515625, 0.234375, 0.890625, 0.578125)]), ('gloves', (127, 133), [(0.515625, 0.390625, 0.640625, 0.515625)]), ('a pot', (140, 145), [(0.078125, 0.609375, 0.265625, 0.859375)]), ('a cup', (157, 162), [(0.890625, 0.765625, 0.984375, 0.984375)])]
|
| 192 |
|
| 193 |
# <grounding> Describe this image in detail: The image features a snowman sitting by<phrase> a campfire</phrase><object><patch_index_0005><patch_index_1007></object> in the snow. He is wearing<phrase> a hat</phrase><object><patch_index_0048><patch_index_0250></object>,<phrase> scarf</phrase><object><patch_index_0240><patch_index_0604></object>, and<phrase> gloves</phrase><object><patch_index_0400><patch_index_0532></object>, with<phrase> a pot</phrase><object><patch_index_0610><patch_index_0872></object> nearby and<phrase> a cup</phrase><object><patch_index_0796><patch_index_1023></object> nearby. The snowman appears to be enjoying the warmth of the fire, and it appears to have a warm and cozy atmosphere.
|
| 194 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
# [('a campfire', (71, 81), [(0.171875, 0.015625, 0.484375, 0.984375)]), ('a hat', (109, 114), [(0.515625, 0.046875, 0.828125, 0.234375)]), ('scarf', (116, 121), [(0.515625, 0.234375, 0.890625, 0.578125)]), ('gloves', (127, 133), [(0.515625, 0.390625, 0.640625, 0.515625)]), ('a pot', (140, 145), [(0.078125, 0.609375, 0.265625, 0.859375)]), ('a cup', (157, 162), [(0.890625, 0.765625, 0.984375, 0.984375)])]
|
| 192 |
|
| 193 |
# <grounding> Describe this image in detail: The image features a snowman sitting by<phrase> a campfire</phrase><object><patch_index_0005><patch_index_1007></object> in the snow. He is wearing<phrase> a hat</phrase><object><patch_index_0048><patch_index_0250></object>,<phrase> scarf</phrase><object><patch_index_0240><patch_index_0604></object>, and<phrase> gloves</phrase><object><patch_index_0400><patch_index_0532></object>, with<phrase> a pot</phrase><object><patch_index_0610><patch_index_0872></object> nearby and<phrase> a cup</phrase><object><patch_index_0796><patch_index_1023></object> nearby. The snowman appears to be enjoying the warmth of the fire, and it appears to have a warm and cozy atmosphere.
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
## Draw the bounding bboxes of the entities on the image
|
| 197 |
+
|
| 198 |
+
Once you have the `entities`, you can use the following helper function to draw their bounding bboxes on the image:
|
| 199 |
+
|
| 200 |
+
```python
|
| 201 |
+
import cv2
|
| 202 |
+
import numpy as np
|
| 203 |
+
import os
|
| 204 |
+
import requests
|
| 205 |
+
import torch
|
| 206 |
+
import torchvision.transforms as T
|
| 207 |
+
|
| 208 |
+
from PIL import Image
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def is_overlapping(rect1, rect2):
|
| 212 |
+
x1, y1, x2, y2 = rect1
|
| 213 |
+
x3, y3, x4, y4 = rect2
|
| 214 |
+
return not (x2 < x3 or x1 > x4 or y2 < y3 or y1 > y4)
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def draw_entity_boxes_on_image(image, entities, show=False, save_path=None):
|
| 218 |
+
"""_summary_
|
| 219 |
+
Args:
|
| 220 |
+
image (_type_): image or image path
|
| 221 |
+
collect_entity_location (_type_): _description_
|
| 222 |
+
"""
|
| 223 |
+
if isinstance(image, Image.Image):
|
| 224 |
+
image_h = image.height
|
| 225 |
+
image_w = image.width
|
| 226 |
+
image = np.array(image)[:, :, [2, 1, 0]]
|
| 227 |
+
elif isinstance(image, str):
|
| 228 |
+
if os.path.exists(image):
|
| 229 |
+
pil_img = Image.open(image).convert("RGB")
|
| 230 |
+
image = np.array(pil_img)[:, :, [2, 1, 0]]
|
| 231 |
+
image_h = pil_img.height
|
| 232 |
+
image_w = pil_img.width
|
| 233 |
+
else:
|
| 234 |
+
raise ValueError(f"invaild image path, {image}")
|
| 235 |
+
elif isinstance(image, torch.Tensor):
|
| 236 |
+
image_tensor = image.cpu()
|
| 237 |
+
reverse_norm_mean = torch.tensor([0.48145466, 0.4578275, 0.40821073])[:, None, None]
|
| 238 |
+
reverse_norm_std = torch.tensor([0.26862954, 0.26130258, 0.27577711])[:, None, None]
|
| 239 |
+
image_tensor = image_tensor * reverse_norm_std + reverse_norm_mean
|
| 240 |
+
pil_img = T.ToPILImage()(image_tensor)
|
| 241 |
+
image_h = pil_img.height
|
| 242 |
+
image_w = pil_img.width
|
| 243 |
+
image = np.array(pil_img)[:, :, [2, 1, 0]]
|
| 244 |
+
else:
|
| 245 |
+
raise ValueError(f"invaild image format, {type(image)} for {image}")
|
| 246 |
+
|
| 247 |
+
if len(entities) == 0:
|
| 248 |
+
return image
|
| 249 |
+
|
| 250 |
+
new_image = image.copy()
|
| 251 |
+
previous_bboxes = []
|
| 252 |
+
# size of text
|
| 253 |
+
text_size = 1
|
| 254 |
+
# thickness of text
|
| 255 |
+
text_line = 1 # int(max(1 * min(image_h, image_w) / 512, 1))
|
| 256 |
+
box_line = 3
|
| 257 |
+
(c_width, text_height), _ = cv2.getTextSize("F", cv2.FONT_HERSHEY_COMPLEX, text_size, text_line)
|
| 258 |
+
base_height = int(text_height * 0.675)
|
| 259 |
+
text_offset_original = text_height - base_height
|
| 260 |
+
text_spaces = 3
|
| 261 |
+
|
| 262 |
+
for entity_name, (start, end), bboxes in entities:
|
| 263 |
+
for (x1_norm, y1_norm, x2_norm, y2_norm) in bboxes:
|
| 264 |
+
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)
|
| 265 |
+
# draw bbox
|
| 266 |
+
# random color
|
| 267 |
+
color = tuple(np.random.randint(0, 255, size=3).tolist())
|
| 268 |
+
new_image = cv2.rectangle(new_image, (orig_x1, orig_y1), (orig_x2, orig_y2), color, box_line)
|
| 269 |
+
|
| 270 |
+
l_o, r_o = box_line // 2 + box_line % 2, box_line // 2 + box_line % 2 + 1
|
| 271 |
+
|
| 272 |
+
x1 = orig_x1 - l_o
|
| 273 |
+
y1 = orig_y1 - l_o
|
| 274 |
+
|
| 275 |
+
if y1 < text_height + text_offset_original + 2 * text_spaces:
|
| 276 |
+
y1 = orig_y1 + r_o + text_height + text_offset_original + 2 * text_spaces
|
| 277 |
+
x1 = orig_x1 + r_o
|
| 278 |
+
|
| 279 |
+
# add text background
|
| 280 |
+
(text_width, text_height), _ = cv2.getTextSize(f" {entity_name}", cv2.FONT_HERSHEY_COMPLEX, text_size, text_line)
|
| 281 |
+
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
|
| 282 |
+
|
| 283 |
+
for prev_bbox in previous_bboxes:
|
| 284 |
+
while is_overlapping((text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2), prev_bbox):
|
| 285 |
+
text_bg_y1 += (text_height + text_offset_original + 2 * text_spaces)
|
| 286 |
+
text_bg_y2 += (text_height + text_offset_original + 2 * text_spaces)
|
| 287 |
+
y1 += (text_height + text_offset_original + 2 * text_spaces)
|
| 288 |
+
|
| 289 |
+
if text_bg_y2 >= image_h:
|
| 290 |
+
text_bg_y1 = max(0, image_h - (text_height + text_offset_original + 2 * text_spaces))
|
| 291 |
+
text_bg_y2 = image_h
|
| 292 |
+
y1 = image_h
|
| 293 |
+
break
|
| 294 |
+
|
| 295 |
+
alpha = 0.5
|
| 296 |
+
for i in range(text_bg_y1, text_bg_y2):
|
| 297 |
+
for j in range(text_bg_x1, text_bg_x2):
|
| 298 |
+
if i < image_h and j < image_w:
|
| 299 |
+
if j < text_bg_x1 + 1.35 * c_width:
|
| 300 |
+
# original color
|
| 301 |
+
bg_color = color
|
| 302 |
+
else:
|
| 303 |
+
# white
|
| 304 |
+
bg_color = [255, 255, 255]
|
| 305 |
+
new_image[i, j] = (alpha * new_image[i, j] + (1 - alpha) * np.array(bg_color)).astype(np.uint8)
|
| 306 |
+
|
| 307 |
+
cv2.putText(
|
| 308 |
+
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
|
| 309 |
+
)
|
| 310 |
+
# previous_locations.append((x1, y1))
|
| 311 |
+
previous_bboxes.append((text_bg_x1, text_bg_y1, text_bg_x2, text_bg_y2))
|
| 312 |
+
|
| 313 |
+
pil_image = Image.fromarray(new_image[:, :, [2, 1, 0]])
|
| 314 |
+
if save_path:
|
| 315 |
+
pil_image.save(save_path)
|
| 316 |
+
if show:
|
| 317 |
+
pil_image.show()
|
| 318 |
+
|
| 319 |
+
return new_image
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
# (The same image from the previous code example)
|
| 323 |
+
url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.png"
|
| 324 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 325 |
+
|
| 326 |
+
# From the previous code example
|
| 327 |
+
entities = [('a snowman', (12, 21), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('a fire', (41, 47), [(0.171875, 0.015625, 0.484375, 0.890625)])]
|
| 328 |
+
|
| 329 |
+
# Draw the bounding bboxes
|
| 330 |
+
draw_entity_boxes_on_image(image, entities, show=True)
|
| 331 |
+
```
|
| 332 |
+
|
| 333 |
+
Here is the annotated image:
|
| 334 |
+
|
| 335 |
+
<a href="https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" target="_blank"><img src="https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/annotated_snowman.jpg" width="500"></a>
|