Spaces:
Running
Running
File size: 1,540 Bytes
b7a2f31 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import base64
from PIL import ImageDraw, ImageFont
from io import BytesIO
def image_to_base64(image):
buffered = BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str
def draw_bounding_boxes(image, bounding_boxes, outline_color="red", line_width=2):
draw = ImageDraw.Draw(image)
for box in bounding_boxes:
xmin, ymin, xmax, ymax = box
draw.rectangle([xmin, ymin, xmax, ymax], outline=outline_color, width=line_width)
return image
def florence_draw_bboxes(image, bounding_boxes, outline_color="red", line_width=2):
draw = ImageDraw.Draw(image)
#font = ImageFont.truetype("sans-serif.ttf", 16)
for bbox, label in zip(bounding_boxes['<OD>']['bboxes'], bounding_boxes['<OD>']['labels']):
x1, y1, x2, y2 = bbox
draw.rectangle([x1, y1, x2, y2], outline=outline_color, width=line_width)
draw.text((x1, x2), label, (255,255,255))
return image
def rescale_bounding_boxes(bounding_boxes, original_width, original_height, scaled_width=1000, scaled_height=1000):
x_scale = original_width / scaled_width
y_scale = original_height / scaled_height
rescaled_boxes = []
for box in bounding_boxes:
xmin, ymin, xmax, ymax = box
rescaled_box = [
xmin * x_scale,
ymin * y_scale,
xmax * x_scale,
ymax * y_scale
]
rescaled_boxes.append(rescaled_box)
return rescaled_boxes |