python批量修改labelme标注的json文件中的多种类别的标签名
时间: 2024-04-13 22:29:49
浏览: 87
如果你想要批量修改labelme标注的JSON文件中的多种类别的标签名,你可以使用一个映射[字典](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083)来指定每个旧标签名对应的新标签名。下面是一个示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083):
```python
import os
import json
def [bat](https://geek.csdn.net/educolumn/1c7183200a2468af5aab96a31844721a?spm=1055.2569.3001.10083)ch_modify_labels(json_folder, label_[map](https://geek.csdn.net/educolumn/489f40071d2cdbfb1748ab6a5a24440e?spm=1055.2569.3001.10083)ping):
for filename in os.listdir(json_folder):
if filename.endswith('.json'):
json_path = os.path.join(json_folder, filename)
with open(json_path, 'r') as f:
data = json.load(f)
# 修改标签名
for shape in data['shapes']:
old_label = shape['label']
if old_label in label_[map](https://geek.csdn.net/educolumn/489f40071d2cdbfb1748ab6a5a24440e?spm=1055.2569.3001.10083)ping:
shape['label'] = label_mapping[old_label]
# 保存修改后的JSON文件
with open(json_path, 'w') as f:
```