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['']['bboxes'], bounding_boxes['']['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