用labelme自己标注的数据转tusimple格式数据集

说明

通过类别labelme标注的数据转为车道线的数据,labelme标注的格式为“linestrip",如下图所示:
在这里插入图片描述
这里自己用的图片尺寸为1280x720大小。如果是自定图片数据大小,请修改h_samples 参数

环境
  • ubuntu18.04
  • opencv4.5.0
  • json
  • python3.7
  • numpy

安装opencv_python、numpy 等第三方依赖包

pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install opencv_python -i https://pypi.tuna.tsinghua.edu.cn/simple
代码

新建文件lable_to_tusimpledataset.py,并添加如下代码:

// --run--
"""
Time: 2022.8.10
Fuction: labelme标注的数据 转 tusimple格式数据集 ,车道线数据集
Author:yu_fang


tusimple格式数据集:
    标注json 文件中每一行包括三个字段 :
    raw_file : 每一个数据段的第20帧图像的的 path 路径
    lanes 和 h_samples 是数据具体的标注内容,为了压缩,h_sample 是纵坐标(等分确定),lanes 是每个车道的横坐标,是个二维数组。-2 表示这个点是无效的点。
    标注的过程应该是,将图片的下半部分如70%*height 等分成N份。然后取车道线(如论虚实)与该标注线交叉的点

"""
import cv2
import os
import glob
import json
import numpy as np
import shutil

print("[LOG] start ...")
# lebalme标注的文件路径,如:01.jpg,01.json ...
labelme_dir = "/media/Linux/work/clrnet/Railway_tusimple20220808"
#labelme_dir = "/media/Linux/work/clrnet/Railway_tusimple20220808_test"

# 存放tusimple数据集的根目录路径
result_dir = "/media/Linux/work/clrnet/tusimple_self_202208101107"
#result_dir = "/media/Linux/work/clrnet/tusimple_self_202208101107_test"

# 保存的json文件名,带后缀,命名类似tusimple数据集里的json文件名
json_file_name = "label_data_0809.json"
# 同时在clips文件夹里子文件命名为0810
clips_sub_name = os.path.splitext(json_file_name)[0].split("_")[-1]
clips_sub = os.path.join("clips",clips_sub_name)
# 判断结果文件路径文件夹是否存在
if not os.path.exists(result_dir):
    os.makedirs(result_dir)
# 创建clips文件夹,及子文件夹
clips_sub_dir = os.path.join(result_dir,clips_sub)
if not os.path.exists(clips_sub_dir):
    os.makedirs(clips_sub_dir)

# 定义h_samples 纵坐标等分区间,根据自己的数据图像调整
h_samples = list(range(160, 720, 10)) # range(start,end,step)
print("h_sample:",h_samples)

#批量获取图片
labelme_img = glob.glob(labelme_dir+'/*.jpg')  # 根据自己的图片格式更改,这里是*.jpg格式
print("labelme_json:",labelme_img)


# 写入json文件
json_file_path = os.path.join(result_dir,json_file_name)
with open(json_file_path, 'w') as wr:
    # 遍历img,json
    for idx, img_path in enumerate(labelme_img):
        print(idx,img_path)
        img_name_file = os.path.basename(img_path)
        # 获取img名字,去掉后缀
        img_name = os.path.splitext(img_name_file)[0]
        # 获取对应的json文件路径
        json_path = os.path.splitext(img_path)[0] + ".json"
        # 初始化一个list存放单张图的所有车道线
        lanes = []
        dict_img_per = {}
        if os.path.isfile(json_path):
            img = cv2.imread(img_path,-1)
            img_w = img.shape[1]
            img_h = img.shape[0]
            #binary_image = np.zeros([img_h, img_w], np.uint8)

            # 绘制横线h_samples
            binary_image_h = np.zeros([img_h, img_w], np.uint8)
            for h in h_samples:
                cv2.line(binary_image_h,(0,h),(img_w-1,h),(255),thickness=1)
            # cv2.imshow("2",binary_image_h)
            # 加载json文件
            with open(json_path, 'r') as json_obj:
                data = json.load(json_obj)
                for shapes in data['shapes']:
                	binary_image = np.zeros([img_h, img_w], np.uint8)
                    # 单条lane
                    single_lane = []
                    label = shapes['label']
                    # print("label:",label)
                    points = shapes['points']   # 标注的linestrip
                    points = np.array(points, dtype=int)
                    cv2.polylines(binary_image,[points],False,(255),thickness=1) # 绘制车道线
                    img_and = cv2.bitwise_and(binary_image,binary_image_h)
                    # 获取宽高交点
                    for h in h_samples:
                        start = False
                        temp_w = []
                        for w in range(img_w):
                            if img_and[h, w] >= 1:
                                start = True
                                temp_w.append(w)
                        if start:
                            half = len(temp_w) // 2
                            median = (temp_w[half] + temp_w[~half])/2
                            median = int(median)
                            # print("half:",half,median)
                            single_lane.append(median)
                        else:
                            single_lane.append(-2)
                    # print("single_lane:",single_lane)
                    # cv2.imshow("1",img_and)
                    # cv2.waitKey(0)
                    lanes.append(single_lane)
            json_obj.close()
            print("lanes:",lanes)
            raw_file = os.path.join(clips_sub,img_name_file)
            shutil.copy(img_path, clips_sub_dir)
            dict_img_per = {"lanes":lanes,"h_samples":h_samples,"raw_file":raw_file}
            # print("dict_img_per:",dict_img_per)
            # 将dict写入名称为wr的文件中
            # json.dump(dict_img_per, wr, indent=4)  # indent控制间隔,缩进
            json.dump(dict_img_per, wr)
            wr.write('\n')
        else:
            continue
    wr.close()

结果

运行lable_to_tusimpledataset.py,批量生成后的结果如下图所示:
在这里插入图片描述

### 如何使用 LabelMe 工具进行车道线的图像标注 LabelMe 是一种灵活且功能强大的开源标注工具,广泛应用于计算机视觉领域中的各种任务。以下是关于如何利用 LabelMe 对车道线数据集进行标注的具体说明。 #### 软件安装与启动 为了开始使用 LabelMe,需先完成其安装过程。通常情况下,可以通过 pip 命令轻松实现安装[^1]。一旦成功安装并运行该程序,则可通过两种方式导入待标注的图片:一是借助左侧快捷工具栏上的 Open 功能;二是直接将目标文件夹内的图片拖拽到软件界面上[^2]。 #### 图像标注方法 当准备就绪后,在实际执行车道线标注环节时需要注意特定的操作细节: - **形状模式的选择** 针对车道线这种连续线条特征的对象,推荐采用 polygons 或 linestrip 形状来进行精确描绘。不过这里存在一个小技巧——尽管某些资料可能建议运用 linestrip 来简化操作流程[^3],但从实践经验来看,选用 polygons 更能确保最终生成的结果具备完整的几何结构,从而避免因未形成闭合图形而导致训练过程中出现问题的情况发生。 - **具体操作步骤** - 打开所需处理的一张或多张道路场景照片; - 右键点击画布区域或者通过顶部菜单找到 Create LineStrip/Curve Strip 的选项以创建新的线条实例[^4]; - 按照真实道路上每条分隔带的实际走向依次放置节点直至整条线路被完整勾勒出来为止; - 定义好每一个独立部分之后赋予它们相应的类别名称(比如left_lane、right_lane等等),这一步骤对于后续模型学习区分不同类型的车道至关重要. #### JSON 文件换为其他格式 完成所有必要的手工编辑工作以后,下一步就是把保存下来的 .json 结构化描述文档化为适合深度学习框架使用的标准输入形式之一。例如 Tusimple 格式换涉及到调整 h_samples 参数值匹配自定义分辨率需求等方面的内容: ```python import os, glob # 设定源目录路径以及输出位置 LabelPaths = glob.glob('./LabelDataset/*.json') for LabelPath in LabelPaths: Name = './Train/' + os.path.basename(LabelPath).split('.')[0] # 构造命令字符串调用外部脚本完成单个JSON对象解析拆解动作 cmd = f'labelme_json_to_dataset "{LabelPath}" --out "{Name}"' os.system(cmd) ``` 上述代码片段展示了怎样遍历指定子目录下的全部 Json 记录,并逐一对他们应用官方提供的 `labelme_json_to_dataset` 函数接口服务来生产对应的像素级掩码映射成果集合. ---
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

圆滚熊

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值