-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
312 lines (227 loc) · 7.95 KB
/
utils.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
import math
import cv2
import numpy as np
# cv2.namedWindow('trackbar')
# cv2.createTrackbar('l_h', 'trackbar', 0, 255, lambda x: x)
# cv2.createTrackbar('l_s', 'trackbar', 0, 255, lambda x: x)
# cv2.createTrackbar('l_v', 'trackbar', 200, 255, lambda x: x)
# cv2.createTrackbar('u_h', 'trackbar', 255, 179, lambda x: x)
# cv2.createTrackbar('u_s', 'trackbar', 50, 255, lambda x: x)
# cv2.createTrackbar('u_v', 'trackbar', 255, 255, lambda x: x)
# find left line
def left_line(x):
return 1.4536741214057507 * x + 24
def right_line(x):
return -1.6447876447876448 * x + 1104.019305019305
# find the steering angle for turning the car
def find_first_left_point(line, mask):
for x in range(344, 0, -3):
y = int(line(x))
if 480 > y > 0 and mask[y][x] > 200:
return y, x
return None
def find_first_right_point(line, mask):
for x in range(344, 639, 3):
y = int(line(x))
if 480 > y > 0 and mask[y][x] > 200:
return y, x
return None
def find_first_center_point(mask):
for y in range(479, 0, -3):
if mask[y][344] == 255:
return y, 344
return None
# Perspective Transformation
def bird_eye_view(image):
tl = (88, 222)
tr = (416, 224)
bl = (7, 378)
br = (475, 379)
cv2.circle(image, tl, 5, (0, 0, 255), -1)
cv2.circle(image, tr, 5, (0, 0, 255), -1)
cv2.circle(image, bl, 5, (0, 0, 255), -1)
cv2.circle(image, br, 5, (0, 0, 255), -1)
# apply the perspective transformation
pts1 = np.float32([tl, tr, bl, br])
pts2 = np.float32([[0, 0], [640, 0], [0, 480], [640, 480]])
# Matrix to warp the image for bird's eye view
matrix = cv2.getPerspectiveTransform(pts1, pts2)
transformed = cv2.warpPerspective(image, matrix, (640, 480))
return transformed
# Object Detection
def extract_lines(image):
# Image Thresholding
hsv_transformed = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# trackbar to find the best values for the mask
# l_h = cv2.getTrackbarPos('l_h', 'trackbar')
# l_s = cv2.getTrackbarPos('l_s', 'trackbar')
# l_v = cv2.getTrackbarPos('l_v', 'trackbar')
# u_h = cv2.getTrackbarPos('u_h', 'trackbar')
# u_s = cv2.getTrackbarPos('u_s', 'trackbar')
# u_v = cv2.getTrackbarPos('u_v', 'trackbar')
l_h = 0
l_s = 0
l_v = 197
u_h = 179
u_s = 255
u_v = 255
lower = np.array([l_h, l_s, l_v])
upper = np.array([u_h, u_s, u_v])
mask = cv2.inRange(hsv_transformed, lower, upper)
return mask
def sign(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
# find the steering angle for drive between the lines
def drive_in_lines(left, right, center):
left_y, left_x = 0, 0
right_y, right_x = 0, 0
center_y, center_x = 0, 0
if left is not None:
left_y, left_x = left
if right is not None:
right_y, right_x = right
if center is not None:
center_y, center_x = center
if left is not None and right is not None:
if center is not None:
if center_y == left_y:
return 90
if center_y == right_y:
return -90
angle_between_left_and_center = (center_x - left_x) / (-center_y + left_y)
angle_between_left_and_center = math.degrees(math.atan(angle_between_left_and_center))
angle_between_right_and_center = (right_x - center_x) / (-right_y + center_y)
angle_between_right_and_center = math.degrees(math.atan(angle_between_right_and_center))
if abs(angle_between_left_and_center - angle_between_right_and_center) < 20:
return sign(angle_between_left_and_center) * 90
angle = (-right_y + left_y) / (right_x - left_x)
angle = math.degrees(math.atan(angle))
return angle
elif right is None and left is not None and center is not None:
angle = math.degrees(math.atan((center_y - left_y) / (center_x - left_x)))
return -angle
elif left is None and right is not None and center is not None:
angle = math.degrees(math.atan((center_y - right_y) / (center_x - right_x)))
return -angle
elif left is None and center is None and right is not None:
return 90
elif right is None and center is None and left is not None:
return -90
else:
return 0
def show_adjusting_circles(image, left, right, center):
if left is not None:
left_y, left_x = left
cv2.circle(image, (left_x, left_y), 5, (0, 0, 255), -1)
if right is not None:
right_y, right_x = right
cv2.circle(image, (right_x, right_y), 5, (0, 0, 255), -1)
if center is not None:
center_y, center_x = center
cv2.circle(image, (center_x, center_y), 5, (0, 0, 255), -1)
def distance_to_left_line(center_x, center_y, mask):
for x in range(center_x, 0, -1):
if mask[center_y][x] > 200:
return center_x - x
return -1
def distance_to_right_line(center_x, center_y, mask):
for x in range(center_x, 640):
if mask[center_y][x] > 200:
return x - center_x
return -1
def turn_left(mask, left, right, center):
center_x = 370
center_y = 470
if distance_to_left_line(center_x, center_y, mask) > 0:
return -30, 2
else:
return drive_in_lines(left, right, center), 3
def keep_straight(left, right, center, flag):
return drive_in_lines(left, right, center), flag + 1
def turn_right(mask, left, right, center, frame):
center_x = 370
center_y = 470
if distance_to_right_line(center_x, center_y, mask) > 0:
return 30, frame
else:
return drive_in_lines(left, right, center), 0
def get_over_obstacle(mask, left, right, center, flag, frame):
if flag < 3:
return turn_left(mask, left, right, center)
elif flag < frame:
return keep_straight(left, right, center, flag)
else:
return turn_right(mask, left, right, center, frame)
def cal_speed(car_angle):
if abs(car_angle) < 10:
car_speed = 100
elif abs(car_angle) < 15:
car_speed = 95
elif abs(car_angle) < 20:
car_speed = 90
elif abs(car_angle) < 25:
car_speed = 85
elif abs(car_angle) < 30:
car_speed = 55
elif abs(car_angle) < 35:
car_speed = 20
elif abs(car_angle) < 40:
car_speed = 10
elif abs(car_angle) < 55:
car_speed = 5
elif abs(car_angle) < 55:
car_speed = 3
elif abs(car_angle) < 70:
car_speed = 2
else:
car_speed = 0
return car_speed
tt = 0
def drive(car, flag):
global tt
image = car.getImage()
sensor = car.getSensors()
transformed = bird_eye_view(image)
mask = extract_lines(transformed)
# find the left line
left = find_first_left_point(left_line, mask)
right = find_first_right_point(right_line, mask)
center = find_first_center_point(mask)
show_adjusting_circles(transformed, left, right, center)
if flag == 0 and (sensor[2] < 1300):
flag = 1
if flag == 0:
tt = 0
angle = drive_in_lines(left, right, center)
car.setSensorAngle(5)
if (sensor[2] < 1499) and car.getSpeed() > 40:
car_speed = 0
else:
car_speed = cal_speed(angle)
else:
if tt == 1 or car.getSpeed() > 60:
frame = 100
tt = 1
if tt == 0:
frame = 45
angle, flag = get_over_obstacle(mask, left, right, center, flag, frame)
car_speed = cal_speed(angle)
if car.getSpeed() > 60:
car_speed = 0
if abs(angle) > 30 and car.getSpeed() > 50:
car_speed = 35
elif abs(angle) > 20 and car.getSpeed() > 50:
car_speed = 45
car.setSteering(angle)
car.setSpeed(car_speed)
if image is not None and image.any():
# Showing the opencv type image
cv2.imshow('frames', image)
cv2.imshow('transformed', transformed)
cv2.imshow('mask', mask)
return flag