international_voice / examples /make_callbot_test_media.py
HoneyTian's picture
update
7661f93
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import argparse
from collections import defaultdict
import json
from pathlib import Path
import numpy as np
from scipy.io import wavfile
import torch
from tqdm import tqdm
from typing import List
from project_settings import project_path
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--call_monitor_examples_wav_dir",
default=(project_path / "data/call_monitor_examples_wav/id-ID").as_posix(),
type=str
)
parser.add_argument(
"--output_dir",
default=(project_path / "data/voice_test_examples").as_posix(),
type=str
)
parser.add_argument("--n_samples", default=7, type=int)
args = parser.parse_args()
return args
def save_media(output_dir: Path, language: str, category: str, call_id: str,
early_media: np.ndarray, active_media: np.ndarray = None,
sample_rate: int = 8000):
early_media_filename = output_dir / "{}/{}/early_media_{}.wav".format(language, category, call_id)
active_media_filename = output_dir / "{}/{}/active_media_{}.wav".format(language, category, call_id)
early_media_filename.parent.mkdir(parents=True, exist_ok=True)
wavfile.write(early_media_filename.as_posix(), sample_rate, early_media)
if active_media is not None:
active_media_filename.parent.mkdir(parents=True, exist_ok=True)
wavfile.write(active_media_filename.as_posix(), sample_rate, active_media)
def main():
args = get_args()
call_monitor_examples_wav_dir = Path(args.call_monitor_examples_wav_dir)
output_dir = Path(args.output_dir)
counter = defaultdict(int)
metadata_file = call_monitor_examples_wav_dir / "metadata.json"
with open(metadata_file.as_posix(), "r", encoding="utf-8") as f:
metadata = json.load(f)
for meta in metadata:
filename = meta["filename"]
early_media_label = meta["early_media_label"]
early_media_ts = meta["early_media_ts"]
on_answer_label = meta["on_answer_label"]
on_answer_ts = meta["on_answer_ts"]
filename = call_monitor_examples_wav_dir / filename
call_id = filename.stem
language = filename.parts[-2]
sample_rate, signal = wavfile.read(filename.as_posix())
if on_answer_ts is None:
early_media = signal
active_media = None
else:
early_media_n_samples = int(on_answer_ts / 1000 * sample_rate)
early_media = signal[:early_media_n_samples]
active_media = signal[early_media_n_samples:]
append_length = 16000 * 15
if len(active_media) < 16000:
multiple = append_length / len(active_media)
active_media_ = [active_media] + [active_media[-16000:]] * int(multiple)
else:
active_media_ = [active_media] + [active_media[-16000:]] * 15
active_media = np.concatenate(active_media_, axis=0)
category = "other"
if on_answer_label in ("voicemail",):
category = "01"
if counter[category] > args.n_samples:
continue
counter[category] += 1
save_media(
output_dir=output_dir,
language=language,
category=category,
call_id=call_id,
early_media=early_media,
active_media=active_media,
sample_rate=sample_rate,
)
if on_answer_label in ("mute", "white_noise"):
category = "03"
if counter[category] > args.n_samples:
continue
counter[category] += 1
save_media(
output_dir=output_dir,
language=language,
category=category,
call_id=call_id,
early_media=early_media,
active_media=active_media,
sample_rate=sample_rate,
)
if early_media_label is not None and on_answer_label in ("voicemail",):
category = "04"
if counter[category] > args.n_samples:
continue
counter[category] += 1
save_media(
output_dir=output_dir,
language=language,
category=category,
call_id=call_id,
early_media=early_media,
active_media=active_media,
sample_rate=sample_rate,
)
if on_answer_label in ("voice",):
category = "05"
if counter[category] > args.n_samples:
continue
counter[category] += 1
save_media(
output_dir=output_dir,
language=language,
category=category,
call_id=call_id,
early_media=early_media,
active_media=active_media,
sample_rate=sample_rate,
)
if on_answer_ts > 25000:
category = "06"
if counter[category] > args.n_samples:
continue
counter[category] += 1
save_media(
output_dir=output_dir,
language=language,
category=category,
call_id=call_id,
early_media=early_media,
active_media=active_media,
sample_rate=sample_rate,
)
if category == "other":
if counter[category] > args.n_samples:
continue
counter[category] += 1
save_media(
output_dir=output_dir,
language=language,
category=category,
call_id=call_id,
early_media=early_media,
active_media=active_media,
sample_rate=sample_rate,
)
return
if __name__ == '__main__':
main()