| import argparse |
| import torch |
| import os |
| import sys |
| import numpy as np |
| import soundfile as sf |
| import torch.utils.data as data |
| import librosa |
| import cv2 as cv |
| import tqdm |
| from tools import audioread, audiowrite, cal_SISNR, load_model |
| |
| import pdb |
| import math |
| import torch.nn.functional as F |
| import scipy.io.wavfile as wavfile |
|
|
| sys.path.append('/mnt/users/hccl.local/wwu/SEMO-621/data_preparation/') |
| from Visual_perturb import * |
|
|
| |
| |
|
|
| |
|
|
|
|
| |
| class dataset_wwu_multispk(data.Dataset): |
| def __init__(self,partition |
| ): |
| self.if_fixed =False |
| self.max_length=6 |
| self.sampling_rate = 16000 |
| self.four2six_length = False |
| self.minibatch =[] |
| self.audio_direc = '/mnt/users/hccl.local/wwu/Voxceleb2/muse/audio_clean/' |
| self.text_direc = '/home/export/base/sc100138/sc100138/online1/wenxuan_tse_data/audio_clean_text/' |
| self.visual_direc = '/mnt/users/hccl.local/wwu/Voxceleb2/origin/video/' |
| self.mixture_direc = '/mnt/Corpus-Upload/Voxceleb2/origin/mixture_3spk/' |
| |
| self.partition = partition |
| self.mix_lst_path = '/mnt/users/hccl.local/wwu/mamba_may20/mixture_data_list_3mix.csv' |
| self.C=3 |
| self.batch_size = 3 |
| self.normMean = 0.4161 |
| self.normStd = 0.1688 |
| self.fps = 25 |
| |
| |
| mix_lst=open(self.mix_lst_path).read().splitlines() |
| mix_lst=list(filter(lambda x: x.split(',')[0]==self.partition, mix_lst))[:] |
| |
| assert (self.batch_size%self.C) == 0, "input batch_size should be multiples of mixture speakers" |
|
|
| self.batch_size = int(self.batch_size/self.C ) |
| |
| |
| |
| sorted_mix_lst = sorted(mix_lst, key=lambda data: float(data.split(',')[-1]), reverse=True) |
| |
| start = 0 |
| while True: |
| end = min(len(sorted_mix_lst), start + self.batch_size) |
| self.minibatch.append(sorted_mix_lst[start:end]) |
| if end == len(sorted_mix_lst): |
| break |
| start = end |
| def __len__(self): |
| return len(self.minibatch) |
| def _audio_norm(self,audio): |
| return np.divide(audio, np.max(np.abs(audio))) |
| def __getitem__(self, index): |
| |
| batch_lst = self.minibatch[index] |
| |
| min_length = self.max_length |
| for _ in range(len(batch_lst)): |
| if float(batch_lst[_].split(",")[-1]) < min_length: |
| min_length = float(batch_lst[_].split(",")[-1]) |
| |
| visuals=[] |
| audios = [] |
| texts = [] |
| file_base_path = [] |
| mixtures = [] |
| |
| for line in batch_lst: |
| mixture_path=self.mixture_direc+self.partition+'/'+ line.replace(',','_').replace('/','_')+'.wav' |
| |
| line=line.split(',') |
| for c in range(self.C): |
| |
| _, mixture_speech = wavfile.read(mixture_path) |
| mix_audio_data = self._audio_norm(mixture_speech[:int(min_length * self.sampling_rate)]) |
| |
| |
| mixtures.append(mix_audio_data) |
| |
| clean_speech_path=self.audio_direc+line[c*4+1]+'/'+line[c*4+2]+'/'+line[c*4+3]+'.wav' |
| file_base_path.append(line[c*4+1]+'/'+line[c*4+2]+'/'+line[c*4+3]) |
| |
| _, clean_speech = wavfile.read(clean_speech_path) |
| |
| audio_data = self._audio_norm(clean_speech[:int(min_length * self.sampling_rate)]) |
| |
| |
| |
| text = '' |
| |
| |
| |
| |
| |
| audios.append(audio_data) |
| texts.append(text) |
| |
| |
| if self.partition != 'test': |
| visual_path = ( |
| self.visual_direc |
| + 'train' |
| + "/" |
| + line[2 + c * 4] |
| + "/" |
| + line[3 + c * 4] |
| + ".mp4" |
| ) |
| else: |
| |
| visual_path = ( |
| self.visual_direc |
| + line[1 + c * 4] |
| + "/" |
| + line[2 + c * 4] |
| + "/" |
| + line[3 + c * 4] |
| + ".mp4" |
| ) |
| |
| captureObj = cv.VideoCapture(visual_path) |
| |
| |
| roiSequence = [] |
| clean_roiSequence = [] |
| roiSize = 112 |
| start = 0 |
| while captureObj.isOpened(): |
| ret, frame = captureObj.read() |
| if ret == True: |
| grayed = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) |
| grayed = grayed / 255 |
| grayed = cv.resize(grayed, (roiSize * 2, roiSize * 2)) |
| roi = grayed[ |
| int(roiSize - (roiSize / 2)) : int( |
| roiSize + (roiSize / 2) |
| ), |
| int(roiSize - (roiSize / 2)) : int( |
| roiSize + (roiSize / 2) |
| ), |
| ] |
| clean_roiSequence.append(roi) |
| else: |
| break |
| captureObj.release() |
| |
| clean_visual = np.asarray(clean_roiSequence) |
| clean_visual = clean_visual[0 : int(min_length * self.fps), ...] |
| clean_visual = (clean_visual - self.normMean) / self.normStd |
| if clean_visual.shape[0] < int(min_length * self.fps): |
| clean_visual = np.pad( |
| clean_visual, |
| ( |
| ( |
| 0, |
| int(min_length * self.fps) |
| - clean_visual.shape[0], |
| ), |
| (0, 0), |
| (0, 0), |
| ), |
| mode="edge", |
| ) |
| visuals.append(clean_visual) |
| |
| |
| |
| |
| |
| assert np.asarray(mixtures).shape == np.asarray(audios).shape |
| |
| return np.asarray(mixtures),np.asarray(audios),np.asarray(visuals), texts,file_base_path |
|
|
| |
|
|
|
|
|
|
| def main(args): |
|
|
| |
| |
| mix_lst = open(args.mix_lst_path).read().splitlines() |
| train_lst = list(filter(lambda x: x.split(",")[0] == "test", mix_lst)) |
| IDs = 0 |
| speaker_dict = {} |
| for line in train_lst: |
| for i in range(2): |
| ID = line.split(",")[i * 4 + 2] |
| if ID not in speaker_dict: |
| speaker_dict[ID] = IDs |
| IDs += 1 |
| args.speaker_dict = speaker_dict |
| args.speakers = len(speaker_dict) |
|
|
| |
| |
| |
| datasets = dataset_wwu_multispk('test') |
|
|
| |
|
|
| |