无法解压不可迭代的

无法解压不可迭代的

本文介绍了无法解压不可迭代的 numpy.float64 对象 python3 opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误,无法理解为什么会出现此问题.下面是代码和错误.

I am getting this error and cant understand why the issue is appearing. Below will be the code and error.

上次可打印锻炼的结果

[-8.54582258e-01  9.83741381e+02] left
[   0.776281243  -160.77584028] right

代码错误发生在make_coordinates 并且行是

The code error happens in make_coordinates and the line is

slope, intercept = line_parameters

完整代码如下:

import cv2
import numpy as np

vid = cv2.VideoCapture('carDriving.mp4')

def processImage(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    canny = cv2.Canny(blur, 50, 150)
    return canny

def region_of_interest(image):
    height = image.shape[0]
    polygons = np.array([
    [(200,height), (1200,height), (750,300)]
    ])
    mask = np.zeros_like(image)
    cv2.fillPoly(mask, polygons, 255)
    masked_image = cv2.bitwise_and(image, mask)
    return masked_image

def display_lines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1, y1, x2, y2 = line.reshape(4)
            cv2.line(line_image, (x1, y1), (x2, y2), (255,0,0), 10)
    return line_image

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

def make_coordinates(image, line_parameters):
    slope, intercept = line_parameters
    y1 = image.shape[0]
    y2 = int(y1*3/5)
    x1 = int(y1 - intercept)/slope
    x1 = int(y2 - intercept)/slope
    return np.array([x1, y1, x2, y2])

while True:
    ret, frame = vid.read()
    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    processed_image = processImage(frame)
    cropped_image = region_of_interest(processed_image)
    lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)
    averaged_lines = average_slope_intercept(grayFrame, lines)
    line_image = display_lines(cropped_image,lines)
    combo_image = cv2.addWeighted(grayFrame, .6, line_image, 1, 1)
    cv2.imshow('result', combo_image)
    print(lines)
    if cv2.waitKey(30) & 0xFF == ord('q'):
        break

vid.release()
cv2.destroyAllWindows()

以及完整的错误信息:

Message=cannot unpack non-iterable numpy.float64 object
Source=C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py
  StackTrace:
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 52, in make_coordinates
    slope, intercept = line_parameters
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 47, in average_slope_intercept
    left_line = make_coordinates(image, left_fit_average)
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 65, in <module>
    averaged_lines = average_slope_intercept(grayFrame, lines)

现在收到另一个错误,第 27 行,第一个错误已修复

Now receiving another error, line 27, first error was fixed

Message=integer argument expected, got float
  Source=C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py
  StackTrace:
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 27, in display_lines
    cv2.line(line_image, (x1, y1), (x2, y2), (255,0,0), 10)
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 76, in <module>
    line_image = display_lines(cropped_image,averaged_lines)

我将第 27 行更改为 cv2.line(line_image, int(x1, y1), int(x2, y2), (255,0,0), 10) 并得到以下错误

I change line 27 to cv2.line(line_image, int(x1, y1), int(x2, y2), (255,0,0), 10) and get the following error

  Message='numpy.float64' object cannot be interpreted as an integer
  Source=C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py
  StackTrace:
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 27, in display_lines
    cv2.line(line_image, int(x1, y1), int(x2, y2), (255,0,0), 10)
  File "C:\Users\Andre\source\repos\SelfDrivingCarTest\SelfDrivingCarTest\SelfDrivingCarTest.py", line 76, in <module>
    line_image = display_lines(cropped_image,averaged_lines)

推荐答案

问题

在您的代码中有一种情况,line_parameters 可以是单个值,np.nan,而不是一对 (slope,intercept)代码> 值.如果拟合的斜率总是 >0,那么 left_fit 最终会成为一个空列表 []:

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

在空列表上运行的 np.average 的输出是 NaN:

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"

因此,在某些情况下left_fit_average = np.average(left_fit) == np.average([]) == np.nan.np.nan 的类型为 numpy.float64.然后您的代码调用:

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)

因此,当对 make_coordinates 的调用到达该行时:

Thus, when the call to make_coordinates gets to the line:

slope, intercept = line_parameters

line_parameters 可能是 np.nan,在这种情况下,您会收到以下错误消息:

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

修复

您可以通过确保将合理的值分配给 slopeintercept 来修复错误,即使 line_parameters=np.nan.您可以通过将赋值行包装在 try... except 子句中来完成此操作:

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.

或者,当 x_fit 值之一没有任何值时,您可以首先阻止 average_slope_intercept 函数调用 make_coordinates其中很有趣:

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)

这篇关于无法解压不可迭代的 numpy.float64 对象 python3 opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:25