本文主要是介绍Opencv 隔帧取数据解码速度优化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 实用tips,若使用opencv进行解码,代码作如下优化能极大提升解码速度:
cap = cv2.VideoCapture(file_path)videolen = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))# 1. decode all frames, time cost!sampledFrames = []for i in range(videolen):ret, frame = cap.read()# maybe first frame is emptyif ret == False:continueimg = frame[:, :, ::-1]sampledFrames.append(img)cap.release()# 2. get frame indexframes_idx = [xxx]# 3. sampleframes = np.array(sampledFrames)imgs = []for idx in frames_idx:imgbuf = frames[idx]img = Image.fromarray(imgbuf, mode='RGB')imgs.append(img)
优化后:
cap = cv2.VideoCapture(file_path)videolen = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))# 1. get frame indexframes_idx = [xxx]# 2. decode target frameimgs = []for i in range(videolen):ret = cap.grab()# maybe first frame is emptyif ret == False:continue if frames_idx and i == frames_idx[0]:frames_idx.pop(0)ret, frame = cap.retrieve()if frame is None:breakimgbuf = frame[:, :, ::-1]img = Image.fromarray(imgbuf, mode='RGB')imgs.append(img)if frames_idx == None:breakcap.release()
这篇关于Opencv 隔帧取数据解码速度优化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!