Labelme标车道线转culane数据集格式

Labelme标车道线转culane数据集格式

Labelme分割标注软件使用请参考: 太阳花的小绿豆

Labelme标车道线参考链接: 用labelme自己标注的数据转tusimple格式数据集

有时候需要做一些车道线的标签,我常用的标注软件是Labelme,如果不知道怎么用Labelme的小伙伴可以参考上面的链接,下载操作流程非常简单很简单,车道线标注的格式用“linestrip",然后打点开始标。
在这里插入图片描述

因为Labelme保存的格式是json,大家可以打开json文件,看它保存了些啥。目前的车道线任务主要使用culane(.txt)和tusimple(.json)两种数据集,虽然也可以自己写自定义数据集的加载代码,但是本人比较懒,将标签格式统一成culane的格式,用他们写好的配置文件,稍微改改就可以运行了。

话不多说,直接上代码,有注释,大家可以debug慢慢看。

"""
Time: 2023.12.5
Fuction: labelme标注的数据 转 culane格式数据集 ,车道线数据集
Author:ZhangYun

culane格式数据集:
    横坐标 纵坐标 横坐标 纵坐标 横坐标 纵坐标 横坐标 纵坐标
    其中,纵坐标(等分确定)
    标注的过程应该是,将图片的下半部分如70%*height 等分成N份。然后取车道线(如论虚实)与该标注线交叉的点
"""
import cv2
import os
import glob
import json
import numpy as np
import shutil


print("[LOG] start ...")
images_dir = r"D:\labelme\images/"

labelme_dir = r"D:\labelme\labels/"
# lebalme标注的文件路径,如:01.jpg,01.json ...

# 存放culane数据集的根目录路径
result_dir = r"D:\labelme\txt/"

# 判断结果文件路径文件夹是否存在
if not os.path.exists(result_dir):
    os.makedirs(result_dir)


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

for idx, img_path in enumerate(images_glob):
    print(idx, img_path)
    # 获取img名字
    img_name_file = os.path.basename(img_path)
    # 获取img名字,去掉后缀
    img_name = os.path.splitext(img_name_file)[0]

    # 读取图像
    img = cv2.imread(img_path, -1)
    print('img.shape:', img.shape)

    img_w = img.shape[1]
    img_h = img.shape[0]

    result_path = result_dir + img_name_file
    # shutil.copy(img_path, result_path)
    cv2.imwrite(result_path, img)

    # 定义h_samples 纵坐标等分区间,根据自己的数据图像调整
    h_samples = list(range(160, img_h, 10))  # range(start,end,step)
    print("h_sample:", h_samples)
    # 绘制横线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)

    # txt格式文件输出
    txt_name = img_name + ".lines.txt"
    txt_file_path = os.path.join(result_dir, txt_name)
    # 标签json文件路径
    json_dir = labelme_dir + img_name + '.json'
    # 加载json文件
    if os.path.isfile(json_dir):
        with open(json_dir, 'r') as json_obj:
            data = json.load(json_obj)
        # 初始化一个list存放单张图的所有车道线
        lanes = []
        #打开txt文件,开始读写
        with open(txt_file_path, "w+") as f:
            for shapes in data['shapes']:
                binary_image = np.zeros([img_h, img_w], np.uint8)
                # 单条lane
                single_lane = []
                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 = np.where(img_and[h, :] > 1)[0]
                    if len(temp_w) > 0:
                        start = True
                    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)
                        single_lane.append(h)
                #写入至少有两个坐标的车道线标签
                if len(single_lane) >= 4:
                    for i in single_lane:
                        f.write(str(i) + ' ')
                    f.write('\n')
                lanes.append(single_lane)
    #image没有车道线,json文件,也生成一个空的txt标签文件。
    else:
        with open(txt_file_path, "w+") as f:
            f.write('')

    f.close()
    json_obj.close()

如果大家想改变图像大小和输出标签,对图像img进行相应cop和resize ,注意binary_image_h和binary_image的设置。

  • 10
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值