File size: 8,272 Bytes
039cdd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
213
214
215
216
217
218
219
220
221
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 fast_bss_eval
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
        # import pdb;pdb.set_trace()
 
        mix_lst=open(self.mix_lst_path).read().splitlines()
        mix_lst=list(filter(lambda x: x.split(',')[0]==self.partition, mix_lst))[:]
        # import pdb;pdb.set_trace()
        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 by mix_audio duration length!!!
        # import pdb;pdb.set_trace()
        # sorted_mix_lst = sorted(mix_lst, key=lambda data: float(data.split(',')[-1]), reverse=True)[:6]
        sorted_mix_lst = sorted(mix_lst, key=lambda data: float(data.split(',')[-1]), reverse=True)
        # import pdb;pdb.set_trace()
        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):
        # import pdb;pdb.set_trace()
        batch_lst = self.minibatch[index]
        # import pdb;pdb.set_trace()
        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])
        # import pdb;pdb.set_trace()
        visuals=[]
        audios = []
        texts = []
        file_base_path = []
        mixtures = []
        # import pdb;pdb.set_trace()
        for line in batch_lst:
            mixture_path=self.mixture_direc+self.partition+'/'+ line.replace(',','_').replace('/','_')+'.wav'
            # import pdb;pdb.set_trace()
            line=line.split(',')
            for c in range(self.C):
                # each line has 2 sample, batch =2, return same mixaudio, so return mix audio twice!!!!!!
                _, mixture_speech = wavfile.read(mixture_path)
                mix_audio_data = self._audio_norm(mixture_speech[:int(min_length * self.sampling_rate)]) 
                # mix_log_feat = self.compute_logfilter(mix_audio_data)
                # mixtures.append(mix_log_feat)
                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])
                # import pdb;pdb.set_trace()
                _, clean_speech = wavfile.read(clean_speech_path)
                # print(len(clean_speech),clean_speech_path)
                audio_data = self._audio_norm(clean_speech[:int(min_length * self.sampling_rate)]) 
                # text_path = self.text_direc+line[c*4+1]+'/'+line[c*4+2]+'/'+line[c*4+3]+'.txt' 
                # with open(text_path, 'r', encoding='utf-8') as file:
                #     text = file.readlines()[0].split('\n')[0]
                text = ''
                # print(text)
                # import pdb;pdb.set_trace()
                
                # #print(audio_data)
                # log_feat = self.compute_logfilter(audio_data)
                audios.append(audio_data)
                texts.append(text)
                # audios.append([])
                
                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"
                    )
                    # import pdb;pdb.set_trace()
                captureObj = cv.VideoCapture(visual_path)
                
                # import pdb;pdb.set_trace()
                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()
                # import pdb;pdb.set_trace()
                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
        # import pdb;pdb.set_trace()
        return np.asarray(mixtures),np.asarray(audios),np.asarray(visuals), texts,file_base_path

 



def main(args):

 
     # speaker id assignment
    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')