Users Online

· Guests Online: 50

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Python Project - Road Lane line detection - Computer Vision Project in Python

Python Project - Road Lane line detection - Computer Vision Project in Python

BY  · UPDATED · AUGUST 7, 2020

 

 

Lane Line detection is a critical component for self driving cars and also for computer vision in general. This concept is used to describe the path for self-driving cars and to avoid the risk of getting in another lane.

In this article, we will build a machine learning project to detect lane lines in real-time. We will do this using the concepts of computer vision using OpenCV library. To detect the lane we have to detect the white markings on both sides on the lane.

 

 

 

 

lane line detection ml project

lane line detection ml project

Road Lane-Line Detection with Python & OpenCV

Using computer vision techniques in Python, we will identify road lane lines in which autonomous cars must run. This will be a critical part of autonomous cars, as the self-driving cars should not cross it’s lane and should not go in opposite lane to avoid accidents.

 

Frame Masking and Hough Line Transformation

To detect white markings in the lane, first, we need to mask the rest part of the frame. We do this using frame masking. The frame is nothing but a NumPy array of image pixel values. To mask the unnecessary pixel of the frame, we simply update those pixel values to 0 in the NumPy array.

After making we need to detect lane lines. The technique used to detect mathematical shapes like this is called Hough Transform. Hough transformation can detect shapes like rectangles, circles, triangles, and lines.

 

 

 

 

Code Download

Please download the source code: Lane Line Detection Project Code

 

 

 

Follow the below steps for lane line detection in Python:

 

 

1. Imports:

  1. import matplotlib.pyplot as plt
  2.  
  3. import numpy as np
  4. import cv2
  5. import os
  6. import matplotlib.image as mpimg
  7. from moviepy.editor import VideoFileClip
  8. import math

2. Apply frame masking and find region of interest:

  1. def interested_region(img, vertices):
  2. if len(img.shape) > 2:
  3. mask_color_ignore = (255,) * img.shape[2] 
  4. else:
  5. mask_color_ignore = 255 
  6. cv2.fillPoly(np.zeros_like(img), vertices, mask_color_ignore) 
  7. return cv2.bitwise_and(img, np.zeros_like(img))

3. Conversion of pixels to a line in Hough Transform space:

  1. def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
  2. lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
  3. line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
  4. lines_drawn(line_img,lines)
  5. return line_img

4. Create two lines in each frame after Hough transform:

  1. def lines_drawn(img, lines, color=[255, 0, 0], thickness=6):
  2. global cache
  3. global first_frame
  4. slope_l, slope_r = [],[]
  5. lane_l,lane_r = [],[]
  6. α =0.2
  7. for line in lines:
  8. for x1,y1,x2,y2 in line:
  9. slope = (y2-y1)/(x2-x1)
  10. if slope > 0.4:
  11. slope_r.append(slope)
  12. lane_r.append(line)
  13. elif slope < -0.4:
  14. slope_l.append(slope)
  15. lane_l.append(line)
  16. img.shape[0] = min(y1,y2,img.shape[0])
  17. if((len(lane_l) == 0) or (len(lane_r) == 0)):
  18. print ('no lane detected')
  19. return 1
  20. slope_mean_l = np.mean(slope_l,axis =0)
  21. slope_mean_r = np.mean(slope_r,axis =0)
  22. mean_l = np.mean(np.array(lane_l),axis=0)
  23. mean_r = np.mean(np.array(lane_r),axis=0)
  24. if ((slope_mean_r == 0) or (slope_mean_l == 0 )):
  25. print('dividing by zero')
  26. return 1
  27. x1_l = int((img.shape[0] - mean_l[0][1] - (slope_mean_l * mean_l[0][0]))/slope_mean_l)
  28. x2_l = int((img.shape[0] - mean_l[0][1] - (slope_mean_l * mean_l[0][0]))/slope_mean_l)
  29. x1_r = int((img.shape[0] - mean_r[0][1] - (slope_mean_r * mean_r[0][0]))/slope_mean_r)
  30. x2_r = int((img.shape[0] - mean_r[0][1] - (slope_mean_r * mean_r[0][0]))/slope_mean_r)
  31. if x1_l > x1_r:
  32. x1_l = int((x1_l+x1_r)/2)
  33. x1_r = x1_l
  34. y1_l = int((slope_mean_l * x1_l ) + mean_l[0][1] - (slope_mean_l * mean_l[0][0]))
  35. y1_r = int((slope_mean_r * x1_r ) + mean_r[0][1] - (slope_mean_r * mean_r[0][0]))
  36. y2_l = int((slope_mean_l * x2_l ) + mean_l[0][1] - (slope_mean_l * mean_l[0][0]))
  37. y2_r = int((slope_mean_r * x2_r ) + mean_r[0][1] - (slope_mean_r * mean_r[0][0]))
  38. else:
  39. y1_l = img.shape[0]
  40. y2_l = img.shape[0]
  41. y1_r = img.shape[0]
  42. y2_r = img.shape[0]
  43. present_frame = np.array([x1_l,y1_l,x2_l,y2_l,x1_r,y1_r,x2_r,y2_r],dtype ="float32")
  44. if first_frame == 1:
  45. next_frame = present_frame
  46. first_frame = 0
  47. else :
  48. prev_frame = cache
  49. next_frame = (1)*prev_frame+α*present_frame
  50. cv2.line(img, (int(next_frame[0]), int(next_frame[1])), (int(next_frame[2]),int(next_frame[3])), color, thickness)
  51. cv2.line(img, (int(next_frame[4]), int(next_frame[5])), (int(next_frame[6]),int(next_frame[7])), color, thickness)
  52. cache = next_frame

5. Process each frame of video to detect lane:

  1. def weighted_img(img, initial_img, α=0.8, β=1., λ=0.):
  2. return cv2.addWeighted(initial_img, α, img, β, λ)
  3. def process_image(image):
  4. global first_frame
  5. gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  6. img_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
  7. lower_yellow = np.array([20, 100, 100], dtype = "uint8")
  8. upper_yellow = np.array([30, 255, 255], dtype="uint8")
  9. mask_yellow = cv2.inRange(img_hsv, lower_yellow, upper_yellow)
  10. mask_white = cv2.inRange(gray_image, 200, 255)
  11. mask_yw = cv2.bitwise_or(mask_white, mask_yellow)
  12. mask_yw_image = cv2.bitwise_and(gray_image, mask_yw)
  13. gauss_gray= cv2.GaussianBlur(mask_yw_image, (5, 5), 0)
  14. canny_edges=cv2.Canny(gauss_gray, 50, 150)
  15. imshape = image.shape
  16. lower_left = [imshape[1]/9,imshape[0]]
  17. lower_right = [imshape[1]-imshape[1]/9,imshape[0]]
  18. top_left = [imshape[1]/2-imshape[1]/8,imshape[0]/2+imshape[0]/10]
  19. top_right = [imshape[1]/2+imshape[1]/8,imshape[0]/2+imshape[0]/10]
  20. vertices = [np.array([lower_left,top_left,top_right,lower_right],dtype=np.int32)]
  21. roi_image = interested_region(canny_edges, vertices)
  22. theta = np.pi/180
  23. line_image = hough_lines(roi_image, 4, theta, 30, 100, 180)
  24. result = weighted_img(line_image, image, α=0.8, β=1., λ=0.)
  25. return result

6. Clip the input video to frames and get the resultant output video file:

  1. first_frame = 1
  2. white_output = '__path_to_output_file__'
  3. clip1 = VideoFileClip("__path_to_input_file__")
  4. white_clip = clip1.fl_image(process_image)
  5. white_clip.write_videofile(white_output, audio=False)

Code for Lane Line Detection Project GUI:

  1. import tkinter as tk
  2. from tkinter import *
  3. import cv2
  4. from PIL import Image, ImageTk
  5. import os
  6. import numpy as np
  7. global last_frame1
  8. last_frame1 = np.zeros((480, 640, 3), dtype=np.uint8)
  9. global last_frame2
  10. last_frame2 = np.zeros((480, 640, 3), dtype=np.uint8)
  11. global cap1
  12. global cap2
  13. cap1 = cv2.VideoCapture("path_to_input_test_video")
  14. cap2 = cv2.VideoCapture("path_to_resultant_lane_detected_video")
  15. def show_vid():
  16. if not cap1.isOpened():
  17. print("cant open the camera1")
  18. flag1, frame1 = cap1.read()
  19. frame1 = cv2.resize(frame1,(400,500))
  20. if flag1 is None:
  21. print ("Major error!")
  22. elif flag1:
  23. global last_frame1
  24. last_frame1 = frame1.copy()
  25. pic = cv2.cvtColor(last_frame1, cv2.COLOR_BGR2RGB)
  26. img = Image.fromarray(pic)
  27. imgtk = ImageTk.PhotoImage(image=img)
  28. lmain.imgtk = imgtk
  29. lmain.configure(image=imgtk)
  30. lmain.after(10, show_vid)
  31. def show_vid2():
  32. if not cap2.isOpened():
  33. print("cant open the camera2")
  34. flag2, frame2 = cap2.read()
  35. frame2 = cv2.resize(frame2,(400,500))
  36. if flag2 is None:
  37. print ("Major error2!")
  38. elif flag2:
  39. global last_frame2
  40. last_frame2 = frame2.copy()
  41. pic2 = cv2.cvtColor(last_frame2, cv2.COLOR_BGR2RGB)
  42. img2 = Image.fromarray(pic2)
  43. img2tk = ImageTk.PhotoImage(image=img2)
  44. lmain2.img2tk = img2tk
  45. lmain2.configure(image=img2tk)
  46. lmain2.after(10, show_vid2)
  47. if __name__ == '__main__':
  48. root=tk.Tk()
  49. lmain = tk.Label(master=root)
  50. lmain2 = tk.Label(master=root)
  51. lmain.pack(side = LEFT)
  52. lmain2.pack(side = RIGHT)
  53. root.title("Lane-line detection")
  54. root.geometry("900x700+100+10")
  55. exitbutton = Button(root, text='Quit',fg="red",command= root.destroy).pack(side = BOTTOM,)
  56. show_vid()
  57. show_vid2()
  58. root.mainloop()
  59. cap.release()

lane line detection ml python project

Summary

This is an intermediate Python project in machine learning, which is helpful for the data science aspirants to master machine learning and gain expertise.

In this lane line detection project, we use OpenCV. Before detecting lane lines, we masked remaining objects and then identified the line with Hough transformation.

 

 

 

 

 

 

  1. lusifer

    gray_image = cv2.cvtColor(image, cv2.COLOmean_r[0][1] – (slope_mean_r * mean_r[0][0])BGR2GRAY)
    ^
    SyntaxError: invalid syntax.

    can you help me please..

  2. Jeremy Evert

    CentOS 7.8, Python 3. We had the same error as lusifer

  3. MANAV KALRA

    Could anyone solve the problem?

  4. Aashique Roshan

    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

  5. Ike

    there is an error in step 4
    for line in lines:
    for x1,y1,x2,y2 in line:
    slope = (y2-y1)/(x2-x1)

    for line in lines: returns none hence not iterateable. anyone with a solution

  6. Adi

    Same problem as Ike. Can someone please share the solution?

    Thanks!

  7. bonny-4

    Same error here as Ike.
    for line in lines:
    TypeError: ‘NoneType’ object is not iterable

    Someone found a solution ?
    THX

  8. SURAJ AGARWAL

    CAN U SHARE ME YOUR DOCUMENTATION

  9. Ashok kumar

    How to run it on my PC

  10. Amogh

    did u found the solution

  11. Varshita Rajana

    Did anyone draw uml diagrams for this project??. Please send me..!

  12. Vempati Ravi Teja

    hi do u have documentation

  13. Harish

    I want this project UML diagrams
    Please send me sir..

  14. Anna Harish

    I want this…lane line detection project UML diagrams… please send me sir.

  15. govind

    lane line detection project UML diagrams…
    please send me sir.

 


 

 


Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.80 seconds
10,819,277 unique visits