| import matplotlib.pyplot as plt |
| from PIL import Image |
| import matplotlib.patches as patches |
|
|
| |
| image_path = "demo_images/yidali.jpg" |
| output_path = "output_image.jpg" |
| image = Image.open(image_path) |
|
|
| |
| data = [ |
| {"bbox_2d": [165, 198, 274, 350], "label": "curtain"}, |
| {"bbox_2d": [138, 150, 305, 400], "label": "window"}, |
| {"bbox_2d": [495, 181, 585, 408], "label": "window"}, |
| {"bbox_2d": [263, 347, 500, 508], "label": "bicycle"}, |
| {"bbox_2d": [101, 430, 235, 509], "label": "flowerpot"}, |
| {"bbox_2d": [235, 400, 299, 487], "label": "flowerpot"}, |
| {"bbox_2d": [359, 328, 408, 452], "label": "flowerpot"}, |
| {"bbox_2d": [654, 376, 696, 428], "label": "flowerpot"}, |
| {"bbox_2d": [689, 403, 711, 429], "label": "flowerpot"}, |
| {"bbox_2d": [711, 399, 731, 425], "label": "flowerpot"}, |
| {"bbox_2d": [731, 356, 759, 382], "label": "flowerpot"}, |
| {"bbox_2d": [0, 402, 69, 552], "label": "flowerpot"} |
| ] |
|
|
| |
| color_map = { |
| "curtain": "#eb60b2", |
| "window": "#4adde5", |
| "bicycle": "#e3d820", |
| "flowerpot": "#2adac0" |
| } |
|
|
| |
| fig, ax = plt.subplots(1, figsize=(12, 8)) |
| ax.imshow(image) |
|
|
| |
| instance_count = {} |
| for item in data: |
| bbox = item["bbox_2d"] |
| label = item["label"] |
|
|
| |
| if label not in instance_count: |
| instance_count[label] = 0 |
| color_depth = 1 - (instance_count[label] * 0.1) |
| base_color = color_map[label] |
| color = base_color |
| instance_count[label] += 1 |
|
|
| |
| rect = patches.Rectangle( |
| (bbox[0], bbox[1]), |
| bbox[2] - bbox[0], |
| bbox[3] - bbox[1], |
| linewidth=4, |
| edgecolor=color, |
| facecolor="none" |
| ) |
| ax.add_patch(rect) |
|
|
| |
| ax.text( |
| bbox[0], |
| bbox[1] - 10, |
| label, |
| color="white", |
| fontsize=13, |
| bbox=dict(facecolor=color, edgecolor="none", boxstyle="round,pad=0.2") |
| ) |
|
|
| |
| ax.axis("off") |
|
|
| |
| plt.savefig(output_path, bbox_inches="tight", pad_inches=0, dpi=300) |
| plt.close() |
|
|
| print(f"Image saved to {output_path}") |