cannot unpack non-iterable numpy.float64 object python3 opencv
The problem
There's a case in your code where line_parameters
can be a single value, np.nan
, instead of a pair of (slope, intercept)
values. If the slope of your fits is always > 0
, then left_fit
will end up being an empty list []
:
if slope < 0:
left_fit.append((slope, intercept))
else:
right_fit.append((slope, intercept))
The output of np.average
run on an empty list is NaN:
np.average([])
# output: np.nan
# also raises two warnings: "RuntimeWarning: Mean of empty slice." and
# "RuntimeWarning: invalid value encountered in double_scalars"
Thus, in some cases left_fit_average = np.average(left_fit) == np.average([]) == np.nan
. np.nan
has a type of numpy.float64
. Your code then calls:
left_line = make_coordinates(image, line_parameters=left_fit_average)
Thus, when the call to make_coordinates
gets to the line:
slope, intercept = line_parameters
it's possible for line_parameters
to be np.nan
, in which case you get the error message about:
TypeError: 'numpy.float64' object is not iterable
A fix
You can fix the bug by making sure that sensible values get assigned to slope
and intercept
even if line_parameters=np.nan
. You can accomplished this by wrapping the assignment line in a try... except
clause:
try:
slope, intercept = line_parameters
except TypeError:
slope, intercept = 0,0
You'll have to decide if this behavior is correct for your needs.
Alternatively, you could prevent the average_slope_intercept
function from calling make_coordinates
in the first place when one of the x_fit
values doesn't have anything interesting in it:
if left_fit:
left_fit_average = np.average(left_fit, axis=0)
print(left_fit_average, 'left')
left_line = make_coordinates(image, left_fit_average)
if right_fit:
right_fit_average = np.average(right_fit, axis=0)
print(right_fit_average, 'right')
right_line = make_coordinates(image, right_fit_average)
As per @tel answer, I like to add some,
try:
slope, intercept = line_parameters
except TypeError:
slope, intercept = 0.001, 0 // It will minimize the error detecting the lane (putting 0, give you a math error)
Again, you can increase the value of maxLineGap to catch the lane when there is so much distance between lanes