Optimize video frame sampling logic

- Replaced manual index calculation with `np.linspace` for improved efficiency and readability.
- Reduced computation overhead by utilizing NumPy's vectorized operations for generating evenly spaced frame indices.
This commit is contained in:
JamePeng
2024-08-27 04:40:48 +08:00
committed by GitHub
parent 3745c3316a
commit 344ddc2cb1

View File

@@ -151,7 +151,7 @@ def encode_video(video_path):
def uniform_sample(frame_indices, num_samples):
# Calculate sampling interval and uniformly sample frame indices
gap = len(frame_indices) / num_samples
sampled_idxs = [int(i * gap + gap / 2) for i in range(num_samples)]
sampled_idxs = np.linspace(gap / 2, len(frame_indices) - gap / 2, num_samples, dtype=int)
return [frame_indices[i] for i in sampled_idxs]
# Read the video and set the decoder's context to CPU