Skip to content

Instantly share code, notes, and snippets.

@pknowledge
Created August 31, 2019 15:24
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pknowledge/c2fb00d270b8363c01c0059ff5942d0e to your computer and use it in GitHub Desktop.
Save pknowledge/c2fb00d270b8363c01c0059ff5942d0e to your computer and use it in GitHub Desktop.
OpenCV Python Tutorial For Beginners - Road Lane Line Detection with OpenCV (Part 3)
import matplotlib.pylab as plt
import cv2
import numpy as np
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
#channel_count = img.shape[2]
match_mask_color = 255
cv2.fillPoly(mask, vertices, match_mask_color)
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def drow_the_lines(img, lines):
img = np.copy(img)
blank_image = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(blank_image, (x1,y1), (x2,y2), (0, 255, 0), thickness=10)
img = cv2.addWeighted(img, 0.8, blank_image, 1, 0.0)
return img
# = cv2.imread('road.jpg')
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
def process(image):
print(image.shape)
height = image.shape[0]
width = image.shape[1]
region_of_interest_vertices = [
(0, height),
(width/2, height/2),
(width, height)
]
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
canny_image = cv2.Canny(gray_image, 100, 120)
cropped_image = region_of_interest(canny_image,
np.array([region_of_interest_vertices], np.int32),)
lines = cv2.HoughLinesP(cropped_image,
rho=2,
theta=np.pi/180,
threshold=50,
lines=np.array([]),
minLineLength=40,
maxLineGap=100)
image_with_lines = drow_the_lines(image, lines)
return image_with_lines
cap = cv2.VideoCapture('test.mp4')
while cap.isOpened():
ret, frame = cap.read()
frame = process(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
@akbloodadarsh
Copy link

In line 17 for line in line: TypeError: 'NoneType' object is not iterable

@YueMoon3773
Copy link

In line 17 for line in line: TypeError: 'NoneType' object is not iterable

it means the second line variable is none.
i think u have to fix it into: "for line in lines"

Copy link

ghost commented May 14, 2020

how can I download the test.mp4 ?

@akbloodadarsh
Copy link

@priyanshumittal8 It doesn't have to be a specific clip, you can download any clip which contains the lane, and then name it test.mp4, and give the location of the clip or paste it in the folder which contains the detector.py

@yashanmick
Copy link

In line 17 for line in line: TypeError: 'NoneType' object is not iterable

@akbloodadarsh It means value of "lines" is None. Try exception handling.
if lines is None:
image_with _lines = image
else:
image_with_lines = draw_the _lines(image, lines)
Hope this could work for you.

@yashanmick
Copy link

yashanmick commented May 19, 2020

how can I download the test.mp4 ?

@priyanshumittal8 You can use any clip which contains the lane. Try this https://www.youtube.com/watch?v=6q5_A5wOwDM

Copy link

ghost commented May 20, 2020

Copy link

ghost commented Jul 24, 2020

though it was supposed to get an image, it's getting none
[

image

](url)

@AbhijitKolambe
Copy link

AttributeError Traceback (most recent call last)
in
46 while cap.isOpened():
47 ret, frame = cap.read()
---> 48 frame = process(frame)
49 cv2.imshow('frame', frame)
50 if cv2.waitKey(1) & 0xFF == ord('q'):

in process(image)
20
21 def process(image):
---> 22 print(image.shape)
23 height = image.shape[0]
24 width = image.shape[1]

AttributeError: 'NoneType' object has no attribute 'shape'
how to fix this

@spartan5699
Copy link

AttributeError Traceback (most recent call last)
in
46 while cap.isOpened():
47 ret, frame = cap.read()
---> 48 frame = process(frame)
49 cv2.imshow('frame', frame)
50 if cv2.waitKey(1) & 0xFF == ord('q'):

in process(image)
20
21 def process(image):
---> 22 print(image.shape)
23 height = image.shape[0]
24 width = image.shape[1]

AttributeError: 'NoneType' object has no attribute 'shape'
how to fix this

region_of_interest_vertices = [
(0, height), (width / 2, height / 2), (width, height)
]
This code is used for the setting area of interest. If you pass any appropriate values in it then it shows NoneTye object has no attribute shape. So check your dimensions of the area of interest.

@Shoaibdar
Copy link

AttributeError Traceback (most recent call last)
in
46 while cap.isOpened():
47 ret, frame = cap.read()
---> 48 frame = process(frame)
49 cv2.imshow('frame', frame)
50 if cv2.waitKey(1) & 0xFF == ord('q'):

in process(image)
20
21 def process(image):
---> 22 print(image.shape)
23 height = image.shape[0]
24 width = image.shape[1]

AttributeError: 'NoneType' object has no attribute 'shape'
how to fix this

Check video path

@ArkaDey1996
Copy link

Sir, I am facing some issues regarding different video qualities. The code is running well and detecting the lanes for 1280X720 resolution videos. But for some videos with other than the resolution, the output tab is not showing. The code even is not showing any error. How can I resolve this problem?

@AdityaBhopalbade
Copy link

how can I download the test.mp4 ?

you can download it from here https://www.kaggle.com/datasets/dpamgautam/video-file-for-lane-detection-project?resource=download

@Salma6591
Copy link

Hello! thanks very much for this Tutorial ,I want instead of using the video to work with raspberry camera what should I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment