对于读一个视频文件,循环语句中反复调用读数据会内存泄露,如下所示:
def get_next_frame(cap):
if True==cap.isOpened():
ret,frame = cap.read()
if not ret:
return None
return np.asarray(frame)
return None
把循环调用,改为根据帧索引读取:
def get_frame(cap,index):
if True==cap.isOpened():
if hasattr(cv2, 'cv'):
cap.set(cv2.cv.CAP_PROP_POS_FRAMES,index) #设置要获取的帧号
else:
cap.set(cv2.CAP_PROP_POS_FRAMES,index) #设置要获取的帧号
ret,frame = cap.read()
if not ret:
return None
return np.asarray(frame)
return None

本文探讨了在循环中反复调用视频文件读取方法导致的内存泄露问题,并提出了一种解决方案,即将循环读取改为根据帧索引读取,有效避免了内存资源的浪费。
1256

被折叠的 条评论
为什么被折叠?



