写一个python程序,能展示labelme标注的.json文件
时间: 2025-02-23 17:31:41 浏览: 48
在Python中,我们可以使用`json`模块来读取`.json`文件,并利用`matplotlib`库或者其他绘图工具来显示标签数据。以下是一个简单的示例,假设我们有一个包含图像路径和注解信息的`labels.json`文件:
```python
import json
import matplotlib.pyplot as plt
def display_labels_from_json(json_file):
# 读取json文件
with open(json_file, 'r') as f:
data = json.load(f)
# 假设json内容类似这样:
# {
# "image_path": "image.jpg",
# "annotations": [
# {"x": 50, "y": 70, "class_id": 1},
# {"x": 150, "y": 200, "class_id": 2}
# ]
# }
image_path = data['image_path']
annotations = data['annotations']
# 创建一幅新的图像
fig, ax = plt.subplots()
# 绘制原始图像
img = plt.imread(image_path)
ax.imshow(img)
# 根据注解绘制矩形框
for annotation in annotations:
x, y = annotation['x'], annotation['y']
class_id = annotation['class_id'] # 这里只是占位,具体颜色需要映射到实际类别
rect = plt.Rectangle((x, y), 20, 20, color='red') # 假设每个点都是20x20像素大小的方块
ax.add_patch(rect)
ax.text(x, y, f"Class {class_id}", fontsize=8) # 添加标签文字
# 显示图像
plt.title('LabelMe JSON file visualization')
plt.show()
# 使用函数
display_labels_from_json('labels.json')
```
这个例子中,我们假设`labels.json`文件的结构已经预先按照上述格式设计。如果你的实际文件结构不同,你需要相应地调整代码。
阅读全文
相关推荐


















