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 empty if ret == False: continue img = frame[:, :, ::-1] sampledFrames.append(img) cap.release() # 2. get frame index frames_idx = [xxx] # 3. sample frames = 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 index frames_idx = [xxx] # 2. decode target frame imgs = [] for i in range(videolen): ret = cap.grab() # maybe first frame is empty if ret == False: continue if frames_idx and i == frames_idx[0]: frames_idx.pop(0) ret, frame = cap.retrieve() if frame is None: break imgbuf = frame[:, :, ::-1] img = Image.fromarray(imgbuf, mode='RGB') imgs.append(img) if frames_idx == None: break cap.release()