yolov5的json标注转txt
时间: 2023-07-19 18:12:26 浏览: 46
将YOLOv5的JSON标注转换为TXT标注可以使用以下Python脚本:
```python
import json
def convert(json_file, txt_file):
with open(json_file, "r") as f:
data = json.load(f)
with open(txt_file, "w") as f:
for image in data:
filename = image["filename"]
for bbox in image["annotations"]:
x, y, w, h = bbox["left"], bbox["top"], bbox["width"], bbox["height"]
class_id = bbox["class"]
x_center = x + w / 2
y_center = y + h / 2
x_center /= image["width"]
y_center /= image["height"]
w /= image["width"]
h /= image["height"]
f.write(f"{class_id} {x_center} {y_center} {w} {h}\n")
```
其中,`json_file` 为YOLOv5的JSON标注文件路径,`txt_file` 为要输出的TXT标注文件路径。调用 `convert` 函数即可将JSON标注文件转换为TXT标注文件。
注意:该脚本仅适用于YOLOv5的JSON标注格式,不适用于其他格式的标注文件。