X-AnyLabeling 生成的JSON格式标签转换为YOLO格式标签详细教程(含代码)
以下是一个Python脚本,用于将JSON格式的标注数据转换为YOLO格式的TXT文件。
import os
import json
from PIL import Image
import warnings
warnings.filterwarnings("ignore")
json_dir = 'path' # JSON文件路径
out_dir = 'path' # 输出的TXT文件路径
# 确保输出目录存在
if not os.path.exists(out_dir):
os.makedirs(out_dir)
def get_json(json_file, filename):
# 读取JSON文件数据
with open(json_file, 'r') as load_f:
content = json.load(load_f)
image_width = 720 # 图片的宽
image_height = 360 # 图片的高
filename_txt = out_dir + filename + '.txt'
# 创建或清空TXT文件
with open(filename_txt, mode="w", encoding="utf-8") as fp:
pass # 创建一个空文件然后关闭
# 类别名称,对应标签序号
name = ['standing', 'sitting', 'lying