-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
152 lines (113 loc) · 3.9 KB
/
testing.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
import cv2
import numpy as np
PATH = 'test3.png'
PATHVID = 'test.mp4'
# thresholds for canny method
LOW_CANNY = 50
UPPPER_CANNY = 150
# ponits for triangular area for lane selection
P1 = 200
P2 = 1400
P3 = 530, 300
# (1 - Length) of lanes predicted, origin is top-left(decrease to increase len)
YCOR = 0.6
def region_wants(image):
''' remove the unwanted region, from the image and
returns part of image that needed'''
h = image.shape[0]
tri_area = np.array([[(P1,h), (P2,h), P3]])
mask = np.zeros_like(image)
# fill the tri_aea in the mask with white
cv2.fillPoly(mask, tri_area, 255)
# bitwise-and to delete the unwanted areas in image
image = cv2.bitwise_and(image, mask)
return image
def coordinates(image, line):
''' converts slope and intercept of a line
into its coordinates having restricted
y intercept '''
m, c = line
# return origin if no line was not found previously
if [m,c] == [0,0]:
return np.array([0,0,0,0])
y1 = image.shape[0]
# keeping y2 restricted to YCOR of image length
y2 = int(y1 * YCOR)
x1 = int((y1 - c) / m)
x2 = int((y2 - c) / m)
return np.array([x1, y1, x2, y2])
def average_lines_parameter(image, lines):
''' average the lines by averaging the slope and
intercept for left and right lane and
returns their coordinates '''
left_line = list()
rigth_line = list()
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line.flatten()
# get slope and intercept for the line
m, c = np.polyfit((x1, x2), (y1, y2), deg=1)
# add the appropriate line to its corresponding list
if m > 0:
left_line.append([m, c])
else:
rigth_line.append([m, c])
# average the lines parameters
if not left_line:
left_avg_line = np.array([0, 0])
else:
left_avg_line = np.average(left_line, axis=0)
if not rigth_line:
rigth_avg_line = np.array([0, 0])
else:
rigth_avg_line = np.average(rigth_line, axis=0)
# convert slopes, intercept to coordinates
left_avg_line = coordinates(image, left_avg_line)
rigth_avg_line = coordinates(image, rigth_avg_line)
return np.array([left_avg_line, rigth_avg_line])
def display_lines(image, lines):
''' takes an image and returns the image with
lines provided drawn on it'''
image_with_lines = np.zeros_like(image)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line.flatten()
# draw the line on the image_with_lines
cv2.line(image_with_lines, (x1, y1), (x2, y2), color=(0,255,0), thickness=10)
return image_with_lines
# image = cv2.imread(PATH)
# starting video capture
video = cv2.VideoCapture(PATHVID)
ret, frame = video.read()
# creating output video file
#out = cv2.VideoWriter('out0.9.avi', cv2.VideoWriter_fourcc(*"MJPG"),
# 30, (int(video.get(3)), int(video.get(4))))
kernel = np.ones((5,5), np.uint8)
while(ret):
# grey scale of the frame
img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# identifying the edges in frame using cannny method
# by computing gradient, to identify change in pixels
# also applies GaussianBlur fn to reduce noise
img = cv2.Canny(img, LOW_CANNY, UPPPER_CANNY)
# increase the width of the foreground objects making it easier to capture lines
img = cv2.dilate(img, kernel, iterations=1)
img = region_wants(img)
# get the lines from different almost in line points
lines = cv2.HoughLinesP(img, lines=np.array([]), rho=2, theta=np.pi/100, threshold=100, minLineLength=10, maxLineGap=5)
# average the lines to single line for each side
avg_lines = average_lines_parameter(frame, lines)
# display the lines on the frame
line_img = display_lines(frame, avg_lines)
# combine the lines with the original frame
img = cv2.addWeighted(frame, 0.8, line_img, 1, 1)
# write the processed frame to output file
# out.write(img)
# show the image with lanes and using key'z' to close the video
cv2.imshow('lanes', mat=img)
if cv2.waitKey(1) == ord('z'):
break
ret, frame = video.read()
# destroy all cv2 windows
video.release()
cv2.destroyAllWindows()