-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.py
74 lines (58 loc) · 2.59 KB
/
video.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import numpy as np
import pydicom
import torch
import torchaudio
from operator import itemgetter
class Video:
def __init__(
self,
frames_filepaths,
audio_filepath,
framerate=50,
max_diff=0.0025,
):
audio, self.sample_rate = torchaudio.load(audio_filepath)
audio = torch.mean(audio, dim=0).squeeze(dim=0)
self.num_samples, = audio.shape
audio_duration = self.num_samples / self.sample_rate
self.framerate = framerate
self.num_frames = len(frames_filepaths)
video_duration = self.num_frames / self.framerate
diff = abs(video_duration - audio_duration)
if diff > max_diff:
raise ValueError(f"Difference in duration of audio and video is too large ({diff})")
self.duration = video_duration
self.audio = audio
self.frames_filepaths = frames_filepaths
@staticmethod
def load_frame(filepath):
ds = pydicom.dcmread(filepath)
frame = torch.tensor(ds.pixel_array.astype(np.float))
return frame
def get_audio_interval(self, start, end):
time = np.linspace(0., self.duration, self.num_samples)
ge_start, = np.where(time >= start) # Greater than or equal to the start
lt_end, = np.where(time < end) # Lower than the end
indices = sorted(set(ge_start) & set(lt_end))
audio_interval = self.audio[indices]
return torch.tensor(time[indices], dtype=torch.float), audio_interval
def get_frames_interval(self, start, end, load_frames=False):
time = np.linspace(0., self.duration, self.num_frames)
ge_start, = np.where(time >= start) # Greater than or equal to the start
lt_end, = np.where(time < end) # Lower than the end
indices = list(set(ge_start) & set(lt_end))
if len(indices) == 0:
return torch.tensor([], dtype=torch.float), []
frames_filepaths = itemgetter(*indices)(self.frames_filepaths)
if isinstance(frames_filepaths, str):
frames_filepaths = [frames_filepaths]
frames_filepaths = sorted(frames_filepaths)
if load_frames:
frames = torch.stack([self.load_frame(fp) for fp in frames_filepaths])
else:
frames = frames_filepaths
return torch.tensor(time[indices], dtype=torch.float), frames
def get_interval(self, start, end, load_frames=False):
_, audio_interval = self.get_audio_interval(start, end)
_, frames_interval = self.get_frames_interval(start, end, load_frames)
return audio_interval, frames_interval