You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

车道检测中遇TypeError:无法解包NoneType对象的解决问询

车道辅助项目:TypeError: cannot unpack non-iterable NoneType object 问题排查与解决

运行车道辅助项目代码时触发错误:TypeError: cannot unpack non-iterable NoneType object,经排查确认是ExtendShortLane函数返回了None。

相关代码

检测函数detect_lanes

from .colour_segmentation import segment_lanes
from ...config import config
from .midlane_estimation import estimate_midlane
import cv2
import numpy as np
from .data_extraction import FetchInfoAndDisplay
from .cleaning import GetYellowInnerEdge, ExtendShortLane


# this is my lane_detection file
def detect_lanes(img):
    
    img_cropped = img[config.CropHeight_resized:,:]

    mid_lane_mask, mid_lane_edge,outer_lane_edge, outerlane_side_sep,outerlane_points = segment_lanes(img_cropped,config.minArea_resized)

    estimated_midlane = estimate_midlane(mid_lane_edge,config.MaxDist_resized)

    OuterLane_OneSide, Outer_cnts_oneSide,Mid_cnts, Offset_correction  = GetYellowInnerEdge(outer_lane_edge, estimated_midlane, outerlane_points)

    extended_midlane, extended_outerlane = ExtendShortLane(estimated_midlane,Mid_cnts,Outer_cnts_oneSide, OuterLane_OneSide)
    
    Distance , Curvature = FetchInfoAndDisplay(mid_lane_edge, extended_midlane, extended_outerlane, img_cropped, Offset_correction=Offset_correction)


    
    cv2.imshow("mid_lane_mask", mid_lane_mask)
    cv2.imshow("mid_lane_edge", mid_lane_edge)
    cv2.imshow("outer_lane_edge", outer_lane_edge)
    cv2.imshow("outerlane_side_sep", outerlane_side_sep)
    cv2.imshow("estimated_midlane", estimated_midlane)

    cv2.imshow("OuterLane_OneSide", OuterLane_OneSide)
    cv2.imshow("extended_midlane", extended_midlane)
    cv2.imshow("extended_outerlane", extended_outerlane)

    cv2.waitKey(1)

ExtendShortLane函数实现

def ExtendShortLane(MidLane,Mid_cnts,Outer_cnts,OuterLane):
    if(Mid_cnts and Outer_cnts):
        Mid_cnts_Rowsorted = Cord_Sort(Mid_cnts,"rows")
        Outer_cnts_Rowsorted = Cord_Sort(Outer_cnts,"rows")
        Image_bottom = MidLane.shape[0]
        total_no_of_cnts_midlane = Mid_cnts_Rowsorted.shape[0]
        total_no_of_cnts_outerlane = Outer_cnts_Rowsorted.shape[0]


        BottomPoint_Mid = Mid_cnts_Rowsorted[total_no_of_cnts_midlane-1,:]
        if (BottomPoint_Mid[1] < Image_bottom):
            MidLane = cv2.line(MidLane,tuple(BottomPoint_Mid),(BottomPoint_Mid[0],Image_bottom),255,2)


        BottomPoint_Outer = Outer_cnts_Rowsorted[total_no_of_cnts_outerlane-1,:]
        if (BottomPoint_Outer[1] < Image_bottom):
            if(total_no_of_cnts_outerlane>20):
                shift=20
            else:
                shift=2
            RefLast10Points = Outer_cnts_Rowsorted[total_no_of_cnts_outerlane-shift:total_no_of_cnts_outerlane] 

            if(len(RefLast10Points)>1):
                Ref_x = RefLast10Points[:,0] #cols
                Ref_y = RefLast10Points[:,1] #rows
                Ref_parameters = np.polyfit(Ref_x, Ref_y,1)
                Ref_slope = Ref_parameters[0]
                Ref_yiCntercept = Ref_parameters[1]

                if(Ref_slope < 0):
                    Ref_LineTouchPoint_col = 0
                    Ref_LineTouchPoint_row = Ref_yiCntercept
                else:
                    Ref_LineTouchPoint_col = OuterLane.shape[1]-1
                    Ref_LineTouchPoint_row = Ref_slope * Ref_LineTouchPoint_col + Ref_yiCntercept
                Ref_TouchPoint = (Ref_LineTouchPoint_col,int(Ref_LineTouchPoint_row))#col,row
                Ref_BottomPoint_tup = tuple(BottomPoint_Outer)
                OuterLane =cv2.line(OuterLane,Ref_TouchPoint,Ref_BottomPoint_tup,255)
                 
                if(Ref_LineTouchPoint_row < Image_bottom):
                    Ref_TouchPoint_Ref = (Ref_LineTouchPoint_col, Image_bottom)     
                    OuterLane = cv2.line(OuterLane,Ref_TouchPoint,Ref_TouchPoint_Ref,255,2)      
        return MidLane,OuterLane

问题原因

ExtendShortLane函数的返回语句被包裹在if(Mid_cnts and Outer_cnts)条件分支内:

  • 当Mid_cnts为空、Outer_cnts为空,或者两者都为空时,条件不成立,函数不会执行任何return语句,Python会默认返回None
  • 调用处尝试用extended_midlane, extended_outerlane = ExtendShortLane(...)解包None,触发TypeError

解决方法

确保函数在所有分支下都返回可解包的二元组,最简单的方式是将return MidLane,OuterLane移到条件分支外,或者在条件不满足时返回原始输入的车道数据:

修改后的ExtendShortLane函数:

def ExtendShortLane(MidLane,Mid_cnts,Outer_cnts,OuterLane):
    if(Mid_cnts and Outer_cnts):
        Mid_cnts_Rowsorted = Cord_Sort(Mid_cnts,"rows")
        Outer_cnts_Rowsorted = Cord_Sort(Outer_cnts,"rows")
        Image_bottom = MidLane.shape[0]
        total_no_of_cnts_midlane = Mid_cnts_Rowsorted.shape[0]
        total_no_of_cnts_outerlane = Outer_cnts_Rowsorted.shape[0]


        BottomPoint_Mid = Mid_cnts_Rowsorted[total_no_of_cnts_midlane-1,:]
        if (BottomPoint_Mid[1] < Image_bottom):
            MidLane = cv2.line(MidLane,tuple(BottomPoint_Mid),(BottomPoint_Mid[0],Image_bottom),255,2)


        BottomPoint_Outer = Outer_cnts_Rowsorted[total_no_of_cnts_outerlane-1,:]
        if (BottomPoint_Outer[1] < Image_bottom):
            if(total_no_of_cnts_outerlane>20):
                shift=20
            else:
                shift=2
            RefLast10Points = Outer_cnts_Rowsorted[total_no_of_cnts_outerlane-shift:total_no_of_cnts_outerlane] 

            if(len(RefLast10Points)>1):
                Ref_x = RefLast10Points[:,0] #cols
                Ref_y = RefLast10Points[:,1] #rows
                Ref_parameters = np.polyfit(Ref_x, Ref_y,1)
                Ref_slope = Ref_parameters[0]
                Ref_yiCntercept = Ref_parameters[1]

                if(Ref_slope < 0):
                    Ref_LineTouchPoint_col = 0
                    Ref_LineTouchPoint_row = Ref_yiCntercept
                else:
                    Ref_LineTouchPoint_col = OuterLane.shape[1]-1
                    Ref_LineTouchPoint_row = Ref_slope * Ref_LineTouchPoint_col + Ref_yiCntercept
                Ref_TouchPoint = (Ref_LineTouchPoint_col,int(Ref_LineTouchPoint_row))#col,row
                Ref_BottomPoint_tup = tuple(BottomPoint_Outer)
                OuterLane =cv2.line(OuterLane,Ref_TouchPoint,Ref_BottomPoint_tup,255)
                 
                if(Ref_LineTouchPoint_row < Image_bottom):
                    Ref_TouchPoint_Ref = (Ref_LineTouchPoint_col, Image_bottom)     
                    OuterLane = cv2.line(OuterLane,Ref_TouchPoint,Ref_TouchPoint_Ref,255,2)      
    # 无论条件是否满足,都返回车道数据
    return MidLane,OuterLane

也可以根据业务逻辑,在条件不满足时添加额外的处理(比如返回空车道标记),但核心是保证函数始终返回可解包的二元组。

内容的提问来源于stack exchange,提问作者mina lisa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 23:47:33