-
Notifications
You must be signed in to change notification settings - Fork 198
/
Copy pathtest_video_split_by_scene_mapper.py
170 lines (154 loc) · 5.68 KB
/
test_video_split_by_scene_mapper.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import unittest
from data_juicer.core.data import NestedDataset as Dataset
from data_juicer.ops.mapper.video_split_by_scene_mapper import \
VideoSplitBySceneMapper
from data_juicer.utils.mm_utils import SpecialTokens
from data_juicer.utils.unittest_utils import DataJuicerTestCaseBase
class VideoSplitBySceneMapperTest(DataJuicerTestCaseBase):
data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..',
'data')
vid1_path = os.path.join(data_path, 'video1.mp4') # about 12s
vid2_path = os.path.join(data_path, 'video2.mp4') # about 23s
vid3_path = os.path.join(data_path, 'video3.mp4') # about 50s
vid1_base, vid1_ext = os.path.splitext(os.path.basename(vid1_path))
vid2_base, vid2_ext = os.path.splitext(os.path.basename(vid2_path))
vid3_base, vid3_ext = os.path.splitext(os.path.basename(vid3_path))
op_name = 'video_split_by_scene_mapper'
def get_res_list(self, dataset: Dataset):
res_list = []
for sample in dataset.to_list():
scene_num = len(sample['videos'])
if 'text' in sample:
res_list.append({
'scene_num': scene_num,
'text': sample['text']
})
else:
res_list.append({'scene_num': scene_num})
return res_list
def _run_helper(self, op, source_list, target_list):
dataset = Dataset.from_list(source_list)
dataset = dataset.map(op.process)
res_list = self.get_res_list(dataset)
self.assertEqual(res_list, target_list)
def test_ContentDetector(self):
ds_list = [
{
'videos': [self.vid1_path] # 3 scenes
},
{
'videos': [self.vid2_path] # 1 scene
},
{
'videos': [self.vid3_path] # 2 scenes
}
]
tgt_list = [{'scene_num': 3}, {'scene_num': 1}, {'scene_num': 2}]
op = VideoSplitBySceneMapper(detector='ContentDetector',
threshold=27.0,
min_scene_len=15)
self._run_helper(op, ds_list, tgt_list)
def test_AdaptiveDetector(self):
ds_list = [
{
'videos': [self.vid1_path] # 3 scenes
},
{
'videos': [self.vid2_path] # 1 scene
},
{
'videos': [self.vid3_path] # 8 scenes
}
]
tgt_list = [{'scene_num': 3}, {'scene_num': 1}, {'scene_num': 8}]
op = VideoSplitBySceneMapper(detector='AdaptiveDetector',
threshold=3.0,
min_scene_len=15)
self._run_helper(op, ds_list, tgt_list)
def test_ThresholdDetector(self):
ds_list = [
{
'videos': [self.vid1_path] # 1 scene
},
{
'videos': [self.vid2_path] # 1 scene
},
{
'videos': [self.vid3_path] # 1 scene
}
]
tgt_list = [{'scene_num': 1}, {'scene_num': 1}, {'scene_num': 1}]
op = VideoSplitBySceneMapper(detector='ThresholdDetector',
threshold=12.0,
min_scene_len=15)
self._run_helper(op, ds_list, tgt_list)
def test_default_progress(self):
ds_list = [
{
'videos': [self.vid1_path] # 3 scenes
},
{
'videos': [self.vid2_path] # 1 scene
},
{
'videos': [self.vid3_path] # 2 scenes
}
]
tgt_list = [{'scene_num': 3}, {'scene_num': 1}, {'scene_num': 2}]
op = VideoSplitBySceneMapper(show_progress=True)
self._run_helper(op, ds_list, tgt_list)
def test_default_kwargs(self):
ds_list = [
{
'videos': [self.vid1_path] # 2 scenes
},
{
'videos': [self.vid2_path] # 1 scene
},
{
'videos': [self.vid3_path] # 2 scenes
}
]
tgt_list = [{'scene_num': 2}, {'scene_num': 1}, {'scene_num': 2}]
op = VideoSplitBySceneMapper(luma_only=True, kernel_size=5)
self._run_helper(op, ds_list, tgt_list)
def test_default_with_text(self):
ds_list = [
{
'text':
f'{SpecialTokens.video} this is video1 {SpecialTokens.eoc}',
'videos': [self.vid1_path] # 3 scenes
},
{
'text':
f'{SpecialTokens.video} this is video2 {SpecialTokens.eoc}',
'videos': [self.vid2_path] # 1 scene
},
{
'text':
f'{SpecialTokens.video} this is video3 {SpecialTokens.eoc}',
'videos': [self.vid3_path] # 2 scenes
}
]
tgt_list = [
{
'text':
f'{SpecialTokens.video}{SpecialTokens.video}{SpecialTokens.video} this is video1 {SpecialTokens.eoc}', # noqa: E501
'scene_num': 3
},
{
'text':
f'{SpecialTokens.video} this is video2 {SpecialTokens.eoc}',
'scene_num': 1
},
{
'text':
f'{SpecialTokens.video}{SpecialTokens.video} this is video3 {SpecialTokens.eoc}', # noqa: E501
'scene_num': 2
}
]
op = VideoSplitBySceneMapper()
self._run_helper(op, ds_list, tgt_list)
if __name__ == '__main__':
unittest.main()