Create image_display_with bbox.py
Browse files- image_display_with bbox.py +35 -0
image_display_with bbox.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
+
|
| 3 |
+
def display_bbox_and_label(image, bbox, label):
|
| 4 |
+
# Open the image
|
| 5 |
+
# image = Image.open(image_path)
|
| 6 |
+
|
| 7 |
+
# Initialize the drawing context
|
| 8 |
+
draw = ImageDraw.Draw(image)
|
| 9 |
+
|
| 10 |
+
# Define font and font size
|
| 11 |
+
# font = ImageFont.load_default()
|
| 12 |
+
font = ImageFont.load_default().font_variant(size=18)
|
| 13 |
+
|
| 14 |
+
# Draw bounding box on the image
|
| 15 |
+
draw.rectangle(bbox, outline="red", width=5)
|
| 16 |
+
|
| 17 |
+
# Draw label text on the image
|
| 18 |
+
draw.text((bbox[0], bbox[1] - 15), label, fill="green", font=font)
|
| 19 |
+
|
| 20 |
+
# Display the image
|
| 21 |
+
return image
|
| 22 |
+
|
| 23 |
+
# Example usage
|
| 24 |
+
# image_path = 'example_image.jpg' # Path to your image file
|
| 25 |
+
# bounding_box = (100, 100, 300, 300) # Define bounding box as (x_min, y_min, x_max, y_max)
|
| 26 |
+
# label = "Object" # Define label for the bounding box
|
| 27 |
+
# display_bbox_and_label(image_path, bounding_box, label)
|
| 28 |
+
|
| 29 |
+
layout_results = layout_predictions[0]
|
| 30 |
+
for i in range(len(layout_results.bboxes)):
|
| 31 |
+
b_box = layout_results.bboxes[i].bbox
|
| 32 |
+
label = layout_results.bboxes[i].label
|
| 33 |
+
image = display_bbox_and_label(image, b_box, label)
|
| 34 |
+
# display_bbox_and_label(image, layout_results.bboxes[3].bbox, layout_results.bboxes[3].label)
|
| 35 |
+
# layout_predictions
|