首页 > 解决方案 > Python TypeError:“NoneType”对象不可下标-我在进行车道检测时遇到了这个问题

问题描述

Python TypeError:“NoneType”对象不可下标

def draw_back_onto_the_road(img_undistorted, Minv, line_lt, line_rt, keep_state):
    height, width, _ = img_undistorted.shape

    left_fit = line_lt.average_fit if keep_state else line_lt.last_fit_pixel
    right_fit = line_rt.average_fit if keep_state else line_rt.last_fit_pixel
    left_fit.sort()
    right_fit.sort()
    # Generate x and y values for plotting
    ploty = np.linspace(0, height - 1, height)
    left_fitx = left_fit[0] * ploty ** 2 + left_fit[1] * ploty + left_fit[2]
    right_fitx = right_fit[0] * ploty ** 2 + right_fit[1] * ploty + right_fit[2]

错误

left_fitx = left_fit_pixel[0] * ploty ** 2 + left_fit_pixel[1] * ploty + left_fit_pixel[2]
TypeError: 'NoneType' object is not subscriptable

有人可以帮忙吗,拜托

标签: pythontypeerror

解决方案


Probably, left_fit_pixel is None. Think about what you want to do if left_fit_pixel is None and use the following structure:

def draw_back_onto_the_road(img_undistorted, Minv, line_lt, line_rt, keep_state):
    height, width, _ = img_undistorted.shape

    left_fit = line_lt.average_fit if keep_state else line_lt.last_fit_pixel
    right_fit = line_rt.average_fit if keep_state else line_rt.last_fit_pixel
    left_fit.sort()
    right_fit.sort()

    try:
      # Generate x and y values for plotting
      ploty = np.linspace(0, height - 1, height)
      left_fitx = left_fit[0] * ploty ** 2 + left_fit[1] * ploty + left_fit[2]
      right_fitx = right_fit[0] * ploty ** 2 + right_fit[1] * ploty + right_fit[2]
    except TypeError:
      ... # what it should do if left_fit_pixel is None

推荐阅读