Skip to content

Commit 2b8d14f

Browse files
Added result class to simplify individual follower.
Seriously need to reconsider structure of line_window1 & line_window2: These lines are only for rendering, but seem to be half running in the loop and half out. This doesn't make a lot of sense? I think they can be fully replaced with the resultant np.polyval
1 parent 8c363c8 commit 2b8d14f

File tree

2 files changed

+74
-45
lines changed

2 files changed

+74
-45
lines changed

components/lane_follower/src/individual_follower.py

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
import cv2
44
import numpy as np
55

6-
from einops import rearrange
6+
from einops import repeat
77

88

9-
class if_result:
9+
class ifResult:
10+
def __init__(self):
11+
self.result_img = None
12+
self.empty_windows = 0
13+
self.heading = 0
14+
self.slope = 0
15+
1016
pass
1117

1218

@@ -27,8 +33,8 @@ def __init__(self):
2733
# self.fit is set in Plot_Line
2834
self.fit = None
2935
# These two are set below
30-
self._binary_warped = None | np.ndarray
31-
self._histogram = None | np.ndarray
36+
self._binary_warped = None
37+
self._histogram = None
3238

3339
# Need to determine if these should remain properties
3440
# Or if they should be passed as arguments to Plot_Line
@@ -39,22 +45,26 @@ def _binary_warped(self, value: np.ndarray):
3945
self.binary_warped[self.binary_warped.shape[0] // 2 :, :], axis=0
4046
)
4147

42-
def plot_line(self) -> if_result:
48+
def plot_line(self) -> ifResult:
4349
if not self._binary_warped:
4450
raise Exception("no binary warp specified")
4551

4652
# Image to visualize output
47-
out_img = rearrange()
48-
(
49-
np.dstack(
50-
(self._binary_warped, self._binary_warped, self._binary_warped)
51-
)
52-
* 255
53-
)
53+
out_img = repeat(self._binary_warped, 'h w -> h w c', repeat=3) * 255
54+
55+
##These outputs need to be confirmed compatible
56+
# out_img = (
57+
# np.dstack(
58+
# self._binary_warped, self._binary_warped, self._binary_warped
59+
# )
60+
# * 255
61+
# )
5462

5563
window_height = np.int32(
5664
self._binary_warped.shape[0] / IndividualFollower.NWINDOWS
5765
)
66+
##Create result class:
67+
result = ifResult()
5868

5969
lane_inds = []
6070

@@ -99,7 +109,8 @@ def plot_line(self) -> if_result:
99109
empty_windows += 1
100110

101111
if len(lane_inds) == 0:
102-
return None, 0, 0, 0
112+
return result
113+
103114
lane_array = np.concatenate(lane_inds)
104115
lane_x_pos = nonzero_x[lane_array]
105116
lane_y_pos = nonzero_y[lane_array]
@@ -124,34 +135,55 @@ def plot_line(self) -> if_result:
124135
##Generates the search window area
125136
window_img = np.zeros_like(out_img)
126137
##These lines should be broken up accordingly: They render the search area
127-
line_window1 = np.array(
138+
139+
window_1 = np.vstack(
128140
[
129-
np.transpose(
130-
np.vstack(
131-
[
132-
polynomial - IndividualFollower.SEARCH_WIDTH,
133-
plotting_coordinates,
134-
]
135-
)
136-
)
141+
polynomial - IndividualFollower.SEARCH_WIDTH,
142+
plotting_coordinates,
137143
]
138-
)
139-
line_window2 = np.array(
140-
[
141-
np.transpose(
142-
np.vstack(
143-
[
144-
polynomial + IndividualFollower.SEARCH_WIDTH,
145-
plotting_coordinates,
146-
]
147-
)
148-
)
149-
]
150-
)
151-
line_pts = np.hstack((line_window1, line_window2))
144+
).T
145+
146+
window_2 = np.vstack(
147+
[
148+
polynomial - IndividualFollower.SEARCH_WIDTH,
149+
plotting_coordinates,
150+
]
151+
).T
152+
153+
# These replacements also need to be tested
154+
155+
# line_window1 =
156+
# np.array(
157+
# [
158+
# np.transpose(
159+
# np.vstack(
160+
# [
161+
# polynomial - IndividualFollower.SEARCH_WIDTH,
162+
# plotting_coordinates,
163+
# ]
164+
# )
165+
# )
166+
# ]
167+
# )
168+
# line_window2 = np.array(
169+
# [
170+
# np.transpose(
171+
# np.vstack(
172+
# [
173+
# polynomial + IndividualFollower.SEARCH_WIDTH,
174+
# plotting_coordinates,
175+
# ]
176+
# )
177+
# )
178+
# ]
179+
# )
180+
181+
line_pts = np.hstack((window_1, window_2))
152182
cv2.fillPoly(
153183
window_img, np.int_([line_pts]), color=IndividualFollower.BOX_COLOR
154184
)
185+
# This seems to be a lot of numpy fluff that should be replaced with einops
186+
# or removed altogether.
155187
line_pts = np.array(
156188
[np.transpose(np.vstack([polynomial, plotting_coordinates]))],
157189
dtype=np.int32,
@@ -168,10 +200,11 @@ def plot_line(self) -> if_result:
168200
##TODO: Make this use the furthest found box. This way, we'll use the most accurate heading
169201
y_lower = 0
170202
y_upper = (IndividualFollower.NWINDOWS + 1) * window_height
171-
slope = np.polyval(self.fit, y_lower) - np.polyval(
203+
204+
result.slope = np.polyval(self.fit, y_lower) - np.polyval(
172205
self.fit, y_upper
173206
) / (y_upper - y_lower)
174-
heading = math.atan(slope)
175-
result = cv2.addWeighted(out_img, 1, window_img, 0.3, 0)
176-
# TODO: Determine what result really is, and annotate efficiently
177-
return result, empty_windows, heading, slope
207+
result.result_img = cv2.addWeighted(out_img, 1, window_img, 0.3, 0)
208+
result.heading = math.atan(result.slope)
209+
210+
return result

components/lane_follower/src/pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
[project]
22
version = '1.0'
33
name = "individual-follower"
4-
dependencies = [
5-
"cv2",
6-
"numpy",
7-
]
84
authors = [
95
{name = "Vaibhav Hariani"},
106
{name = "Isaiah Rivera"},

0 commit comments

Comments
 (0)