Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Finding_Lanes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<img src="https://media3.giphy.com/media/8QC4kjqN9qDEk/giphy.gif?cid=ecf05e471kl80anlf7s2wx2yz9v5ssu5xxgkbsxq74j9afql&rid=giphy.gif&ct=g">

## 🛠️ <b> Description </b>
A short description about the script must be mentioned here.
Finding Lanes is a Python computer vision script built with OpenCV and NumPy to detect road lane markings in images and video streams. It performs RGB to Grayscale conversion, Gaussian blurring, Canny edge detection, Region of Interest (ROI) polygon masking, Hough Transform line detection, and linear regression slope/intercept averaging to compute and visualize clear lane boundaries.

## ⚙️ <b>Languages or Frameworks Used
Run the following command:
Expand Down
48 changes: 36 additions & 12 deletions Finding_Lanes/lanes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import cv2
import numpy as np

Expand All @@ -14,30 +15,46 @@

def make_coordinate(image, line_parameters):
slope, intercept = line_parameters
if slope == 0:
return None
y1 = image.shape[0]
y2 = int(y1*(3/5))
x1 = int((y1-intercept)/slope)
x2 = int((y2-intercept)/slope)
y2 = int(y1 * (3 / 5))
x1 = int((y1 - intercept) / slope)
x2 = int((y2 - intercept) / slope)
return np.array([x1, y1, x2, y2])


def average_lines_intercept(image, lines):
left_fit = []
right_fit = []
if lines is None:
return None
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
if x1 == x2:
continue
parameters = np.polyfit((x1, x2), (y1, y2), 1)
slope = parameters[0]
intercept = parameters[1]
if slope < 0:
left_fit.append((slope, intercept))
else:
right_fit.append((slope, intercept))
left_fit_average = np.average(left_fit, axis=0)
right_fit_average = np.average(right_fit, axis=0)
left_line = make_coordinate(image, left_fit_average)
right_line = make_coordinate(image, right_fit_average)
return np.array([left_line, right_line])

lines_list = []
if len(left_fit) > 0:
left_fit_average = np.average(left_fit, axis=0)
left_line = make_coordinate(image, left_fit_average)
if left_line is not None:
lines_list.append(left_line)

if len(right_fit) > 0:
right_fit_average = np.average(right_fit, axis=0)
right_line = make_coordinate(image, right_fit_average)
if right_line is not None:
lines_list.append(right_line)

return np.array(lines_list) if len(lines_list) > 0 else None


def canny(image):
Expand Down Expand Up @@ -66,13 +83,20 @@ def roi(image):
return masked_image


cap = cv2.VideoCapture("Finding_Lanes/video.mp4")
dir_path = os.path.dirname(os.path.abspath(__file__))
video_path = os.path.join(dir_path, "video.mp4")
if not os.path.exists(video_path):
video_path = "Finding_Lanes/video.mp4"

while(cap.isOpened()):
_, frame = cap.read()
cap = cv2.VideoCapture(video_path)

while cap.isOpened():
ret, frame = cap.read()
if not ret or frame is None:
break
canny_image = canny(frame)
cropped_image = roi(canny_image)
lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180,
lines = cv2.HoughLinesP(cropped_image, 2, np.pi / 180,
100, np.array([]), minLineLength=40, maxLineGap=5)
averaged_lines = average_lines_intercept(frame, lines)
line_image = display_lines(frame, averaged_lines)
Expand Down
11 changes: 9 additions & 2 deletions Finding_Lanes/sub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import os
import matplotlib.pyplot as plt
import matplotlib.image as img
img = img.imread("Finding_Lanes/picture.jpg")
plt.imshow(img)

dir_path = os.path.dirname(os.path.abspath(__file__))
image_path = os.path.join(dir_path, "picture.jpg")
if not os.path.exists(image_path):
image_path = "Finding_Lanes/picture.jpg"

image = img.imread(image_path)
plt.imshow(image)
plt.show()