Files
audio2motion/scripts/GRID/run-25-extract-mel-feature.py
2022-01-21 17:42:41 +08:00

98 lines
3.5 KiB
Python

import os,sys
import numpy as np
import librosa
import python_speech_features
from pathlib import Path
import multiprocessing
path_work = "/mnt/data/CODE/Audio-driven-TalkingFace-HeadPose/audio-to-expression"
sys.path.insert(0, path_work)
from src.tools.audio import get_melspec
def splited_by_windows(data, wind_stride, wind_length, padding=True):
"""
data: seq_len1 x dim1
return: seq_len2 x wind_length x dim1
where seq_len2 = (L + 2p - k) // wind_stride + 1
"""
seq_len1, dim1 = data.shape
seq_len2 = seq_len1 // wind_stride + 1
#print("DATA befor shape=", data.shape) # DATA befor shape= (121, 13)
data = np.pad(data, pad_width=((wind_length//2,wind_length//2), (0,0)), mode="reflect")
#print("DATA after shape=", data.shape) # DATA after shape= (149, 13)
data_output = np.zeros((seq_len2, wind_length, dim1-1), dtype=float)
for i in range(seq_len2):
ibeg = i * wind_stride
iend = i * wind_stride + wind_length
data_output[i, :, :] = data[ibeg : iend, 1:] # TODO: why form 1:
return data_output
# def func_processing(path_audio):
if __name__ == "__main__":
path_data = Path("/mnt/data/DATA/GRID/processing")
v_ids = sorted(path_data.glob("*"))
print(f"Have {len(v_ids)} dir files, \n {v_ids[:3]}")
# for each person
for i, ipath_id in enumerate(v_ids):
id_name = ipath_id.stem
dir_video = path_data.joinpath(id_name)
v_dir_videos = sorted(dir_video.glob("*/video/video_orignal.mp4"))
print(f"\nid= {id_name}, Have {len(v_dir_videos)} video files, \n {v_dir_videos[:3]}")
# for each video
for j, jpath_videos in enumerate(v_dir_videos):
# if (j+1) % 100 == 0: print(f"processing {j+1}/{len(v_dir_videos)}")
jpath_data = jpath_videos.parent.parent
jpath_frame = jpath_data.joinpath("frames")
nframes = len(sorted(jpath_frame.glob("*.jpg")))
#print(f"jpath_data = {jpath_data}, nframe = {nframes}")
ipath_audio = jpath_data.joinpath("audio/audio_extract.wav")
ipath_feat_save = jpath_data.joinpath("audio/melspec_feat.npy")
# for some data not have .wav files
if not ipath_audio.exists():
print(f"NOT EXISTING: {ipath_audio}")
continue
if ipath_feat_save.exists():
print(f"EXSTING, {ipath_feat_save}")
continue
# extarct mfcc feature
audio, sr = librosa.load(str(ipath_audio), sr=16000) #16000, (69760, )
mfcc_orig = python_speech_features.mfcc(audio, 16000, winstep=0.01)
y, sr = librosa.load(str(ipath_audio), sr=44100)
melFrames = np.transpose(get_melspec(y, sr, wsize=0.04, hsize=0.04)) # (n_frame, 64)
melDelta = np.insert(np.diff(melFrames, n=1, axis=0), 0, np.zeros((1, 64)), axis=0) # (n_frame - 1 , 64) + (1, 64) --> (n_frame, 64)
melDDelta = np.insert(np.diff(melFrames, n=2, axis=0), 0, np.zeros((2, 64)), axis=0) # (n_frame - 2 , 64) + (2, 64) --> (n_frame, 64)
data_audio = np.concatenate((melDelta, melDDelta), axis=1) # (n_frame, 128)
# save
np.save(str(ipath_feat_save), data_audio)
# print(f"SAVE: mfcc size={data_audio.shape} --> {str(ipath_feat_save)}") # mfcc size=(31, 28, 12)
# break
# break