close

[Solved] cannot unpack non-iterable numpy.float64 object python3 opencv

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error cannot unpack non-iterable numpy.float64 object python3 opencv in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How cannot unpack non-iterable numpy.float64 object python3 opencv Error Occurs?

Today I get the following error cannot unpack non-iterable numpy.float64 object python3 opencv in python.

How To Solve cannot unpack non-iterable numpy.float64 object python3 opencv Error ?

  1. How To Solve cannot unpack non-iterable numpy.float64 object python3 opencv Error ?

    To Solve cannot unpack non-iterable numpy.float64 object python3 opencv Error after right_fit.append((slope, intercept)) you should make one less indent till the end of the function.

  2. cannot unpack non-iterable numpy.float64 object python3 opencv

Solution 1

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.nannp.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)

Solution 2

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])

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read