如何将其他机器导出的Label Studio YOLO数据导入本地Label Studio查看?
在本地Label Studio中导入并查看YOLO格式的旋转矩形标注数据
要直接在Label Studio里查看从其他机器导出的YOLO格式旋转矩形标注,你需要完成以下步骤:
1. 创建匹配的Label Studio项目
首先在本地Label Studio新建项目,标注配置必须选择**旋转矩形框(Rotated Bounding Box)**类型:
- 进入项目设置 -> Labeling Interface
- 选择"Custom",粘贴以下配置(根据你的
classes.txt调整类别):
<View> <Image name="image" value="$image"/> <RectangleLabels name="label" toName="image"> <!-- 替换成classes.txt里的所有类别,按顺序排列 --> <Label value="Class1"/> <Label value="Class2"/> <Label value="Class3"/> </RectangleLabels> </View>
确保<Label>的顺序和classes.txt完全一致,否则标签会对应错误。
2. 导入原始图片
在项目中点击Import,选择/images目录下的所有图片(或直接拖拽到导入区域),等待图片上传完成。
3. 批量关联YOLO标签到项目
Label Studio没有直接导入YOLO标签文件的功能,你需要把YOLO格式的标注转换成Label Studio支持的JSON任务格式,用以下Python脚本快速处理:
import os import json from PIL import Image # 替换成你的实际路径 images_dir = "/your/local/path/images" labels_dir = "/your/local/path/labels" classes_file = "/your/local/path/classes.txt" output_json = "label_studio_tasks.json" # 读取类别列表 with open(classes_file, 'r', encoding='utf-8') as f: classes = [line.strip() for line in f if line.strip()] tasks = [] # 遍历所有图片文件 for img_filename in os.listdir(images_dir): if not img_filename.lower().endswith(('.png', '.jpg', '.jpeg')): continue # 对应标签文件路径 label_basename = os.path.splitext(img_filename)[0] + ".txt" label_path = os.path.join(labels_dir, label_basename) annotations = [] if os.path.exists(label_path): with open(label_path, 'r') as f: for line_num, line in enumerate(f): line = line.strip() if not line: continue # YOLO旋转框格式:class_id cx cy w h angle(归一化值,角度为弧度,逆时针) parts = list(map(float, line.split())) class_id = int(parts[0]) cx, cy, w, h, angle = parts[1:6] # 获取图片实际尺寸,反归一化坐标 img_path = os.path.join(images_dir, img_filename) with Image.open(img_path) as img: img_width, img_height = img.size # 转换为Label Studio的坐标格式(x,y为左上角坐标,宽高为实际像素,旋转为顺时针度数) x = (cx - w/2) * img_width y = (cy - h/2) * img_height width = w * img_width height = h * img_height rotation = -angle * 180 / 3.1415926535 # 弧度转度数,反转方向 # 构建标注项 annotations.append({ "result": [ { "value": { "x": x, "y": y, "width": width, "height": height, "rotation": round(rotation, 2), "rectanglelabels": [classes[class_id]] }, "id": f"rot_{img_filename}_{line_num}", "from_name": "label", "to_name": "image", "type": "rectanglelabels" } ] }) # 构建Label Studio任务 task = { "data": { "image": f"/data/local-files/?d={os.path.abspath(images_dir)}/{img_filename}" }, "annotations": annotations } tasks.append(task) # 保存为可导入的JSON文件 with open(output_json, 'w', encoding='utf-8') as f: json.dump(tasks, f, indent=2, ensure_ascii=False)
脚本使用说明:
- 安装依赖:
pip install pillow - 修改脚本中的路径为你的实际文件夹路径
- 运行脚本,生成
label_studio_tasks.json文件 - 在Label Studio项目中点击Import -> Import JSON,选择生成的JSON文件
4. 查看标注
导入完成后,进入项目的标注界面,就能看到每张图片上的旋转矩形标注,和原始YOLO导出的标注完全一致。
注意事项:
- 如果你的YOLO旋转框角度是度数而非弧度,去掉脚本中弧度转度数的计算,直接使用
rotation = -angle(注意方向反转) - 确保Label Studio有权限访问你的本地图片目录,若遇到路径问题,可将图片文件夹复制到Label Studio的
data目录下
内容的提问来源于stack exchange,提问作者Elizabeth Woodward
相关产品推荐
相关产品推荐

