python,将labelimg的标注文件json转换为paddleocr格式label.txt
时间: 2023-07-29 19:11:44 浏览: 172
将labelme和labelimg标注的数据 转成paddlex的数据格式
5星 · 资源好评率100%
可以使用以下代码将labelimg的标注文件json转换为paddleocr格式的label.txt文件:
```python
import json
import os
def labelimg2paddleocr(json_path, label_path):
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
with open(label_path, 'w', encoding='utf-8') as f:
for item in data:
img_path = os.path.splitext(item['imagePath'])[0] + '.jpg'
for shape in item['shapes']:
label = shape['label']
xmin = min(shape['points'], key=lambda x: x[0])[0]
ymin = min(shape['points'], key=lambda x: x[1])[1]
xmax = max(shape['points'], key=lambda x: x[0])[0]
ymax = max(shape['points'], key=lambda x: x[1])[1]
f.write(f'{img_path}\t{xmin},{ymin},{xmax},{ymax},{label}\n')
if __name__ == '__main__':
json_path = 'path/to/labelimg/json/file'
label_path = 'path/to/paddleocr/label.txt'
labelimg2paddleocr(json_path, label_path)
```
其中,`json_path`为labelimg标注文件的json文件路径,`label_path`为转换后的paddleocr格式的label.txt文件路径。转换后的label.txt文件中每一行保存了对应图片中的一个目标的位置和标签信息。每个标注信息由5个字段组成,分别为图片路径、目标左上角x坐标、目标左上角y坐标、目标右下角x坐标、目标右下角y坐标和目标标签。
阅读全文