File size: 2,044 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
import matplotlib.pyplot as plt
from PIL import Image
import matplotlib.patches as patches

# 加载图像
image_path = "demo_images/yidali2.jpg"  # 替换为第二张图像的实际路径
output_path = "output_image_2.jpg"      # 设置保存路径
image = Image.open(image_path)

# 第二张图像的边界框和类别数据
data = [
    {"bbox_2d": [668, 240, 799, 538], "label": "Person"},
    {"bbox_2d": [531, 319, 703, 580], "label": "Person"},
    {"bbox_2d": [207, 235, 348, 569], "label": "Person"},
    {"bbox_2d": [623, 344, 691, 389], "label": "Violin"},
    {"bbox_2d": [281, 195, 425, 560], "label": "cello"},
    {"bbox_2d": [0, 302, 208, 492], "label": "bicycle"},
    {"bbox_2d": [338, 316, 432, 449], "label": "bicycle"},
    {"bbox_2d": [679, 270, 840, 384], "label": "guitar"}
]

# 定义颜色(添加 guitar 的颜色)
color_map = {
    "Person": "#ff5733",  # 亮橙色
    "Violin": "#33ff57",  # 亮绿色
    "cello": "#5733ff",   # 亮蓝色
    "bicycle": "#e3d820", # 黄色
    "guitar": "#ff33a1"   # 亮粉色(新增颜色)
}

# 创建绘图
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 = color_map[label]
    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}")