Reputation: 335
Guys I am trying to follow this quick tutorial. Also I am trying to learn every code that we type. But I got an error. I am trying to learn every code so can you please tell me why I am getting this error? And how to solve this error?
I get the error in function of def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
The Error I get:
File "C:\Users\alpgu\Desktop\opencv\yolboyama.py", line 112, in poly_left = np.poly1d(np.polyfit(
File "<array_function internals>", line 5, in polyfit
File "C:\Users\alpgu\anaconda3\lib\site-packages\numpy\lib\polynomial.py", line 599, in polyfit raise TypeError("expected non-empty vector for x")
TypeError: expected non-empty vector for x
I think problem is about the line of::
poly_left = np.poly1d(np.polyfit(
left_line_y,
left_line_x,
deg=1
))
Why it is happening?
Note==> When I change my max x and min y codes(you said it should be int so I tried to change my code. I also tried to do int but it gives another error. So I couldnt understand what should I do know?)::
min_y = int(yol_resmi.shape * (3 / 5)) # <-- Just below the horizon
max_y = int(yol_resmi.shape)
It gives the error of
TypeError: can't multiply sequence by non-int of type 'float'
Is The problem about my max_x - min_y values or is the problem about my polly_left value?
All of my code:
from os.path import sep
import cv2
import numpy as np
import math
cap = cv2.VideoCapture('photos' + sep + 'roads.mp4')
while(cap.isOpened()):
ret, yol_resmi = cap.read()
# Define a blank matrix that matches the image height/width.
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
channel_count = yol_resmi.shape[2]
match_mask_color = (255,) * channel_count
cv2.fillPoly(mask, vertices, match_mask_color)
masked_image = cv2.bitwise_and(img, mask)
return masked_image
height, width, channel = yol_resmi.shape
region_of_interest_vertices = [
(0, height),
(width / 2, height / 2),
(width, height),
]
def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
line_img = np.zeros(
(
img.shape[0],
img.shape[1],
3
),
dtype=np.uint8
)
img = np.copy(img)
if lines is None:
return
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(line_img, (x1, y1), (x2, y2), color, thickness)
img = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0)
return img
#COLOR_BGR2GRAY ekle
gray_image = cv2.cvtColor(yol_resmi, cv2.COLOR_RGB2GRAY)
cannyed_image = cv2.Canny(gray_image, 200, 300)
cropped_image = region_of_interest(
cannyed_image,
np.array(
[region_of_interest_vertices],
np.int32
),
)
lines = cv2.HoughLinesP(
cropped_image,
rho=6,
theta=np.pi / 60,
threshold=160,
lines=np.array([]),
minLineLength=40,
maxLineGap=25
)
print(lines)
line_image = draw_lines(yol_resmi, lines)
left_line_x = []
left_line_y = []
right_line_x = []
right_line_y = []
for line in lines:
for x1, y1, x2, y2 in line:
slope = (y2 - y1) / (x2 - x1) # <-- Calculating the slope.
if math.fabs(slope) < 0.5: # <-- Only consider extreme slope
continue
if slope <= 0: # <-- If the slope is negative, left group.
left_line_x.extend([x1, x2])
left_line_y.extend([y1, y2])
else: # <-- Otherwise, right group.
right_line_x.extend([x1, x2])
right_line_y.extend([y1, y2])
min_y = yol_resmi.shape[0] * (3 / 5) # <-- Just below the horizon
max_y = yol_resmi.shape[0] # <-- The bottom of the image
poly_left = np.poly1d(np.polyfit(
left_line_y,
left_line_x,
deg=1
))
left_x_start = int(poly_left(max_y))
left_x_end = int(poly_left(min_y))
poly_right = np.poly1d(np.polyfit(
right_line_y,
right_line_x,
deg=1
))
right_x_start = int(poly_right(max_y))
right_x_end = int(poly_right(min_y))
line_image = draw_lines(
yol_resmi,
[[
[left_x_start, max_y, left_x_end, min_y],
[right_x_start, max_y, right_x_end, min_y],
]],
thickness=5,
)
# Convert to grayscale here.
#gray_image = cv2.cvtColor(cropped_image, cv2.COLOR_RGB2GRAY)
# Call Canny Edge Detection here.
#cannyed_image = cv2.Canny(gray_image, 100, 200)
#cv2.imshow('yol resmi', yol_resmi)
cv2.imshow('edge detection', cropped_image)
cv2.imshow('line detection', line_image)
#cv2.imshow('Edge Detection', cannyed_image)
cv2.waitKey(1)
Upvotes: 0
Views: 794
Reputation: 708
this is all that I could do:
from os.path import sep
import cv2
import numpy as np
import math
import warnings
cap = cv2.VideoCapture('photos' + sep + 'roads.mp4')
while cap.isOpened():
ret, yol_resmi = cap.read()
# Define a blank matrix that matches the image height/width.
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
channel_count = yol_resmi.shape[2]
match_mask_color = (255,) * channel_count
cv2.fillPoly(mask, vertices, match_mask_color)
masked_image = cv2.bitwise_and(img, mask)
return masked_image
height, width, channel = yol_resmi.shape
region_of_interest_vertices = [
(0, height),
(width / 2, height / 2),
(width, height),
]
def draw_lines(img, lines, color=[255, 0, 0], thickness=3):
line_img = np.zeros(
(
img.shape[0],
img.shape[1],
3
),
dtype=np.uint8
)
img = np.copy(img)
if lines is None:
return
for line in lines:
for x1, y1, x2, y2 in line:
print(type(x1), type(x2), type(y1), type(y2))
cv2.line(line_img, (x1, y1), (x2, int(y2)), color, thickness)
img = cv2.addWeighted(img, 0.8, line_img, 1.0, 0.0)
return img
# COLOR_BGR2GRAY ekle
gray_image = cv2.cvtColor(yol_resmi, cv2.COLOR_RGB2GRAY)
cannyed_image = cv2.Canny(gray_image, 200, 300)
cropped_image = region_of_interest(
cannyed_image,
np.array(
[region_of_interest_vertices],
np.int32
),
)
lines = cv2.HoughLinesP(
cropped_image,
rho=6,
theta=np.pi / 60,
threshold=160,
lines=np.array([]),
minLineLength=40,
maxLineGap=25
)
print(lines)
print(len(lines))
line_image = draw_lines(yol_resmi, lines)
left_line_x = [0, 0]
left_line_y = [0, 0]
right_line_x = [0, 0]
right_line_y = [0, 0]
for line in lines:
for x1, y1, x2, y2 in line:
slope = (y2 - y1) / (x2 - x1) # <-- Calculating the slope.
if math.fabs(slope) < 0.5: # <-- Only consider extreme slope
continue
if slope <= 0: # <-- If the slope is negative, left group.
left_line_x = [x1, x2]
left_line_y = [y1, y2]
else: # <-- Otherwise, right group.
right_line_x = [x1, x2]
right_line_y = [y1, y2]
min_y = yol_resmi.shape[0] * (3 / 5) # <-- Just below the horizon
max_y = yol_resmi.shape[0] # <-- The bottom of the image
try:
poly_left = np.poly1d(np.polyfit(
left_line_y,
left_line_x,
deg=1
))
left_x_start = int(poly_left(max_y))
left_x_end = int(poly_left(min_y))
poly_right = np.poly1d(np.polyfit(
right_line_y,
right_line_x,
deg=1
))
right_x_start = int(poly_right(max_y))
right_x_end = int(poly_right(min_y))
except np.linalg.LinAlgError as e:
warnings.warn("There is no lines in chosen region")
cv2.imshow('line detection', yol_resmi)
cv2.waitKey(1)
continue
line_image = draw_lines(
yol_resmi,
[[
[left_x_start, max_y, left_x_end, min_y],
[right_x_start, max_y, right_x_end, min_y],
]],
thickness=5,
)
# Convert to grayscale here.
# gray_image = cv2.cvtColor(cropped_image, cv2.COLOR_RGB2GRAY)
# Call Canny Edge Detection here.
# cannyed_image = cv2.Canny(gray_image, 100, 200)
# cv2.imshow('yol resmi', yol_resmi)
cv2.imshow('edge detection', cropped_image)
cv2.imshow('line detection', line_image)
# cv2.imshow('Edge Detection', cannyed_image)
cv2.waitKey(1)
Upvotes: 1