首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:无法解包不可迭代的numpy.float64对象

TypeError:无法解包不可迭代的numpy.float64对象
EN

Stack Overflow用户
提问于 2019-12-16 21:46:35
回答 2查看 3.6K关注 0票数 0

下面的代码使用openCV模块来识别道路上的车道。我使用python3.6进行编码(我使用atom IDE进行开发。之所以提供这个信息,是因为stackoverflow不允许我在没有不必要的信息行的情况下发布信息。所以请忽略括号中的注释)代码在给定的示例视频中运行良好。但当我为另一个视频运行它时,它抛出了以下错误:

代码语言:javascript
复制
(base) D:\Self-Driving course\finding-lanes>RayanFindingLanes.py
C:\Users\Tarun\Anaconda3\lib\site-packages\numpy\lib\function_base.py:392: RuntimeWarning: Mean of empty slice.
  avg = a.mean(axis)
C:\Users\Tarun\Anaconda3\lib\site-packages\numpy\core\_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
Traceback (most recent call last):
  File "D:\Self-Driving course\finding-lanes\RayanFindinglanes.py", line 81, in <module>
    averaged_lines = average_slope_intercept(frame, lines)
  File "D:\Self-Driving course\finding-lanes\RayanFindinglanes.py", line 51, in average_slope_intercept
    right_line = make_points(image, right_fit_average)
  File "D:\Self-Driving course\finding-lanes\RayanFindinglanes.py", line 56, in make_points
    slope, intercept = line
TypeError: cannot unpack non-iterable numpy.float64 object

这个错误意味着什么,以及如何解决它?

代码:

代码语言:javascript
复制
import cv2
import numpy as np

def canny(img):
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    kernel = 5
    blur = cv2.GaussianBlur(gray,(kernel, kernel),0)
    canny = cv2.Canny(blur, 50, 150)
    return canny

def region_of_interest(canny):
    height = canny.shape[0]
    width = canny.shape[1]
    mask = np.zeros_like(canny)

    triangle = np.array([[
    (200, height),
    (550, 250),
    (1100, height),]], np.int32)

    cv2.fillPoly(mask, triangle, 255)
    masked_image = cv2.bitwise_and(canny, mask)
    return masked_image

def display_lines(img,lines):
    line_image = np.zeros_like(img)
    if lines is not None:
        for line in lines:
            for x1, y1, x2, y2 in line:
                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 None:
        return None
    for line in lines:
        for x1, y1, x2, y2 in line:
            fit = np.polyfit((x1,x2), (y1,y2), 1)
            slope = fit[0]
            intercept = fit[1]
            if slope < 0: # y is reversed in image
                left_fit.append((slope, intercept))
            else:
                right_fit.append((slope, intercept))
    # add more weight to longer lines
    left_fit_average  = np.average(left_fit, axis=0)
    right_fit_average = np.average(right_fit, axis=0)
    left_line  = make_points(image, left_fit_average)
    right_line = make_points(image, right_fit_average)
    averaged_lines = [left_line, right_line]
    return averaged_lines

def make_points(image, line):
    slope, intercept = line
    y1 = int(image.shape[0])# bottom of the image
    y2 = int(y1*3/5)         # slightly lower than the middle
    x1 = int((y1 - intercept)/slope)
    x2 = int((y2 - intercept)/slope)
    return [[x1, y1, x2, y2]]

cap = cv2.VideoCapture("test3.mp4")
while(cap.isOpened()):
    _, frame = cap.read()
    canny_image = canny(frame)
    cropped_canny = region_of_interest(canny_image)
    lines = cv2.HoughLinesP(cropped_canny, 2, np.pi/180, 100, np.array([]), minLineLength=40,maxLineGap=5)
    averaged_lines = average_slope_intercept(frame, lines)
    line_image = display_lines(frame, averaged_lines)
    combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
    cv2.imshow("result", combo_image)
    if cv2.waitKey(1) == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
EN

回答 2

Stack Overflow用户

发布于 2020-05-25 00:37:48

在一些帧中,所有的斜率都大于0,因此left_fit列表为空。正因为如此,当你计算left_fit平均值时,你会得到错误。解决此问题的方法之一是使用前一帧的left_fit average。我已经用同样的方法解决了这个问题。请查看下面的代码,并让我知道它是否解决了您的问题。

代码语言:javascript
复制
global_left_fit_average = []
global_right_fit_average = []
def average_slope_intercept(image, lines):
    left_fit = []
    right_fit = []
    global global_left_fit_average
    global global_right_fit_average

    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))
    if (len(left_fit) == 0):
        left_fit_average = global_left_fit_average
    else:
        left_fit_average = np.average(left_fit, axis=0)
        global_left_fit_average = left_fit_average

    right_fit_average = np.average(right_fit, axis=0)
    global_right_fit_average = right_fit_average
    left_line = make_corordinates(image, left_fit_average)
    right_line = make_corordinates(image, right_fit_average)
    return np.array([left_line, right_line])
票数 1
EN

Stack Overflow用户

发布于 2019-12-16 21:59:41

HoughLinesP返回一个列表,它可以是一个空列表,但不一定是None

因此,函数average_slope_intercept中的代码行

代码语言:javascript
复制
if lines is None:
        return None

用处不大。

您需要检查len(lines) == 0

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59357940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档