culane数据集转换代码
时间: 2025-03-30 09:09:36 浏览: 45
CULane 数据集是一个广泛使用的车道线检测基准数据集,它包含大量带标注的真实道路图像。如果你需要将 CULane 数据集转换成其他格式(如 COCO 格式或其他自定义格式),通常会涉及到对原始标签文件的解析、处理以及保存。
以下是关于如何编写代码实现 CULane 转换的基本思路及步骤:
### 1. 理解 CULane 的结构
CULane 数据集一般由以下几个部分组成:
- **图片**:存储在 `images` 文件夹下的训练集、验证集和测试集对应的子目录内。
- **标记文件**:通常是 `.lines.txt` 或者类似的文本形式,每行记录了一组点坐标信息表示一条车道线的位置。
### 2. 编写转换脚本
假设我们要将其转为 JSON 格式的 COCO 数据集样式,则可以按照以下步骤操作:
#### (1) 解析 txt 文件中的内容
```python
def parse_culane_txt(txt_file):
with open(txt_file, 'r') as f:
lines = [[list(map(float, point.split())) for point in line.strip().split()] for line in f]
return lines # 返回每个lane对应的一系列point列表
```
#### (2) 将 lane points 转换成 polygon mask 或 bounding box 形式
如果目标是生成 Mask R-CNN 所需输入的话就需要进一步加工了;若只是简单的边界框模式则可以直接计算包围盒范围即可。
#### (3) 构建最终输出结果并存入 json 中
利用 Python 内置库 `json.dump()` 函数完成序列化过程,并指定适当的缩进让其更易读取查看。
完整示例如下所示:
```python
import os
import cv2
import json
# ... (前面省略部分内容)
if __name__ == "__main__":
dataset_dir = '/path/to/culane/'
images_info = []
annotations = []
img_id = ann_id = 0
splits = ['train', 'val']
for split in splits:
label_root = os.path.join(dataset_dir, "labels", split)
files = [os.path.splitext(f)[0] for f in os.listdir(label_root)]
for file_name in files:
lanes_points = parse_culane_txt(os.path.join(label_root, '{}.lines.txt'.format(file_name)))
image_path = os.path.join('color_images', split, "{}.jpg".format(file_name))
height,width,_=cv2.imread(image_path).shape
image={
"file_name":image_path,
"id":img_id,
"height":height ,
"width" : width }
images_info.append(image)
for i,lane_pts in enumerate(lanes_points,start=ann_id):
poly=[[x,y]for x ,y in lane_pts ]
annotation={"segmentation":[poly],
"area": calculate_area(poly),
"iscrowd":False ,
"image_id ":img_id ,
"bbox" :get_bounding_box(poly) ,
"category_id ":1 ,
"id":i}
annotations.append(annotation )
ann_id +=len(annotations )
img_id+=1
output_dict={'info':{},"licenses":[],"categories":[{"supercategory":"none","id":1,"name":"lane"}],'images':images_info,'annotations':annotations}
with open('./culane_converted.json','w')as outfile :
json.dump(output_dict,outfile ,indent=4)
print("Conversion Done!")
```
阅读全文
相关推荐
















