-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathmsdd.py
More file actions
105 lines (87 loc) · 3.54 KB
/
Copy pathmsdd.py
File metadata and controls
105 lines (87 loc) · 3.54 KB
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
import json
import os
import tempfile
from typing import Union
import torch
import torchaudio
from nemo.collections.asr.models.msdd_models import NeuralDiarizer
from nemo.collections.asr.parts.utils.speaker_utils import rttm_to_labels
from omegaconf import OmegaConf
class MSDDDiarizer:
def __init__(self, device: Union[str, torch.device]):
self.model: NeuralDiarizer = NeuralDiarizer(cfg=create_config()).to(device)
def diarize(self, audio: torch.Tensor):
with tempfile.TemporaryDirectory() as temp_path:
torchaudio.save(
os.path.join(temp_path, "mono_file.wav"),
audio,
16000,
channels_first=True,
)
manifest_path = os.path.join(temp_path, "manifest.json")
meta = {
"audio_filepath": os.path.join(temp_path, "mono_file.wav"),
"offset": 0,
"duration": None,
"label": "infer",
"text": "-",
"rttm_filepath": None,
"uem_filepath": None,
}
with open(manifest_path, "w") as f:
json.dump(meta, f)
self.model._initialize_configs(
manifest_path=manifest_path,
max_speakers=8,
num_speakers=None,
tmpdir=temp_path,
batch_size=24,
num_workers=0,
verbose=True,
)
self.model.clustering_embedding.clus_diar_model._diarizer_params.out_dir = (
temp_path
)
self.model.clustering_embedding.clus_diar_model._diarizer_params.manifest_filepath = (
manifest_path
)
self.model.msdd_model.cfg.test_ds.manifest_filepath = manifest_path
self.model.diarize()
pred_labels_clus = rttm_to_labels(
os.path.join(temp_path, "pred_rttms", "mono_file.rttm")
)
labels = []
for label in pred_labels_clus:
start, end, speaker = label.split()
start, end = float(start), float(end)
start, end = int(start * 1000), int(end * 1000)
labels.append((start, end, int(speaker.split("_")[1])))
# pred_labels_clus = [label.split() for label in pred_labels_clus]
# labels = [
# (int(start * 1000), int(end*1000), int(speaker.split("_")[1]))
# for start, end, speaker in pred_labels_clus
# ]
labels = sorted(labels, key=lambda x: x[0])
return labels
def create_config():
config = OmegaConf.load(
os.path.join(os.path.dirname(__file__), "diar_infer_telephonic.yaml")
)
pretrained_vad = "vad_multilingual_marblenet"
pretrained_speaker_model = "titanet_large"
config.diarizer.out_dir = None
config.diarizer.manifest_filepath = None
config.diarizer.speaker_embeddings.model_path = pretrained_speaker_model
config.diarizer.oracle_vad = (
False # compute VAD provided with model_path to vad config
)
config.diarizer.clustering.parameters.oracle_num_speakers = False
# Here, we use our in-house pretrained NeMo VAD model
config.diarizer.vad.model_path = pretrained_vad
config.diarizer.vad.parameters.onset = 0.8
config.diarizer.vad.parameters.offset = 0.6
config.diarizer.vad.parameters.pad_offset = -0.05
config.diarizer.msdd_model.model_path = (
"diar_msdd_telephonic" # Telephonic speaker diarization model
)
return config