我在python中使用收藏.deque用它来做一些计算。这是我的原始代码:
clip=moviepy.editor.VideoFileClip('file.mp4')
clip_size= clip.size[::-1]
Depth=30
dc=5
TempKern = # some array of size Depth
RingBuffer=deque(np.zeros(clip_size, dtype=float),maxlen=NewDepth)
modified_clip = clip.fl_image(new_filtered_output)
modified_clip.write_videofile('output.mp4'))
def new_filtered_output(image):
global RingBuffer
inter_frame=somefunction(image)# inter_frame and image shape is same as clip_size
RingBuffer.append(inter_frame)
# Apply kernel
Output = dc + np.sum([np.asarray(RingBuffer)[j]*TempKern[j] for j in range(Depth)],axis=0)
return Output
这是最快的办法吗?我听说有个选择。但我不知道如何让它像上面的代码一样工作?在
可以将环形缓冲区作为numpy数组,方法是将大小加倍并切片:
现在,您不必每帧都将deque转换为numpy数组(以及每个循环迭代….)。在
如注释中所述,您可以更有效地应用内核:
^{pr2}$或者:
我注意到您更改了上面的代码,但您原来的代码是:
如果您首先将数组展平以进行计算,那么可以大大简化事情并使其运行得更快一些。在
^{pr2}$第二个实现返回相同的值,在我的机器上运行速度快4倍
您可以这样进一步简化和参数化:
three(5, (2,3))
注意,第二个和第三个版本返回一个numpy数组。如果需要,可以使用
.tolist()
将其强制转换为列表。在根据您的反馈-编辑以下内容:
相关问题 更多 >
编程相关推荐