JackIsNotInTheBox commited on
Commit
798c73c
·
1 Parent(s): ff95229

Fix EinopsError: handle videos with frame count not divisible by 30

Browse files
Files changed (1) hide show
  1. onset_util.py +11 -1
onset_util.py CHANGED
@@ -21,16 +21,26 @@ def extract_onset(video_path, onset_model, tmp_path, device="cuda"):
21
  # Load the video, change fps:
22
  video_path_low_fps = reencode_video_with_diff_fps(video_path, tmp_path, 15, start_second, truncate_second)
23
  frames, _, _ = read_video(video_path_low_fps, pts_unit="sec", output_format="TCHW")
 
24
  if frames.shape[0] >= 150:
25
  frames = frames[:150]
26
  elif frames.shape[0] >= 120:
27
  frames = frames[:120]
 
 
 
 
 
 
 
 
 
28
 
29
  # Transform frames
30
  frames = frames / 255.0
31
  frames = transform(frames)
32
 
33
- frames = rearrange(frames, '(b t) c h w -> b c t h w', t=30).to(device)
34
 
35
  # Forward pass through the model to get onset features
36
  with torch.no_grad():
 
21
  # Load the video, change fps:
22
  video_path_low_fps = reencode_video_with_diff_fps(video_path, tmp_path, 15, start_second, truncate_second)
23
  frames, _, _ = read_video(video_path_low_fps, pts_unit="sec", output_format="TCHW")
24
+ t = 30
25
  if frames.shape[0] >= 150:
26
  frames = frames[:150]
27
  elif frames.shape[0] >= 120:
28
  frames = frames[:120]
29
+ else:
30
+ # Trim to the largest multiple of t that fits
31
+ n = (frames.shape[0] // t) * t
32
+ if n == 0:
33
+ # Pad up to t frames if video is shorter than one clip
34
+ pad = torch.zeros(t - frames.shape[0], *frames.shape[1:], dtype=frames.dtype)
35
+ frames = torch.cat([frames, pad], dim=0)
36
+ n = t
37
+ frames = frames[:n]
38
 
39
  # Transform frames
40
  frames = frames / 255.0
41
  frames = transform(frames)
42
 
43
+ frames = rearrange(frames, '(b t) c h w -> b c t h w', t=t).to(device)
44
 
45
  # Forward pass through the model to get onset features
46
  with torch.no_grad():