Skip to content

Commit a8283e5

Browse files
authored
Merge pull request #76 from zmdlw/main
Finding Load Lanes
2 parents bf542ba + a344805 commit a8283e5

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

Finding_Lanes/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!--Please do not remove this part-->
2+
![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99)
3+
![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)
4+
5+
# <b>🚘 Finding Lanes </b>
6+
### Let's go!
7+
<img src="https://media3.giphy.com/media/8QC4kjqN9qDEk/giphy.gif?cid=ecf05e471kl80anlf7s2wx2yz9v5ssu5xxgkbsxq74j9afql&rid=giphy.gif&ct=g">
8+
9+
## 🛠️ <b> Description </b>
10+
A short description about the script must be mentioned here.
11+
12+
## ⚙️ <b>Languages or Frameworks Used
13+
Run the following command:
14+
```sh
15+
$ python -m pip install --upgrade pip
16+
$ python -m pip install opencv-python
17+
```
18+
## <img src="https://cdn-icons-png.flaticon.com/512/109/109197.png" width="30" height="30"> <b>How to run</b>
19+
open a terminal in the folder where your script is located and run the following command:
20+
```sh
21+
$ python lanes.py
22+
```
23+
## <img src="https://cdn-icons-png.flaticon.com/512/61/61180.png" width="30" height="30"><b> How to close</b>
24+
25+
Just press q
26+
27+
## 📺 <b> Demo </b>
28+
29+
<img src="Finding_Lanes/capture.png" width=100% height=100%>
30+
31+
## 🤖 <b> Author</b>
32+
zmdlw (https://github.com/zmdlw)

Finding_Lanes/capture.png

632 KB
Loading

Finding_Lanes/lanes.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
# 1. convert the image to gray scale
6+
# 2. blur the image
7+
# 3. detect the edges
8+
# 4. create a mask
9+
# 5. apply the mask to the image
10+
# 6. detect the lines
11+
# 7. average the lines
12+
# 8. display the lines
13+
14+
15+
def make_coordinate(image, line_parameters):
16+
slope, intercept = line_parameters
17+
y1 = image.shape[0]
18+
y2 = int(y1*(3/5))
19+
x1 = int((y1-intercept)/slope)
20+
x2 = int((y2-intercept)/slope)
21+
return np.array([x1, y1, x2, y2])
22+
23+
24+
def average_lines_intercept(image, lines):
25+
left_fit = []
26+
right_fit = []
27+
for line in lines:
28+
x1, y1, x2, y2 = line.reshape(4)
29+
parameters = np.polyfit((x1, x2), (y1, y2), 1)
30+
slope = parameters[0]
31+
intercept = parameters[1]
32+
if slope < 0:
33+
left_fit.append((slope, intercept))
34+
else:
35+
right_fit.append((slope, intercept))
36+
left_fit_average = np.average(left_fit, axis=0)
37+
right_fit_average = np.average(right_fit, axis=0)
38+
left_line = make_coordinate(image, left_fit_average)
39+
right_line = make_coordinate(image, right_fit_average)
40+
return np.array([left_line, right_line])
41+
42+
43+
def canny(image):
44+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
45+
blur = cv2.GaussianBlur(gray, (5, 5), 0)
46+
canny = cv2.Canny(blur, 50, 150)
47+
return canny
48+
49+
50+
def display_lines(image, lines):
51+
line_image = np.zeros_like(image)
52+
if lines is not None:
53+
for x1, y1, x2, y2 in lines:
54+
cv2.line(line_image, (x1, y1), (x2, y2), (0, 0, 255), 10)
55+
return line_image
56+
57+
58+
def roi(image):
59+
height = image.shape[0]
60+
polygons = np.array([
61+
[(200, height), (1100, height), (550, 250)]
62+
])
63+
mask = np.zeros_like(image)
64+
cv2.fillPoly(mask, polygons, 255)
65+
masked_image = cv2.bitwise_and(image, mask)
66+
return masked_image
67+
68+
69+
cap = cv2.VideoCapture("Finding_Lanes/video.mp4")
70+
71+
while(cap.isOpened()):
72+
_, frame = cap.read()
73+
canny_image = canny(frame)
74+
cropped_image = roi(canny_image)
75+
lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180,
76+
100, np.array([]), minLineLength=40, maxLineGap=5)
77+
averaged_lines = average_lines_intercept(frame, lines)
78+
line_image = display_lines(frame, averaged_lines)
79+
combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
80+
cv2.imshow("result", combo_image)
81+
if cv2.waitKey(10) == ord('q'):
82+
break
83+
84+
cap.release()
85+
cv2.destroyAllWindows()

Finding_Lanes/picture.jpg

758 KB
Loading

Finding_Lanes/sub.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import matplotlib.pyplot as plt
2+
import matplotlib.image as img
3+
img = img.imread("Finding_Lanes/picture.jpg")
4+
plt.imshow(img)
5+
plt.show()

Finding_Lanes/video.mp4

1.63 MB
Binary file not shown.

0 commit comments

Comments
 (0)