python - 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
有人可以帮忙吗,拜托
解决方案
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
推荐阅读
- flutter - 轮播滑块导致 GestureDetector 延迟
- r - 尽管块已成功运行,但当我在 RMD 中按 knit 时出错
- python - 如何线性插值有序数据
- javascript - 如何添加一个下载按钮以在进行更改后下载图像?
- python - Python 递归平方
- python - 如何在以后的某个单元中重新运行特定的 Jupyter 笔记本单元?(不使用函数,泡菜缓存等)
- javascript - 使用烧瓶本地上传的图像不会显示在另一个页面上
- python - 实例的弱引用在删除后不起作用,但在删除时适用于整个类
- three.js - aframe.js 如何改变形状中心位置?(枢轴)
- jqgrid - JqGrid - 更新所有数据单元格中的值