File size: 6,018 Bytes
7661f93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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()