Temporal kernel filtering on video frames coming in a loop I have a video which I read in a loop frame by frame. For every frame, I want to do temporal kernel filtering, the coefficients of which come from the input variable model (a dictionary). The temporal kernel is an array that has size of 15. So, basically, for every frame we apply this function. The variables scale, clip_size are constants. Firstly I add borders across the obtained frame. Then I start a buffer of size (15,240,320) where (240,320) is the clip size. Variable bindex is used to write the buffer. The buffer variable will store only last 15 modified frames, hence bindex needs to be updated at every function iteration. Variable sindex is used to traverse through the buffer and apply the buffer, hence it has to be returned too.  Since the input_frames are coming from a video in a loop, I had to make some variables be both inputs and outputs. Is there a way to speed this up? Any better way to do this?

def filtered_output(input_frame,model, Buffer,bindex,sindex,scale,clip_size):
    # padding is required
    top=model['item'][0]
    bot=model['item1'].shape[1]-top-1
    left=model['item'][1]
    right=model['item1'].shape[0]-left-1
    # NewDepth = 15
    # clip_size is [240,320]
    # In the external loop, bindex = 0, sindex =0 in the beginning
    # We now create a copy of our current frame, with appropriate padding
    frame2= cv2.copyMakeBorder(input_frame,top,bot,left,right,cv2.BORDER_CONSTANT,value=0.5)
    Buffer[bindex] = scipy.signal.convolve2d(frame2, model['item1'], boundary='symm', mode='valid')
    sindex = (bindex+1) % NewDepth # point to oldest image in the buffer
    temp=np.zeros(tuple(clip_size),dtype=float)
    temp = model['TempKern'][0]*Buffer[sindex]

    sindex = (sindex+1) % NewDepth
    for j in range(1, NewDepth) :  # iterate from oldest to newest frame
        temp = temp + model['TempKern'][j]*Buffer[sindex]
        sindex = (sindex+1) % NewDepth
    bindex = (bindex+1) % NewDepth
    temp=np.nan_to_num(temp)
    output_frame = ApplyCubicSpline(model['CubicSplineCoeffs'],temp)*scale
    return output_frame, Buffer,bindex,sindex


My question is whether we can make this function faster somehow (not that it is slow)? If yes, how? Is it the best practice to use bindex, sindex as both inputs and outputs to the function? Are there any alternatives?
Maybe not the answer you're looking for, but I focused on that loop:

for j in range(1, NewDepth) :  # iterate from oldest to newest frame
    temp = temp + model['TempKern'][j]*Buffer[sindex]
    sindex = (sindex+1) % NewDepth


To speed this up you could:


create a variable to avoid accessing model['TempKern'] (dict access)
use in-place adition for temp


improved:

tk = model['TempKern']
for j in range(1, NewDepth) :  # iterate from oldest to newest frame
    temp += tk[j]*Buffer[sindex]
    sindex = (sindex+1) % NewDepth


Aside: outside the loop you do:

temp=np.zeros(tuple(clip_size),dtype=float)
temp = model['TempKern'][0]*Buffer[sindex]


so you can remove the first line, as it has no effect, temp is overwritten at the next line.