label-studio 导入既有的yolo格式标注

这是新增识别物体训练前的一个预处理过程。你的额外标注要建立在模型已经具备的识别能力的基础上。

0.参考资料:

1.主要参考源:

Tutorial: Importing Local YOLO Pre-Annotated Images to Label Studio | Label Studio

2.备用的label-studio-converter文档: 

GitHub - HumanSignal/label-studio-converter: Tools for converting Label Studio annotations into common dataset formats


 

1.安装label转换工具

pip install label-studio-converter

2.将原有的coco数据集放在C:根目录下:例如:

2.1 classes.txt的内容为

classes的列表,每个物体一行,不允许有同名文件。注意,yolo的classid是从0开始的。缺省的class_id有80个,即:0~79,第一个用户自定义class_id=80。

person
bicycle
car  

2.2 image, labels不再拆分为训练和验证 

3.转换 

这一步会借助label-studio-converter工具(pip  install安装)来进行实际的yolo label->label studio格式的.json .xml文件格式转换。

注意,命令行中,images在classes.txt文件所在目录中单独列出,然后, -i的参数是 classes.txt所在目录。 -i 指向的目录结构,需要把images和labels分别分文件夹放置,然后把classes.txt放在这个文件夹的根目录。--image-root-url指向的目标是需要在Label studio中能够访问到的图片源,请参考4.2节。

#!/bin/bash
#label-studio-converter import yolo -i /dataset/before_import -o output.json --image-root-url "/data/local
#-files/?d=dataset/before_import/images"
# the directory should be:
# /dataset/before_import
# /dataset/before_import/classes.txt
# /dataset/before_import/images
# /dataset/before_import/labels
label-studio-converter import yolo -i /git/dataset/moonpie/images -o output.json --image-root-url "/data/local
-files/?d=dataset/before_import/images"

C:\Users\twica>label-studio-converter import yolo -i /dataset/before_import -o output.json --image-root-url "/data/local
-files/?d=dataset/before_import/images"

INFO:root:Reading YOLO notes and categories from C:\dataset\before_import
INFO:root:Found 81 categories
INFO:root:Converting labels from C:\dataset\before_import\labels
INFO:root:image extensions->, ['.jpg']
INFO:root:Saving Label Studio JSON to C:\Users\twica\output.json

  1. Create a new project in Label Studio
  2. Use Labeling Config from "C:\Users\twica\output.label_config.xml"
  3. Setup serving for images
       E.g. you can use Local Storage (or others):
       https://labelstud.io/guide/storage.html#Local-storage
       See tutorial here:
https://github.com/HumanSignal/label-studio-converter/tree/master?tab=readme-ov-file#yolo-to-label-studio-converter

  4. Import "C:\Users\twica\output.json" to the project

4.导入

4.1 导入label-config

这个需要把output.label_config.xml,手工粘贴进来

4.2 设置工程的image src源

注意这里要设置为文件源,注意,我是把label_studio部署在windows系统的,这里的目录结构更明显。这里的源,只需要指向图片文件夹本身。


4.3 import

 4.4 导入后

5. 进阶 - 使用模型生成yolo标签,然后重新导入label-studio

最终的混合标注数据集:

只描述一下工作量,不再附上代码:

  1. 使用yolo detect.py工具调用多个.pt工具进行标记(比如yolo11s.pt预训练模型),然后还有单独的特定物体的训练集的.pt模型。生成yolo格式的标记文件。
  2. 标记文件的classesid重新规划。modified and merge.
  3. 针对merge之后的yolo标记文件进行上述转换。merge的yolo标记文件示例:

附录A 混合标注yolo格式的label.txt

注意这里的标记实际是有问题的。如果只新增了一个待识别物体,它的classid应该是80,而不是81。一旦这里设置为81,则实际的classes.txt文件应该包含82行才对。82个classes.

devcontainers@LAPTOP-0BJBKJGQ:/git/dataset/moonpie/images/labels$ cat Cake9.txt
55 0.679134 0.346854 0.15093 0.196628
55 0.501391 0.58123 0.147194 0.195507
55 0.679892 0.574384 0.156641 0.193863
55 0.33148 0.357971 0.152468 0.196884
55 0.330766 0.574022 0.154556 0.191562
55 0.160202 0.576827 0.164656 0.196943
55 0.501387 0.351655 0.141083 0.205394
55 0.674795 0.149105 0.147664 0.207623
55 0.339976 0.154799 0.139387 0.197028
60 0.503871 0.503981 0.979967 0.983309
55 0.853498 0.350365 0.165343 0.19789
55 0.86647 0.570387 0.162402 0.193435
81 0.160803 0.576713 0.159472 0.196644
81 0.33004 0.573643 0.148516 0.189898
81 0.501294 0.580369 0.143412 0.191946
81 0.501001 0.351631 0.137057 0.201614
81 0.515014 0.850536 0.145317 0.219617
81 0.679284 0.346961 0.14827 0.190701
81 0.68753 0.849333 0.151887 0.223992
81 0.679555 0.573725 0.153101 0.193082
81 0.340212 0.153363 0.133932 0.19493
81 0.498815 0.147943 0.134052 0.195807
81 0.8381 0.140529 0.152241 0.199529
81 0.331765 0.357309 0.14806 0.193367
81 0.177596 0.15824 0.152942 0.196422
81 0.854053 0.351927 0.161736 0.1959
81 0.674657 0.147272 0.143009 0.203568
81 0.145352 0.832953 0.166784 0.213355
81 0.163286 0.359973 0.160709 0.191367
81 0.862673 0.569785 0.166771 0.191214
81 0.872219 0.855528 0.17271 0.221026
81 0.324842 0.84606 0.15045 0.223207

Label Studio 是一个开源的数据标注工具,支持多种数据类型的标注,包括图像、文本、音频和视频。导出的 YOLO 格式数据可以用于训练目标检测模型。以下是如何使用 Label Studio 导出的 YOLO 格式数据的步骤: ### 1. 导出数据 在 Label Studio 中完成标注后,可以选择导出格式YOLO。导出后的数据通常包含以下文件: - 图片文件:通常在一个文件夹中。 - 标注文件:每个图片对应一个 `.txt` 文件,文件名与图片名相同。 ### 2. 理解 YOLO 格式 YOLO 格式标注文件每行表示一个目标,格式如下: ``` <class_id> <x_center> <y_center> <width> <height> ``` - `class_id`:类别编号,从 0 开始。 - `x_center`:目标框中心点的 x 坐标,相对于图片宽度的比例。 - `y_center`:目标框中心点的 y 坐标,相对于图片高度的比例。 - `width`:目标框的宽度,相对于图片宽度的比例。 - `height`:目标框的高度,相对于图片高度的比例。 ### 3. 准备训练数据 将导出的图片和标注文件组织成训练集和验证集。通常会将数据集分为训练集和验证集,并创建相应的目录结构。 ### 4. 配置 YOLO 模型 根据使用的 YOLO 版本,配置模型配置文件(如 `yolov3.cfg` 或 `yolov4.cfg`),设置类别数、训练参数等。 ### 5. 训练模型 使用 YOLO 的训练脚本(如 `train.py`)进行模型训练。指定训练集和验证集的路径、配置文件路径等参数。 ### 6. 评估和推理 训练完成后,可以使用模型进行目标检测。YOLO 提供了推理脚本(如 `detect.py`),可以输入图片或视频,输出检测结果。 ### 示例代码 以下是一个简单的示例代码,展示如何使用 YOLO 进行推理: ```python import cv2 import numpy as np # 加载 YOLO 模型 net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") # 加载类别名称 with open("coco.names", "r") as f: classes = [line.strip() for line in f.readlines()] # 加载图片 image = cv2.imread("image.jpg") height, width, channels = image.shape # 创建 blob blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) # 获取输出层 layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] # 前向传播 outs = net.forward(output_layers) # 处理检测结果 class_ids = [] confidences = [] boxes = [] conf_threshold = 0.5 nms_threshold = 0.4 for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > conf_threshold: center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) # 应用非极大值抑制 indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold) # 绘制检测结果 for i in indices: i = i[0] x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) color = (0, 255, 0) cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # 显示结果 cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子正

thanks, bro...

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值