import json
import os
def convert_labelme_to_yolo(json_file, output_txt_file, label_to_class_id):
with open(json_file, 'r') as file:
data = json.load(file)
image_width = data['imageWidth']
image_height = data['imageHeight']
with open(output_txt_file, 'w') as file:
for shape in data['shapes']:
points = shape['points']
class_id = label_to_class_id.get(shape['label'], -1)
normalized_points = []
for x, y in points:
nx = round(x / image_width, 6)
ny = round(y / image_height, 6)
normalized_points.append(f"{nx} {ny}")
file.write(f"{class_id} " + " ".join(normalized_points) + "\n")
def batch_convert(json_folder, output_folder, label_to_class_id):
# 确保输出文件夹存在
os.makedirs(o
labelme标注的<分割数据集>json转yolo<分割>数据集txt
于 2024-04-16 19:18:37 首次发布