Python 获取类型错误:切片索引必须是整数

Python 获取类型错误:切片索引必须是整数,python,typeerror,Python,Typeerror,我得到了这个错误 Traceback (most recent call last): File "C:\Users\rohit\Desktop\Python\driving-lane-departure-warning-master\main.py", line 19, in <module> img_aug = process_frame(img) File "C:\Users\rohit\Desktop\Python\driving-lane-departure

我得到了这个错误

Traceback (most recent call last):
  File "C:\Users\rohit\Desktop\Python\driving-lane-departure-warning-master\main.py", line 19, in <module>
    img_aug = process_frame(img)
  File "C:\Users\rohit\Desktop\Python\driving-lane-departure-warning-master\lane.py", line 612, in process_frame
    output = create_output_frame(offcenter, pts, img_undist_, fps, curvature, curve_direction, binary_sub)
  File "C:\Users\rohit\Desktop\Python\driving-lane-departure-warning-master\lane.py", line 484, in create_output_frame
    whole_frame[220+h/2:220+2*h/2, 20:20+w/2, :] = undist_birdview
TypeError: slice indices must be integers or None or have an __index__ method
回溯(最近一次呼叫最后一次):
文件“C:\Users\rohit\Desktop\Python\driving lane offer warning master\main.py”,第19行,在
img\U aug=过程帧(img)
文件“C:\Users\rohit\Desktop\Python\driving lane offer warning master\lane.py”,第612行,进程中
输出=创建输出帧(偏心、pts、img未失真、fps、曲率、曲线方向、二进制子帧)
文件“C:\Users\rohit\Desktop\Python\driving lane offer warning master\lane.py”,第484行,在创建输出框架中
全帧[220+h/2:220+2*h/2,20:20+w/2,:]=Undis_birdview
TypeError:切片索引必须是整数或无,或具有_索引_方法
有人能告诉我如何删除这个错误吗

下面是一段代码:

def create_output_frame(offcenter, pts, undist_ori, fps, curvature, curve_direction, binary_sub, threshold=0.6):
    """
    :param offcenter:
    :param pts:
    :param undist_ori:
    :param fps:
    :param threshold:
    :return:
    """

    undist_ori = cv2.resize(undist_ori, (0,0), fx=1/output_frame_scale, fy=1/output_frame_scale)
    w = undist_ori.shape[1]
    h = undist_ori.shape[0]

    undist_birdview = warper(cv2.resize(undist_ori, (0,0), fx=1/2, fy=1/2), M_b)

    color_warp = np.zeros_like(undist_ori).astype(np.uint8)

    # create a frame to hold every image
    whole_frame = np.zeros((h*3,w*3, 3), dtype=np.uint8)


    if abs(offcenter) > threshold:  # car is offcenter more than 0.6 m
        # Draw Red lane
        cv2.fillPoly(color_warp, np.int_([pts]), (255, 0, 0)) # red
    else: # Draw Green lane
        cv2.fillPoly(color_warp, np.int_([pts]), (0,255, 0))  # green

    newwarp = cv2.warpPerspective(color_warp, M_inv, (int(frame_width/input_scale), int(frame_height/input_scale)))

    # Combine the result with the original image    # result = cv2.addWeighted(undist, 1, newwarp, 0.3, 0)

    newwarp_ = cv2.resize(newwarp,None, fx=input_scale/output_frame_scale, fy=input_scale/output_frame_scale, interpolation = cv2.INTER_LINEAR)

    output = cv2.addWeighted(undist_ori, 1, newwarp_, 0.3, 0)

    ############## generate the combined output frame only for visualization purpose ################
    whole_frame[40:40+h, 20:20+w, :] = undist_ori
    whole_frame[40:40+h, 60+w:60+2*w, :] = output
    whole_frame[220+h/2:220+2*h/2, 20:20+w/2, :] = undist_birdview
    whole_frame[220+h/2:220+2*h/2, 40+w/2:40+w, 0] = cv2.resize((binary_sub*255).astype(np.uint8), (0,0), fx=1/2, fy=1/2)
    whole_frame[220+h/2:220+2*h/2, 40+w/2:40+w, 1] = cv2.resize((binary_sub*255).astype(np.uint8), (0,0), fx=1/2, fy=1/2)
    whole_frame[220+h/2:220+2*h/2, 40+w/2:40+w, 2] = cv2.resize((binary_sub*255).astype(np.uint8), (0,0), fx=1/2, fy=1/2)

    font = cv2.FONT_HERSHEY_SIMPLEX
    if offcenter >= 0:
        offset = offcenter
        direction = 'Right'
    elif offcenter < 0:
        offset = -offcenter
        direction = 'Left'

    info_road = "Road Status"
    info_lane = "Lane info: {0}".format(curve_direction)
    info_cur = "Curvature {:6.1f} m".format(curvature)
    info_offset = "Off center: {0} {1:3.1f}m".format(direction, offset)
    info_framerate = "{0:4.1f} fps".format(fps)
    info_warning = "Warning: offcenter > 0.6m (use higher threshold in real life)"

    cv2.putText(whole_frame, "Departure Warning System with a Monocular Camera", (23,25), font, 0.8, (255,255,0), 1, cv2.LINE_AA)
    cv2.putText(whole_frame, "Origin", (22,70), font, 0.6, (255,255,0), 1, cv2.LINE_AA)
    cv2.putText(whole_frame, "Augmented", (40+w+25,70), font, 0.6, (255,255,0), 1, cv2.LINE_AA)
    cv2.putText(whole_frame, "Bird's View", (22+30,70+35+h), font, 0.6, (255,255,0), 1, cv2.LINE_AA)
    cv2.putText(whole_frame, "Lanes", (22+225,70+35+h), font, 0.6, (255,255,0), 1, cv2.LINE_AA)
    cv2.putText(whole_frame, info_road, (40+w+50,70+35+h), font, 0.8, (255,255,0), 1,cv2.LINE_AA)
    cv2.putText(whole_frame, info_warning, (35+w,60+h), font, 0.4, (255,255,0), 1,cv2.LINE_AA)
    cv2.putText(whole_frame, info_lane, (40+w+50,70+35+40+h), font, 0.8, (255,255,0), 1,cv2.LINE_AA)
    cv2.putText(whole_frame, info_cur, (40+w+50,70+35+80+h), font, 0.8, (255,255,0), 1,cv2.LINE_AA)
    cv2.putText(whole_frame, info_offset, (40+w+50,70+35+120+h), font, 0.8, (255,255,0), 1,cv2.LINE_AA)
    cv2.putText(whole_frame, info_framerate, (40+w+250,70), font, 0.6, (255,255,0), 1,cv2.LINE_AA)

    return whole_frame
def create_output_frame(偏心、pts、未失真、fps、曲率、曲线方向、二进制、阈值=0.6):
"""
:参数偏心:
:param pts:
:param undi_ori:
:参数fps:
:参数阈值:
:返回:
"""
Undis_ori=cv2。调整大小(Undis_ori,(0,0),fx=1/输出_帧_比例,fy=1/输出_帧_比例)
w=原始形状[1]
h=原始形状[0]
Undis_birdview=整经机(cv2.调整大小(Undis_ori,(0,0),fx=1/2,fy=1/2),M_b)
颜色扭曲=np.zeros类(未扭曲)。astype(np.uint8)
#创建一个框架来保存每个图像
整帧=np.0((h*3,w*3,3),dtype=np.uint8)
如果abs(偏心)>临界值:#车辆偏心超过0.6 m
#划红车道
cv2.fillPoly(颜色扭曲,np.int([pts]),(255,0,0))#红色
否则:#画绿道
cv2.fillPoly(颜色扭曲,np.int([pts]),(0255,0))#绿色
newwarp=cv2.warpPerspective(颜色扭曲、M翻转、(int(帧宽度/输入比例)、int(帧高度/输入比例)))
#将结果与原始图像相结合#结果=cv2.addWeighted(Undis,1,newwarp,0.3,0)
newwarp\uU=cv2.调整大小(newwarp,无,fx=input\u scale/output\u frame\u scale,fy=input\u scale/output\u frame\u scale,插值=cv2.内部线性)
输出=cv2.addWeighted(未经编辑,1,新扭曲,0.3,0)
##############仅为可视化目的生成组合输出帧################
整帧[40:40+h,20:20+w,:]=Undis\u ori
整帧[40:40+h,60+w:60+2*w,:]=输出
全帧[220+h/2:220+2*h/2,20:20+w/2,:]=Undis_birdview
整帧[220+h/2:220+2*h/2,40+w/2:40+w,0]=cv2.调整大小((二进制子*255).aType(np.uint8),(0,0),fx=1/2,fy=1/2)
整帧[220+h/2:220+2*h/2,40+w/2:40+w,1]=cv2.resize((二进制)255.astype(np.uint8),(0,0),fx=1/2,fy=1/2)
整帧[220+h/2:220+2*h/2,40+w/2:40+w,2]=cv2.调整大小((二进制子*255).aType(np.uint8),(0,0),fx=1/2,fy=1/2)
font=cv2.font\u HERSHEY\u SIMPLEX
如果偏心>=0:
偏移=偏心
方向=‘右’
elif偏心<0:
偏移量=-偏心
方向=‘左’
info_road=“道路状态”
info_lane=“lane info:{0}”。格式(曲线方向)
info_cur=“曲率{:6.1f}m”。格式(曲率)
info_offset=“偏心:{0}{1:3.1f}m”。格式(方向,偏移)
info_framerate=“{0:4.1f}fps”。格式(fps)
info_warning=“警告:偏心>0.6m(在现实生活中使用更高的阈值)”
cv2.putText(全帧,“带单目摄像头的离境警告系统”,(23,25),字体,0.8,(255255,0),1,cv2.LINE\u AA)
cv2.putText(整个框架,“原点”(22,70),字体,0.6,(255255,0),1,cv2.LINE\uAA)
cv2.putText(整个框架,“增强的”(40+w+25,70),字体,0.6,(255255,0),1,cv2.LINE\uAA)
cv2.putText(整个框架,“鸟瞰图”(22+30,70+35+h),字体,0.6,(255255,0),1,cv2.LINE_AA)
cv2.putText(全框架,“车道”(22+225,70+35+h),字体,0.6,(255255,0),1,cv2.LINE\uAA)
cv2.putText(全帧,信息路,(40+w+50,70+35+h),字体,0.8,(255255,0),1,cv2.LINE\u AA)
cv2.putText(整个框架,信息警告,(35+w,60+h),字体,0.4,(255255,0),1,cv2.LINE\uAA)
cv2.putText(全帧,信息通道,(40+w+50,70+35+40+h),字体,0.8,(255255,0),1,cv2.LINE\u AA)
cv2.putText(整帧,信息流,(40+w+50,70+35+80+h),字体,0.8,(255255,0),1,cv2.LINE\uAA)
cv2.putText(整帧,信息偏移,(40+w+50,70+35+120+h),字体,0.8,(255255,0),1,cv2.LINE\uAA)
cv2.putText(全帧,信息帧率,(40+w+250,70),字体,0.6,(255255,0),1,cv2.LINE\uAA)
返回整帧

您使用的是哪个版本的Python? 问题有可能被2除。
尝试将“h/2”和“w/2”分别替换为“h//2”和“w//2”。

如果切片索引为浮点或双精度,或者切片语法不正确,则可能发生这种情况。请检查切片语法或切片索引的类型

可能的错误情况:

a = [1,2,3,4]
a = a[1.2:]
TypeError: slice indices must be integers or None or have an __index__ method

a = a[1,2:]
TypeError: slice indices must be integers or None or have an __index__ method

a = a[1:2,3:4,:]
TypeError: slice indices must be integers or None or have an __index__ method

你能给我一个电话吗?很抱歉浪费了你的时间。我解决了这个问题。这是由于部门的问题。