-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpreprocess.py
More file actions
212 lines (182 loc) · 7.58 KB
/
preprocess.py
File metadata and controls
212 lines (182 loc) · 7.58 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import subprocess
import argparse
from librosa.feature import mfcc
import librosa
import os
import shutil
from tqdm import tqdm
from scipy.io import wavfile
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
import glob
import torch
import torch.nn as nn
import pretrainedmodels
import pretrainedmodels.utils as utils
C, H, W = 3, 224, 224
def extract_frame(video, dst):
'''
Given the input video path, convert each frame of the video
into jpg format in the destination directory.
Args:
video: video path
dst: destination folder
'''
with open(os.devnull, "w") as ffmpeg_log:
command = 'ffmpeg -i ' + video + ' -vf scale=400:300 ' + '-qscale:v 2 '+ '{0}/%06d.jpg'.format(dst)
subprocess.call(command, shell=True, stdout=ffmpeg_log, stderr=ffmpeg_log)
def extract_image_feats(opt, model, load_image_fn):
'''
Extract features by feeding a certain amount of frames jpg into
an ImageNet pre-trained model, concatnenate them together and
save the numpy array into a .npy file in the video folder.
Args:
opt: the option dictionary
model: the model to extract image features
load_image_fn: the function to load the image and feed into the CNN
'''
global C, H, W
model.eval()
dir_fc = opt['output_dir']
print('extracting video features...')
print('save video features to %s' % (dir_fc))
video_list = glob.glob(os.path.join(opt['video_dir'], '*.mp4'))
for video in tqdm(video_list):
video_id = video.split("/")[-1].split(".")[0]
dst = dir_fc + '/' + video_id
if not os.path.exists(dst):
os.mkdir(dst)
print(video_id, 'does not have audio information')
extract_frame(video, dst)
image_list = sorted(glob.glob(os.path.join(dst, '*.jpg')))
samples = np.round(np.linspace(
0, len(image_list) - 1, opt['n_frame_steps']))
image_list = [image_list[int(sample)] for sample in samples]
images = torch.zeros((len(image_list), C, H, W))
for i in range(len(image_list)):
img = load_image_fn(image_list[i])
images[i] = img
with torch.no_grad():
image_feats = model(images.cuda().squeeze())
image_feats = image_feats.cpu().numpy()
outfile = os.path.join(dst, 'video.npy')
np.save(outfile, image_feats)
for file in os.listdir(dst):
if file.endswith('.jpg'):
os.remove(os.path.join(dst, file))
def vToA(opt):
'''
Convert videos into audio .wav file. Skip the video that does not have
any sound.
Args:
opt: option dictionary
'''
video_dir = opt['video_dir']
dst = opt['output_dir']
band_width = opt['band_width']
output_channels = opt['output_channels']
output_frequency = opt['output_frequency']
# print(video_id)
if os.path.exists(dst):
print(" cleanup: " + dst + "/")
shutil.rmtree(dst)
os.makedirs(dst)
for video in tqdm(os.listdir(video_dir)):
video = video_dir + '/' + video
video_id = video.split("/")[-1].split(".")[0]
with open(os.devnull, "w") as ffmpeg_log:
command = 'ffmpeg -i ' + video + ' -ab ' + str(band_width) + 'k -ac ' + str(output_channels) + ' -ar ' + str(output_frequency) + ' -vn ' + dst + '/' + video_id + '.wav'
subprocess.call(command, shell=True, stdout=ffmpeg_log, stderr=ffmpeg_log)
def split_audio(opt):
'''
splitting audio files into 1-sec segments, and extract
MFCCs for the segment. All segments are pad into the
same temporal length, concatenated together and then pad
into a certain fixed length.
Args:
opt: option dictionary
'''
print('splitting audios...')
output_dir = opt['output_dir']
print('output directory: '+output_dir)
for audio in os.listdir(output_dir):
audio = output_dir + '/' + audio
video_id = audio.split("/")[-1].split(".")[0]
dst = output_dir + '/' + video_id
if os.path.exists(dst):
shutil.rmtree(dst)
os.mkdir(dst)
with open(os.devnull, 'w') as ffmpeg_log:
command = 'ffmpeg -i ' + audio + ' -f segment -segment_time 1 -c copy ' + dst+ '/' + '%02d.wav'
subprocess.call(command, shell=True, stdout=ffmpeg_log, stderr=ffmpeg_log)
output = np.zeros((20, 0))
for segment in os.listdir(dst):
segment = dst + '/' + segment
sample_rate, audio_info = wavfile.read(segment)
audio_length = audio_info.shape[0]
if audio_length<=16000:
audio_info = np.pad(audio_info, (0, 16000-audio_length), 'constant', constant_values=0)
else:
audio_info = audio_info[0:16000]
audio_info = audio_info.astype(np.float32)
mfcc_feats = mfcc(audio_info, sr=sample_rate)
#print(mfcc_feats.shape)
output = np.concatenate((output, mfcc_feats), axis=1)
#print(output.shape)
outfile = os.path.join(dst, 'audio.npy')
np.save(outfile, output.T)
for file in os.listdir(dst):
if file.endswith('.wav'):
os.remove(os.path.join(dst, file))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--video_dir', type=str, default='../msrvtt_2017/train-video',
help='The video dir that one would like to extract audio file from')
parser.add_argument('--output_dir', type=str, default='../msrvtt_2017/preprocessed',
help='The file output directory')
parser.add_argument('--output_channels', type=int, default=1,
help='The number of output audio channels, default to 1')
parser.add_argument('--output_frequency', type=int, default=16000,
help='The output audio frequency in Hz, default to 16000')
parser.add_argument('--band_width', type=int, default=160,
help='Bandwidth specified to sample the audio (unit in kbps), default to 160')
parser.add_argument('--model', type=str, default='resnet152',
help='The pretrained model to use for extracting image features, default to resnet152')
parser.add_argument('--gpu', type=str, default='0',
help='The CUDA_VISIBLE_DEVICES argument, default to 0')
parser.add_argument('--n_frame_steps', type=int, default=80,
help='The number of frames to extract from a single video')
opt = parser.parse_args()
opt=vars(opt)
if not os.path.exists(opt['output_dir']):
os.mkdir(opt['output_dir'])
vToA(opt)
split_audio(opt)
print('cleaning up original .wav files...')
dir = opt['output_dir']
dir = os.listdir(dir)
for file in dir:
if file.endswith('.wav'):
os.remove(os.path.join(opt['output_dir'], file))
os.environ['CUDA_VISIBLE_DEVICES'] = opt['gpu']
if opt['model'] == 'resnet152':
C, H, W = 3, 224, 224
model = pretrainedmodels.resnet152(pretrained='imagenet')
load_image_fn = utils.LoadTransformImage(model)
elif opt['model'] == 'inception_v3':
C, H, W = 3, 299, 299
model = pretrainedmodels.inceptionv3(pretrained='imagenet')
load_image_fn = utils.LoadTransformImage(model)
elif opt['model'] == 'vgg16':
C, H, W = 3, 224, 224
model = pretrainedmodels.vgg16(pretrained='imagenet')
load_image_fn = utils.LoadTransformImage(model)
else:
print('The image model is not supported')
model.last_linear = utils.Identity()
model = nn.DataParallel(model)
model = model.cuda()
extract_image_feats(opt, model, load_image_fn)
if __name__ == '__main__':
main()