-
Notifications
You must be signed in to change notification settings - Fork 2
/
lane_data_gen_culanes.py
131 lines (98 loc) · 3.17 KB
/
lane_data_gen_culanes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#
# Include Setting
#
import cv2
import pandas
import numpy as np
import glob
import os
import json
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as mpatches
from matplotlib.path import Path
from skimage.draw import line, bezier_curve
from tqdm import tqdm
BASE_PATH = "G:/Datasets/CULane/"
POINT_COUNT = 50
VAL_COUNT = 6000
TRAIN_COUNT = 30000
IMAGES = glob.glob(BASE_PATH+"/**/**/*.jpg")
IMAGE_SIZE = (720,1280)
test_image = cv2.imread(IMAGES[0])
IMAGE_SIZE = test_image.shape
print(IMAGE_SIZE)
def load_label_file(path):
with open(path) as contents:
lines = contents.readlines()
lanes = []
for line in lines:
line = line.replace(" \n", "")
coords_str = line.split(" ")
coords = []
true_idx = 0
for i in range(int(len(coords_str)/2)):
coords.append((float(coords_str[true_idx]),float(coords_str[true_idx + 1])))
true_idx += 2
lanes.append(coords)
return lanes
def split(data, nTrain, nVal):
offset = 0
train = []
val = []
for i in tqdm(range(nTrain)):
train.append(data[i])
for i in tqdm(range(nVal)):
val.append(data[nTrain + i])
return (train, val)
def draw(image, points, color):
for i in range(len(points)- 1 ):
vert = points[i]
next_vert = points[i + 1];
if vert[0] < 1:
continue
if next_vert[0] < 1:
continue
y1 = int(vert[0] / DOWNSCALE)
x1 = int(vert[1] / DOWNSCALE)
y2 = int(next_vert[0] / DOWNSCALE)
x2 = int(next_vert[1] / DOWNSCALE)
rr, cc = line(x1,y1,x2,y2)
rr = np.clip(rr, 0, int(IMAGE_SIZE[0] / DOWNSCALE) - 2)
cc = np.clip(cc, 0, int(IMAGE_SIZE[1] / DOWNSCALE) -2)
image[rr ,cc, :] = 1.0
image[rr ,cc - 1, :] = 1.0
image[rr ,cc + 1, :] = 1.0
image[rr - 1 ,cc , :] = 1.0
image[rr + 1 ,cc , :] = 1.0
image[rr - 1 ,cc - 1, :] = 1.0
image[rr + 1 ,cc + 1, :] = 1.0
return image
DOWNSCALE = 2
def parse(image_path):
label_path = image_path.replace(".jpg", ".lines.txt");
label = load_label_file(label_path)
image = cv2.imread(image_path)
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (256, 128))
image = cv2.normalize(image.astype('float'), None,
0.0, 1.0, cv2.NORM_MINMAX)
lane_img = np.zeros([int(IMAGE_SIZE[0] / DOWNSCALE),int(IMAGE_SIZE[1] / DOWNSCALE),3])
for lane in label:
lane_img = draw(lane_img,lane, (1.0,0,0))
lane_img = cv2.resize(lane_img, (256, 128))
return [image, lane_img]
print("LABEL COUNT: " + str(len(IMAGES)))
print("--- SPLITTING LABELES --- ")
train_data, val_data = split(IMAGES, TRAIN_COUNT, VAL_COUNT);
print("--- PARSING LABELES --- ")
index = 0
for label in tqdm(train_data):
data = parse(label)
np.save("data/lane/train/culane-"+str(index)+".npy", data)
index += 1
index = 0
for label in tqdm(val_data):
data = parse(label)
np.save("data/lane/val/culane-"+str(index)+".npy", data)
index += 1