Skip to content

Commit e5fd3d1

Browse files
Broke individual follower up into loop & not loop
1 parent 20d1e0b commit e5fd3d1

File tree

1 file changed

+18
-76
lines changed

1 file changed

+18
-76
lines changed

components/lane_follower/src/individual_follower.py

Lines changed: 18 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ def _binary_warped(self, value: np.ndarray):
3636
self.binary_warped[self.binary_warped.shape[0] // 2 :, :], axis=0
3737
)
3838

39-
def get_white_pixels(self, window, window_height, out_img=None):
39+
def get_white_pixels(self):
40+
# Returns an output image
4041
NWINDOWS = self.consts["NWINDOWS"]
4142
SEARCH_WIDTH = self.consts['SEARCH_WIDTH']
43+
44+
window_height = np.int32(self._binary_warped.shape[0] / NWINDOWS)
4245
lane_inds = []
4346

4447
nonzero_pixels = self._binary_warped.nonzero()
@@ -48,59 +51,6 @@ def get_white_pixels(self, window, window_height, out_img=None):
4851
empty_windows = 0
4952
lane_base = np.argmax(self.histogram[:])
5053

51-
for window in range(NWINDOWS):
52-
window_dims = window * window_height
53-
win_y_upper = self._binary_warped.shape[0] - window_dims
54-
# One window height lower than win_y_higher
55-
win_y_lower = win_y_upper - window_height
56-
win_x_lower = lane_base - SEARCH_WIDTH
57-
win_x_upper = lane_base + SEARCH_WIDTH
58-
59-
lower_coords = (win_x_lower, win_y_lower)
60-
upper_coords = (win_x_upper, win_y_lower)
61-
cv2.rectangle(
62-
out_img,
63-
lower_coords,
64-
upper_coords,
65-
self.consts["BOX_COLOR"],
66-
self.consts["DRAW_THICKNESS"],
67-
)
68-
69-
white_pix_inds = (
70-
(nonzero_y >= win_y_lower)
71-
& (nonzero_y < win_y_upper)
72-
& (nonzero_x >= win_x_lower)
73-
& (nonzero_x >= win_x_upper)
74-
).nonzero_pixels()[0]
75-
76-
# This should likely be moved into the if statement: leaving for now
77-
lane_inds.append(white_pix_inds)
78-
if len(white_pix_inds) > self.consts["MINPIXELS"]:
79-
# np.mean will return a float: We need an exact value
80-
lane_base = np.int32(np.mean(nonzero_x[white_pix_inds]))
81-
else:
82-
empty_windows += 1
83-
84-
# if len(lane_inds) == 0:
85-
# return result
86-
87-
lane_array = np.concatenate(lane_inds)
88-
lane_x_pos = nonzero_x[lane_array]
89-
lane_y_pos = nonzero_y[lane_array]
90-
# TODO: test if this statement is necessary
91-
if lane_x_pos.any() and lane_y_pos.any():
92-
self._fit = np.polyfit(
93-
lane_y_pos, lane_x_pos, self.consts["LANE_POLY_SIZE"]
94-
)
95-
96-
out_img[nonzero_y[lane_array], nonzero_x[lane_array]] = (
97-
self.consts["LANE_COLOR"]
98-
)
99-
100-
def plot_line(self) -> ifResult:
101-
if not self._binary_warped:
102-
raise Exception("no binary warp specified")
103-
10454
# Image to visualize output
10555
out_img = repeat(self._binary_warped, 'h w -> h w c', repeat=3) * 255
10656
##These outputs need to be confirmed compatible
@@ -111,22 +61,6 @@ def plot_line(self) -> ifResult:
11161
# * 255
11262
# )
11363

114-
NWINDOWS = self.consts['NWINDOWS']
115-
116-
window_height = np.int32(self._binary_warped.shape[0] / NWINDOWS)
117-
##Create result class:
118-
result = ifResult()
119-
120-
lane_inds = []
121-
122-
nonzero_pixels = self._binary_warped.nonzero()
123-
nonzero_y = np.array(nonzero_pixels[0])
124-
nonzero_x = np.array(nonzero_pixels[1])
125-
126-
empty_windows = 0
127-
lane_base = np.argmax(self.histogram[:])
128-
129-
SEARCH_WIDTH = self.consts['SEARCH_WIDTH']
13064
for window in range(NWINDOWS):
13165
window_dims = window * window_height
13266
win_y_upper = self._binary_warped.shape[0] - window_dims
@@ -159,10 +93,8 @@ def plot_line(self) -> ifResult:
15993
lane_base = np.int32(np.mean(nonzero_x[white_pix_inds]))
16094
else:
16195
empty_windows += 1
162-
16396
if len(lane_inds) == 0:
164-
return result
165-
97+
return None
16698
lane_array = np.concatenate(lane_inds)
16799
lane_x_pos = nonzero_x[lane_array]
168100
lane_y_pos = nonzero_y[lane_array]
@@ -175,17 +107,27 @@ def plot_line(self) -> ifResult:
175107
out_img[nonzero_y[lane_array], nonzero_x[lane_array]] = (
176108
self.consts["LANE_COLOR"]
177109
)
110+
return out_img
111+
112+
def plot_line(self) -> ifResult:
113+
if not self._binary_warped:
114+
raise Exception("no binary warp specified")
115+
116+
result = ifResult()
117+
118+
NWINDOWS = self.consts["NWINDOWS"]
119+
window_height = np.int32(self._binary_warped.shape[0] / NWINDOWS)
120+
out_img = self.get_white_pixels()
121+
if out_img is None:
122+
return result
178123

179124
##Generates the search window area
180125
window_img = np.zeros_like(out_img)
181-
182126
# Create linearly spaced points at each height, evaluate polynomial, create coordinates
183127
x_val = np.arange(0, window_height, step=5).T
184128
y_val = np.polyval(self.fit, x_val)
185129
coords = np.concatenate((x_val, y_val), axis=0)
186-
187130
##TODO: Test if this approach works
188-
189131
cv2.polylines(
190132
out_img,
191133
coords,

0 commit comments

Comments
 (0)