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'] bot=model['item1'].shape-top-1 left=model['item'] right=model['item1'].shape-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']*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
sindex as both inputs and outputs to the function? Are there any alternatives?