-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlane_detection_node2.py
322 lines (279 loc) · 14.8 KB
/
lane_detection_node2.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from std_msgs.msg import Float32, Int32, Int32MultiArray
import cv2
from cv_bridge import CvBridge
import numpy as np
import os
# Nodes in this program
NODE_NAME = 'lane_detection_node'
# Topics subcribed/published to in this program
CAMERA_TOPIC_NAME = '/camera/color/image_raw'
CENTROID_TOPIC_NAME = '/centroid'
class LaneDetection(Node):
def __init__(self):
super().__init__(NODE_NAME)
self.centroid_error_publisher = self.create_publisher(Float32, CENTROID_TOPIC_NAME, 10)
self.centroid_error_publisher
self.centroid_error = Float32()
self.camera_subscriber = self.create_subscription(Image, CAMERA_TOPIC_NAME, self.locate_centroid, 10)
self.camera_subscriber
self.bridge = CvBridge()
self.max_num_lines_detected = 10
self.image_width = 0
self.image_height = 0
self.start_height = 0
self.bottom_height = 0
self.left_width = 0
self.right_width = 0
self.error_threshold = 0.1
self.declare_parameters(
namespace='',
parameters=[
('Hue_low', 1),
('Hue_high', 1),
('Saturation_low', 1),
('Saturation_high', 1),
('Value_low', 1),
('Value_high', 1),
('gray_lower', 1),
('inverted_filter', 0),
('kernal_size',1),
('erosion_itterations',1),
('dilation_itterations',1),
('number_of_lines', 0),
('error_threshold', 0),
('Width_min', 1),
('Width_max', 1),
('crop_width_decimal',0.8),
('rows_to_watch_decimal',0.2),
('rows_offset_decimal',0.5),
('camera_centerline',0.5),
('debug_cv', 0)
])
self.Hue_low = self.get_parameter('Hue_low').value
self.Hue_high = self.get_parameter('Hue_high').value
self.Saturation_low = self.get_parameter('Saturation_low').value
self.Saturation_high = self.get_parameter('Saturation_high').value
self.Value_low = self.get_parameter('Value_low').value
self.Value_high = self.get_parameter('Value_high').value
self.gray_lower = self.get_parameter('gray_lower').value
self.inverted_filter = self.get_parameter('inverted_filter').value
self.kernal_size = self.get_parameter('kernal_size').value
self.erosion_itterations = self.get_parameter('erosion_itterations').value
self.dilation_itterations = self.get_parameter('dilation_itterations').value
self.number_of_lines = self.get_parameter('number_of_lines').value
self.error_threshold = self.get_parameter('error_threshold').value
self.min_width = self.get_parameter('Width_min').value
self.max_width = self.get_parameter('Width_max').value
self.crop_width_decimal = self.get_parameter('crop_width_decimal').value
self.rows_to_watch_decimal = self.get_parameter('rows_to_watch_decimal').value
self.rows_offset_decimal = self.get_parameter('rows_offset_decimal').value
self.camera_centerline = self.get_parameter('camera_centerline').value
self.debug_cv = self.get_parameter('debug_cv').value
self.camera_init = False
self.previous_error = 0.0 # Add this line to store previous error
self.no_detection_count = 0 # Add this to track consecutive no-detections
self.max_no_detection_uses = 1000 # Maximum number of times to use previous error
self.get_logger().info(
f'\nHue_low: {self.Hue_low}'
f'\nHue_high: {self.Hue_high}'
f'\nSaturation_low: {self.Saturation_low}'
f'\nSaturation_high: {self.Saturation_high}'
f'\nValue_low: {self.Value_low}'
f'\nValue_high: {self.Value_high}'
f'\ngray_lower: {self.gray_lower}'
f'\ninverted_filter: {self.inverted_filter}'
f'\nkernal_size: {self.kernal_size}'
f'\nerosion_itterations: {self.erosion_itterations}'
f'\ndilation_itterations: {self.dilation_itterations}'
f'\nnumber_of_lines: {self.number_of_lines}'
f'\nerror_threshold: {self.error_threshold}'
f'\nmin_width: {self.min_width}'
f'\nmax_width: {self.max_width}'
f'\ncrop_width_decimal: {self.crop_width_decimal}'
f'\nrows_to_watch_decimal: {self.rows_to_watch_decimal}'
f'\nrows_offset_decimal: {self.rows_offset_decimal}'
f'\ncamera_centerline: {self.camera_centerline}'
f'\ndebug_cv: {self.debug_cv}')
def locate_centroid(self, data):
# Image processing from rosparams
frame = self.bridge.imgmsg_to_cv2(data)
if not self.camera_init:
self.get_logger().info(f'\n Initializing Camera...')
height, width, channels = frame.shape
# Vertical crop/pan
rows_to_watch = int(height * self.rows_to_watch_decimal)
rows_offset = int(height * (1 - self.rows_offset_decimal))
# Horizontal crop
self.start_height = int(height - rows_offset)
self.bottom_height = int(self.start_height + rows_to_watch)
self.left_width = int((width / 2) * (1 - self.crop_width_decimal))
self.right_width = int((width / 2) * (1 + self.crop_width_decimal))
self.camera_init = True
self.get_logger().info(f'\n Camera Initialized')
self.image_width = int(self.right_width - self.left_width)
self.image_height = self.bottom_height-self.start_height
img = frame[self.start_height:self.bottom_height, self.left_width:self.right_width]
# changing color space to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([self.Hue_low, self.Saturation_low, self.Value_low])
upper = np.array([self.Hue_high, self.Saturation_high, self.Value_high])
mask = cv2.inRange(hsv, lower, upper)
if self.inverted_filter == 1:
bitwise_mask = cv2.bitwise_and(hsv, hsv, mask=cv2.bitwise_not(mask))
else:
bitwise_mask = cv2.bitwise_and(hsv, hsv, mask=mask)
# changing to gray color space
gray = cv2.cvtColor(bitwise_mask, cv2.COLOR_BGR2GRAY)
# changing to black and white color space
gray_upper = 255
(dummy, blackAndWhiteImage) = cv2.threshold(gray, self.gray_lower, gray_upper, cv2.THRESH_BINARY)
# get rid of white noise from grass
kernel = np.ones((self.kernal_size, self.kernal_size), np.uint8)
blurred = cv2.blur(blackAndWhiteImage,(self.kernal_size, self.kernal_size))
erosion = cv2.erode(blurred, kernel, iterations = self.erosion_itterations)
dilation = cv2.dilate(erosion, kernel, iterations = self.dilation_itterations)
(dummy, blackAndWhiteImage) = cv2.threshold(dilation, self.gray_lower, gray_upper, cv2.THRESH_BINARY)
contours, dummy = cv2.findContours(blackAndWhiteImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Defining points of a line to be drawn for visualizing error
cam_center_line_x = int(self.image_width * self.camera_centerline)
start_point = (cam_center_line_x,0)
end_point = (cam_center_line_x, int(self.bottom_height))
start_point_thresh_pos_x = int(cam_center_line_x - (self.error_threshold * self.image_width/2))
start_point_thresh_neg_x = int(cam_center_line_x + (self.error_threshold * self.image_width/2))
start_point_thresh_pos = (start_point_thresh_pos_x, 0)
end_point_thresh_pos = (start_point_thresh_pos_x, int(self.bottom_height))
start_point_thresh_neg = (start_point_thresh_neg_x, 0)
end_point_thresh_neg = (start_point_thresh_neg_x, int(self.bottom_height))
# Setting up data arrays
cx_list = []
cy_list = []
old_error = 0.0
# plotting contours and their centroids
for contour in contours[:self.number_of_lines]:
[x, y], [w, h], phi = cv2.minAreaRect(contour)
rect = cv2.minAreaRect(contour)
if self.min_width < w < self.max_width:
try:
box = cv2.boxPoints(rect)
box = np.int0(box)
img = cv2.drawContours(img,[box], 0, (0, 255, 0), 3)
m = cv2.moments(contour)
cx = int(m['m10'] / m['m00'])
cy = int(m['m01'] / m['m00'])
cx_list.append(cx)
cy_list.append(cy)
cv2.circle(img, (cx, cy), 7, (0, 255, 0), -1)
img = cv2.line(img, start_point, end_point, (0,255,0), 4)
img = cv2.line(img, start_point_thresh_pos, end_point_thresh_pos, (0,0,255), 2)
img = cv2.line(img, start_point_thresh_neg, end_point_thresh_neg, (0,0,255), 2)
except ZeroDivisionError:
pass
# Further image processing to determine optimal steering value
try:
# When more than 1 road mark is found
if len(cx_list) > 1:
error_list = []
count = 0
# calculate errors for all detected road lines
for cx_pos in cx_list:
error = float((cx_pos - cam_center_line_x) / cam_center_line_x)
error_list.append(error)
# finding average error of all road lines
avg_error = (sum(error_list) / float(len(error_list)))
# check difference in error from closest to furthest road line
p_horizon_diff = abs(error_list[0] - error_list[-1])
# if path is approximately straight, then steer towards average error
if abs(p_horizon_diff) <= self.error_threshold:
error_x = avg_error
old_error = error_x
pixel_error = int(cam_center_line_x * (1 + error_x))
mid_x, mid_y = pixel_error, int((self.image_height/2))
self.get_logger().info(f"Straight curve: [tracking error: {error_x}], [tracking angle: {phi}]")
# if path is curved, then steer towards minimum error
else:
# exclude any road lines within error threshold by making their error large
for error in error_list:
if abs(error) < self.error_threshold:
error = 1
error_list[count] = error
count+=1
# getting min error (closest roadline)
error_x = min(error_list, key=abs)
old_error = error_x
# get index of min error for plotting
error_x_index = error_list.index(min(error_list, key=abs))
mid_x, mid_y = cx_list[error_x_index], cy_list[error_x_index]
self.get_logger().info(f"Curvy road: [tracking error: {error_x}], [tracking angle: {phi}]")
# plotting roadline to be tracked
cv2.circle(img, (mid_x, mid_y), 7, (255, 0, 0), -1)
start_point_error = (cam_center_line_x, mid_y)
img = cv2.line(img, start_point_error, (mid_x, mid_y), (0,0,255), 4)
# publish error data
self.centroid_error.data = float(error_x)
self.centroid_error_publisher.publish(self.centroid_error)
self.previous_error = error_x # Store successful error
self.no_detection_count = 0 # Reset counter on successful detection
# When only 1 road mark was found
elif len(cx_list) == 1:
mid_x, mid_y = cx_list[0], cy_list[0]
error_x = float((mid_x - cam_center_line_x) / cam_center_line_x)
old_error = error_x
start_point_error = (cam_center_line_x, mid_y)
img = cv2.line(img, start_point_error, (mid_x, mid_y), (0,0,255), 4)
cv2.circle(img, (mid_x, mid_y), 7, (0, 0, 255), -1)
self.centroid_error.data = error_x
self.centroid_error_publisher.publish(self.centroid_error)
self.get_logger().info(f"Only detected one line: [tracking error: {error_x}], [tracking angle: {phi}]")
self.previous_error = error_x # Store successful error
self.no_detection_count = 0 # Reset counter on successful detection
# When Nothing was found
else:
if self.no_detection_count < self.max_no_detection_uses:
error_x = self.previous_error
self.centroid_error.data = error_x
self.centroid_error_publisher.publish(self.centroid_error)
self.no_detection_count += 1
self.get_logger().warn(
f"No lines detected (count: {self.no_detection_count}). "
f"Using previous error: {error_x}"
)
else:
error_x = 0.0 # Default to center if we've used previous error too many times
self.centroid_error.data = error_x
self.centroid_error_publisher.publish(self.centroid_error)
self.get_logger().warn(
"No lines detected for too long. Defaulting to center."
)
# clean slate
error_list = [0] * self.number_of_lines
cx_list = []
cy_list = []
except ValueError:
pass
# plotting results
self.debug_cv = self.get_parameter('debug_cv').value # ability to update debug in real-time
if self.debug_cv:
cv2.imshow('img', img)
cv2.imshow('blackAndWhiteImage', blackAndWhiteImage)
cv2.waitKey(1)
else:
cv2.destroyAllWindows()
def main(args=None):
rclpy.init(args=args)
centroid_publisher = LaneDetection()
try:
rclpy.spin(centroid_publisher)
centroid_publisher.destroy_node()
rclpy.shutdown()
except KeyboardInterrupt:
centroid_publisher.get_logger().info(f'Shutting down {NODE_NAME}...')
# Kill cv2 windows and node
cv2.destroyAllWindows()
centroid_publisher.destroy_node()
rclpy.shutdown()
centroid_publisher.get_logger().info(f'{NODE_NAME} shut down successfully.')
if __name__ == '__main__':
main()