Spaces:
Running
Running
File size: 5,563 Bytes
f74ae8e ad1f7b5 6c8bea2 f74ae8e 6c8bea2 f74ae8e |
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 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
import os
from pathlib import Path
import random
import sys
import shutil
pwd = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(pwd, "../../"))
import pandas as pd
from scipy.io import wavfile
from tqdm import tqdm
import librosa
from project_settings import project_path
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--file_dir", default="./", type=str)
parser.add_argument(
"--noise_dir",
default=r"E:\Users\tianx\HuggingDatasets\nx_noise\data\noise",
type=str
)
parser.add_argument(
"--speech_dir",
default=r"E:\programmer\asr_datasets\aishell\data_aishell\wav\train",
type=str
)
parser.add_argument("--train_dataset", default="train.xlsx", type=str)
parser.add_argument("--valid_dataset", default="valid.xlsx", type=str)
parser.add_argument("--duration", default=2.0, type=float)
parser.add_argument("--min_snr_db", default=-10, type=float)
parser.add_argument("--max_snr_db", default=20, type=float)
parser.add_argument("--target_sample_rate", default=8000, type=int)
parser.add_argument("--scale", default=1, type=float)
args = parser.parse_args()
return args
def filename_generator(data_dir: str):
data_dir = Path(data_dir)
for filename in data_dir.glob("**/*.wav"):
yield filename.as_posix()
def target_second_signal_generator(data_dir: str, duration: int = 2, sample_rate: int = 8000):
data_dir = Path(data_dir)
for filename in data_dir.glob("**/*.wav"):
signal, _ = librosa.load(filename.as_posix(), sr=sample_rate)
raw_duration = librosa.get_duration(y=signal, sr=sample_rate)
if raw_duration < duration:
# print(f"duration less than {duration} s. skip filename: {filename.as_posix()}")
continue
if signal.ndim != 1:
raise AssertionError(f"expected ndim 1, instead of {signal.ndim}")
signal_length = len(signal)
win_size = int(duration * sample_rate)
for begin in range(0, signal_length - win_size, win_size):
row = {
"filename": filename.as_posix(),
"raw_duration": round(raw_duration, 4),
"offset": round(begin / sample_rate, 4),
"duration": round(duration, 4),
}
yield row
def get_dataset(args):
file_dir = Path(args.file_dir)
file_dir.mkdir(exist_ok=True)
noise_dir = Path(args.noise_dir)
speech_dir = Path(args.speech_dir)
noise_generator = target_second_signal_generator(
noise_dir.as_posix(),
duration=args.duration,
sample_rate=args.target_sample_rate
)
speech_generator = target_second_signal_generator(
speech_dir.as_posix(),
duration=args.duration,
sample_rate=args.target_sample_rate
)
dataset = list()
count = 0
process_bar = tqdm(desc="build dataset excel")
for noise, speech in zip(noise_generator, speech_generator):
flag = random.random()
if flag > args.scale:
continue
noise_filename = noise["filename"]
noise_raw_duration = noise["raw_duration"]
noise_offset = noise["offset"]
noise_duration = noise["duration"]
speech_filename = speech["filename"]
speech_raw_duration = speech["raw_duration"]
speech_offset = speech["offset"]
speech_duration = speech["duration"]
random1 = random.random()
random2 = random.random()
row = {
"noise_filename": noise_filename,
"noise_raw_duration": noise_raw_duration,
"noise_offset": noise_offset,
"noise_duration": noise_duration,
"speech_filename": speech_filename,
"speech_raw_duration": speech_raw_duration,
"speech_offset": speech_offset,
"speech_duration": speech_duration,
"snr_db": random.uniform(args.min_snr_db, args.max_snr_db),
"random1": random1,
"random2": random2,
"flag": "TRAIN" if random2 < 0.8 else "TEST",
}
dataset.append(row)
count += 1
duration_seconds = count * args.duration
duration_hours = duration_seconds / 3600
process_bar.update(n=1)
process_bar.set_postfix({
# "duration_seconds": round(duration_seconds, 4),
"duration_hours": round(duration_hours, 4),
})
dataset = pd.DataFrame(dataset)
dataset = dataset.sort_values(by=["random1"], ascending=False)
dataset.to_excel(
file_dir / "dataset.xlsx",
index=False,
)
return
def split_dataset(args):
"""分割训练集, 测试集"""
file_dir = Path(args.file_dir)
file_dir.mkdir(exist_ok=True)
df = pd.read_excel(file_dir / "dataset.xlsx")
train = list()
test = list()
for i, row in df.iterrows():
flag = row["flag"]
if flag == "TRAIN":
train.append(row)
else:
test.append(row)
train = pd.DataFrame(train)
train.to_excel(
args.train_dataset,
index=False,
# encoding="utf_8_sig"
)
test = pd.DataFrame(test)
test.to_excel(
args.valid_dataset,
index=False,
# encoding="utf_8_sig"
)
return
def main():
args = get_args()
get_dataset(args)
split_dataset(args)
return
if __name__ == "__main__":
main()
|