# python – cannot unpack non-iterable numpy.float64 object python3 opencv

## python – cannot unpack non-iterable numpy.float64 object python3 opencv

# The problem

Theres 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
```

its 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
```

Youll 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 doesnt 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

#### python – cannot unpack non-iterable numpy.float64 object python3 opencv

I found the solution, in your code there is the wrong indent:

instead of your code:

```
def average_slope_intercept(image, lines):
left_fit = []
right_fit = []
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
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)
print(left_fit_average, left)
print(right_fit_average, right)
left_line = make_coordinates(image, left_fit_average)
right_line = make_coordinates(image, right_fit_average)
#return np.array([left_line, right_line])**
```

after ** right_fit.append((slope, intercept))** you should make one less indent till the end of the function.

So, your code must be:

```
def average_slope_intercept(image, lines):
left_fit = []
right_fit = []
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line.reshape(4)
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)
print(left_fit_average, left)
print(right_fit_average, right)
left_line = make_coordinates(image, left_fit_average)
right_line = make_coordinates(image, right_fit_average)
return np.array([left_line, right_line])
```