帮我写一个批量将yolopose多个txtx文件转换成cvat导出的cocokeypoints的单个json文件脚本
时间: 2024-01-27 22:05:31 浏览: 57
批量转换json文件
好的,以下是能为您提供帮助的脚本代码:
```python
import json
import glob
files = glob.glob('*.txt')
data = {
"annotations": [],
"categories": [
{
"id": 1,
"name": "person",
"supercategory": "person"
}
],
"images": []
}
count = 0
for file in files:
with open(file, 'r') as f:
content = f.read()
content = content.split('\n')
for line in content:
line = line.split(' ')
if len(line) < 4:
continue
image_id = file.split('.')[0]
x, y, w, h = map(float, line[1:])
data['annotations'].append({
"id": count,
"image_id": int(image_id),
"category_id": 1,
"keypoints": [x, y, 1, x+w, y, 1, x+w, y+h, 1, x, y+h, 1],
"num_keypoints": 4,
"iscrowd": 0
})
count += 1
data['images'].append({
"id": int(image_id),
"file_name": image_id + ".jpg",
"width": 1280,
"height": 720,
"license": 1
})
with open('annotations.json', 'w') as f:
json.dump(data, f)
```
请注意,这段脚本只针对特定的数据格式,您需要自己根据自己的数据格式进行修改,否则可能会导致操作失败。
阅读全文