File size: 928 Bytes
2c966e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import os
import random
import numpy as np
def dense_sampling_from_extracted_frames(folder_path, num_clips=6, frames_per_clip=5):
frame_files = sorted([os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.npy')])
num_frames = len(frame_files)
print(f"Found {num_frames} frames in {folder_path}")
if num_frames < num_clips * frames_per_clip:
raise ValueError("Not enough frames to sample the required clips.")
frames_per_segment = num_frames // num_clips
clips = []
for i in range(num_clips):
segment_start = i * frames_per_segment
segment_end = segment_start + frames_per_segment - 1
max_start_frame = segment_end - frames_per_clip + 1
start_frame = random.randint(segment_start, max_start_frame)
clip = [np.load(frame_files[start_frame + j]) for j in range(frames_per_clip)]
clips.append(clip)
return clips
|