-
Notifications
You must be signed in to change notification settings - Fork 0
/
data preparation.py
59 lines (50 loc) · 2.6 KB
/
data preparation.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
import json
import os
import glob
import cv2
import numpy as np
path_labels = r"F:\Curvelanes\train\labels"
path_images = r"F:\Curvelanes\train\images"
for filename in glob.glob(os.path.join(path_labels, "*.json")):
with open(os.path.join(os.getcwd(), filename), "r") as f:
road = json.loads(f.read())
filename = filename[len(path_labels) + 1: -11] + ".png"
image = cv2.imread(path_images + "\\" + filename, cv2.IMREAD_GRAYSCALE)
lane_binary = np.zeros((len(image), len(image[1])))
for lanes in road["Lines"]:
lane_x = []
lane_y = []
for coordinates in lanes:
lane_x.append(float(coordinates["x"]))
lane_y.append(float(coordinates["y"]))
lane_function = []
for i in range(len(lane_y) - 1):
lane_function.append(np.poly1d(np.polyfit(lane_y[i:i + 2], lane_x[i: i + 2], 1)))
i = 0
for y in range(round(lane_y[-1]), round(lane_y[0])):
if y > lane_y[len(lane_y) - 1 - i - 1]:
i += 1
if lane_function[len(lane_function) - 1 - i](y) > len(lane_binary[0]):
break
if abs(lane_function[len(lane_function) - 1 - i].coeffs[0]) > 5:
for a in range(-3, 4):
if lane_function[len(lane_function) - 1 - i].coeffs[0] >= 0:
for b in range(round(-2 / abs(1 / lane_function[len(lane_function) - 1 - i].coeffs[0])), 4):
try:
lane_binary[y + a, round(lane_function[len(lane_function) - 1 - i](y)) + b] = 255
except Exception:
pass
else:
for b in range(-3, round(2 / abs(1 / lane_function[len(lane_function) - 1 - i].coeffs[0]))):
try:
lane_binary[y + a, round(lane_function[len(lane_function) - 1 - i](y)) + b] = 255
except Exception:
pass
else:
for a in range(-3, 4):
for b in range(-3, 4):
try:
lane_binary[y + a, round(lane_function[len(lane_function) - 1 - i](y)) + b] = 255
except Exception:
pass
cv2.imwrite(r'F:\\Curvelanes\\train\\labels2\\' + filename, lane_binary)