-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlane_data_gen.py
167 lines (130 loc) · 3.93 KB
/
lane_data_gen.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#
# 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
#
# Data Path Setting
#
DATA_PATH_BASE = "G:/Datasets/Berkly/"
INPUT_DATA_BASE = DATA_PATH_BASE + "input/"
INPUT_IMAGES = INPUT_DATA_BASE + "images/"
INPUT_LABELS = INPUT_DATA_BASE + "labels/"
TRAIN_IMAGES = INPUT_IMAGES + "train/"
VAL_IMAGES = INPUT_IMAGES + "val/"
TRAIN_LABELS = INPUT_LABELS + "bdd100k_labels_images_train.json"
VAL_LABELS = INPUT_LABELS + "bdd100k_labels_images_val.json"
#
# Data State
#
STATE_OFFESET = 0
STATE_INDEX = 0
#
# Settings
#
VAL_LOAD = 3000
TRAIN_LOAD = 10000
DOWNSCALE = 4
VAL_START = 40
#
# Load Labels into Memory
#
def load_label_file(path):
with open(path) as json_file:
data = json.load(json_file)
return data
print ("--- LOADING VALIDATION LABELES --- ")
val_file = load_label_file(VAL_LABELS)
#
# Parse Labels
#
def parse_label(entry):
image_name = entry['name']
labels = entry['labels']
lanes = []
formatted_data = []
for label in labels:
cat = label['category']
if cat not in 'lane':
continue
if label['attributes']['laneDirection'] not in "parallel":
#print(label['attributes']['laneDirection'] )
continue
polygon = label['poly2d'][0]
verts = polygon['vertices']
types = polygon['types']
codes = []
moves = {'L': Path.LINETO,'C': Path.CURVE4}
codes = [moves[t] for t in types]
codes[0] = Path.MOVETO
lanes.append([codes,verts])
formatted_data.append([image_name, lanes])
return formatted_data
#
# Image Generation
#
def label_to_image(label):
lines = label[1]
image = np.zeros([int(720 / DOWNSCALE),int(1280 / DOWNSCALE),3])
for cur in lines:
verts = cur[1]
control = cur[0]
offset = 1
if 4 in control:
path = Path(verts, control)
patch = mpatches.PathPatch(path, snap=True, lw=10, edgecolor=(1.0,1.0,1.0))
verts = patch.get_verts()
offset = 2
for i in range(len(verts)- offset ):
vert = verts[i]
next_vert = verts[i + 1];
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(720 / DOWNSCALE) - 2)
cc = np.clip(cc, 0, int(1280 / 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
image = cv2.resize(image, (254, 126))
return image
def get_source(path):
image = cv2.imread(path)
image = cv2.normalize(image.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
image = cv2.resize(image, (254,126))
return image
print ("--- GENERATING VALIDATION DATA ---")
for i in tqdm(range(VAL_LOAD)):
entry = val_file[i]
labels = parse_label(entry)
for label in labels:
data_entry = [get_source(VAL_IMAGES + label[0]), label_to_image(label)]
np.save("data/lane/val/bddlane-"+str(STATE_INDEX)+".npy", data_entry);
STATE_INDEX += 1
print ("--- LOADING TRAINING LABELES --- ")
train_file = load_label_file(TRAIN_LABELS)
STATE_INDEX = 0
print ("--- GENERATING TRAINING DATA ---")
for i in tqdm(range(TRAIN_LOAD)):
entry = train_file[i]
labels = parse_label(entry)
for label in labels:
data_entry = [get_source(TRAIN_IMAGES + label[0]), label_to_image(label)]
np.save("data/lane/train/bddlane-"+str(STATE_INDEX)+".npy", data_entry);
STATE_INDEX += 1