均匀采样

补最后一帧

def preprocess_video(video_path, num_frames=16, size=(64, 64)):
    """改进的视频预处理,保持一致性"""
    cap = cv2.VideoCapture(video_path)
    frames = []
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        frame = cv2.resize(frame, size)
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        frames.append(frame)
    cap.release()

    max_length = 32
    if len(frames) > max_length:
        frames = frames[-max_length:]
    
    # 改进的帧采样策略
    if len(frames) > num_frames:
        # 从视频中均匀采样,而不是线性空间
        step = len(frames) // num_frames
        indices = list(range(0, len(frames), step))[:num_frames]
        frames = [frames[i] for i in indices]
    elif len(frames) < num_frames:
        # 重复最后一帧而不是插入黑帧
        last_frame = frames[-1] if frames else np.zeros((size[1], size[0], 3), dtype=np.uint8)
        while len(frames) < num_frames:
            frames.append(last_frame.copy())
    
    return frames

Logo

邀请您加入社区

更多推荐