File size: 2,224 Bytes
7e3773e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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}")