(
image: Image.Image,
bboxes: List[Dict[str, Any]],
colors: Dict[str, Tuple[int, int, int]],
use_tracking_label: bool = False,
)
| 3488 | |
| 3489 | |
| 3490 | def _plot_counting( |
| 3491 | image: Image.Image, |
| 3492 | bboxes: List[Dict[str, Any]], |
| 3493 | colors: Dict[str, Tuple[int, int, int]], |
| 3494 | use_tracking_label: bool = False, |
| 3495 | ) -> Image.Image: |
| 3496 | width, height = image.size |
| 3497 | fontsize = max(12, int(min(width, height) / 40)) |
| 3498 | draw = ImageDraw.Draw(image) |
| 3499 | font = ImageFont.truetype( |
| 3500 | str(resources.files("vision_agent.fonts").joinpath("default_font_ch_en.ttf")), |
| 3501 | fontsize, |
| 3502 | ) |
| 3503 | for i, elt in enumerate(bboxes, 1): |
| 3504 | if use_tracking_label: |
| 3505 | label = elt["label"].split(":")[0] |
| 3506 | color = colors[elt["label"].split(":")[1].strip()] |
| 3507 | else: |
| 3508 | label = f"{i}" |
| 3509 | color = colors[elt["label"]] |
| 3510 | box = elt["bbox"] |
| 3511 | |
| 3512 | # denormalize the box if it is normalized |
| 3513 | box = denormalize_bbox(box, (height, width)) |
| 3514 | x0, y0, x1, y1 = box |
| 3515 | cx, cy = (x0 + x1) / 2, (y0 + y1) / 2 |
| 3516 | |
| 3517 | text_box = draw.textbbox( |
| 3518 | (cx, cy), text=label, font=font, align="center", anchor="mm" |
| 3519 | ) |
| 3520 | |
| 3521 | # Calculate the offset to center the text within the bounding box |
| 3522 | text_width = text_box[2] - text_box[0] |
| 3523 | text_height = text_box[3] - text_box[1] |
| 3524 | text_x0 = cx - text_width / 2 |
| 3525 | text_y0 = cy - text_height / 2 |
| 3526 | text_x1 = cx + text_width / 2 |
| 3527 | text_y1 = cy + text_height / 2 |
| 3528 | |
| 3529 | # Draw the rectangle encapsulating the text |
| 3530 | draw.rectangle((text_x0, text_y0, text_x1, text_y1), fill=color) |
| 3531 | |
| 3532 | # Draw the text at the center of the bounding box |
| 3533 | draw.text( |
| 3534 | (text_x0, text_y0), |
| 3535 | label, |
| 3536 | fill="black", |
| 3537 | font=font, |
| 3538 | anchor="lt", |
| 3539 | ) |
| 3540 | |
| 3541 | return image |
| 3542 | |
| 3543 | |
| 3544 | FUNCTION_TOOLS = [ |
no test coverage detected