tusimple数据集(json)转换脚本

 Lanenet开源代码,用tusimple数据集进行训练,需要将.json数据集转换为readme中的图像格式。于是,手动写个python脚本转换工具。

lanenet github地址:https://github.com/MaybeShewill-CV/lanenet-lane-detection

tusimple数据集下载地址:http://benchmark.tusimple.ai/#/t/1/dataset 

import cv2
import json
import numpy as np

file=open('label_data_0601.json','r')
image_num=0
for line in file.readlines():
    data=json.loads(line)
    image=cv2.imread(data['raw_file'])
    binaryimage=np.zeros((image.shape[0],image.shape[1],1),np.uint8)
    instanceimage=binaryimage.copy()
    arr_width=data['lanes']
    arr_height=data['h_samples']
    width_num=len(arr_width)
    height_num=len(arr_height)
    for i in range(height_num):
        lane_hist=20
        for j in range(width_num):
            if arr_width[j][i-1]>0 and arr_width[j][i]>0:
                binaryimage[int(arr_height[i]),int(arr_width[j][i])]=255
                instanceimage[int(arr_height[i]),int(arr_width[j][i])]=lane_hist
                if i>0:
                    cv2.line(binaryimage, (int(arr_width[j][i-1]),int(arr_height[i-1])), (int(arr_width[j][i]),int(arr_height[i])), 255, 10)
                    cv2.line(instanceimage,(int(arr_width[j][i-1]),int(arr_height[i-1])), (int(arr_width[j][i]),int(arr_height[i])), lane_hist, 10)
            lane_hist+=50
    string1="gt_image_binary/"+str(image_num)+".png"
    string2="gt_image_instance/"+str(image_num)+".png"
    string3="image/"+str(image_num)+".png"
    cv2.imwrite(string1,binaryimage)
    cv2.imwrite(string2,instanceimage)
    cv2.imwrite(string3,image)
    image_num=image_num+1
file.close()

 这样就生成了区分车道线的二值图、对应车道线不同实例的灰度图和原图,如图所示:

tusimple数据集(json)转换脚本_第1张图片

tusimple数据集(json)转换脚本_第2张图片

tusimple数据集(json)转换脚本_第3张图片

存在对应的文件夹内 

 

再将按4:1的比例生成训练集与测试集脚本,代码如下:

import random

def divide(p,list):
    train=[]
    val=[]
    for x in list:
        if random.random()<=p:
            train.append(list[x])
        else:
            val.append(list[x])
    return train,val

fo1=open("train.txt","w")
fo2=open("val.txt","w")
num=3625
list=[]
for i in range(0,num+1):
    list.append(i)
p=0.8
train,val=divide(p,list)
str1="/home/zdw/lanenet-lane-detection/data/training_data_example/image/" 
str3=".png /home/zdw/lanenet-lane-detection/data/training_data_example/gt_image_binary/"
str4=".png /home/zdw/lanenet-lane-detection/data/training_data_example/gt_image_instance/"
str5=".png\n"
for i in train:
    str2=str(i)
    str0=str1+str2+str3+str2+str4+str2+str5
    fo1.write(str0)
    num=num+1
fo1.close()
for i in val:
    str2=str(i)
    str0=str1+str2+str3+str2+str4+str2+str5
    fo2.write(str0)
    num=num+1
fo2.close()

 

 

你可能感兴趣的:(深度学)