🚀 🎹 說話人分割
本項目基於 pyannote.audio 2.0
實現說話人分割功能,能夠有效識別音頻中的不同說話人。它可以自動處理音頻,無需手動進行語音活動檢測、指定說話人數量或對內部模型進行微調。
🚀 快速開始
本項目依賴於 pyannote.audio 2.0
,請參考 安裝說明 進行安裝。
💻 使用示例
基礎用法
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained("pyannote/speaker-diarization@2022.07")
diarization = pipeline("audio.wav")
with open("audio.rttm", "w") as rttm:
diarization.write_rttm(rttm)
高級用法
如果事先知道說話人的數量,可以在參數字典中包含 num_speakers
參數:
handler = EndpointHandler()
diarization = handler({"inputs": base64_audio, "parameters": {"num_speakers": 2}})
也可以使用 min_speakers
和 max_speakers
參數提供說話人數量的下限和/或上限:
handler = EndpointHandler()
diarization = handler({"inputs": base64_audio, "parameters": {"min_speakers": 2, "max_speakers": 5}})
如果您想進行更多嘗試,可以試驗各種管道超參數。例如,可以通過增加 segmentation_onset
閾值的值來使用更激進的語音活動檢測:
hparams = handler.pipeline.parameters(instantiated=True)
hparams["segmentation_onset"] += 0.1
handler.pipeline.instantiate(hparams)
要應用更新後的處理程序進行可以處理說話人數量的 API 推理,請使用以下代碼:
from typing import Dict
from pyannote.audio import Pipeline
import torch
import base64
import numpy as np
SAMPLE_RATE = 16000
class EndpointHandler():
def __init__(self, path=""):
self.pipeline = Pipeline.from_pretrained("KIFF/pyannote-speaker-diarization-endpoint")
def __call__(self, data: Dict[str, bytes]) -> Dict[str, str]:
"""
Args:
data (:obj:):
包含反序列化後的音頻文件字節
Return:
A :obj:`dict`:. base64 編碼的圖像
"""
inputs = data.pop("inputs", data)
parameters = data.pop("parameters", None)
audio_data = base64.b64decode(inputs)
audio_nparray = np.frombuffer(audio_data, dtype=np.int16)
audio_tensor= torch.from_numpy(audio_nparray).float().unsqueeze(0)
pyannote_input = {"waveform": audio_tensor, "sample_rate": SAMPLE_RATE}
if parameters is not None:
diarization = self.pipeline(pyannote_input, **parameters)
else:
diarization = self.pipeline(pyannote_input)
processed_diarization = [
{"label": str(label), "start": str(segment.start), "stop": str(segment.end)}
for segment, _, label in diarization.itertracks(yield_label=True)
]
return {"diarization": processed_diarization}
🔧 技術細節
即時因子
使用一塊 Nvidia Tesla V100 SXM2 GPU(用於神經推理部分)和一塊 Intel Cascade Lake 6248 CPU(用於聚類部分)時,即時因子約為 5%。
換句話說,處理一小時的對話大約需要 3 分鐘。
準確性
該管道在不斷增加的數據集上進行了基準測試。
處理過程完全自動化:
- 無需手動進行語音活動檢測(文獻中有時會這樣做)
- 無需手動指定說話人數量(儘管可以將其提供給管道)
- 無需對內部模型進行微調,也無需針對每個數據集調整管道超參數
... 採用最嚴格的說話人分割錯誤率(DER)設置(在 本文 中稱為 "Full"):
📚 詳細文檔
📄 許可證
本項目採用 MIT 許可證。
📚 引用
@inproceedings{Bredin2021,
Title = {{End-to-end speaker segmentation for overlap-aware resegmentation}},
Author = {{Bredin}, Herv{\'e} and {Laurent}, Antoine},
Booktitle = {Proc. Interspeech 2021},
Address = {Brno, Czech Republic},
Month = {August},
Year = {2021},
}
@inproceedings{Bredin2020,
Title = {{pyannote.audio: neural building blocks for speaker diarization}},
Author = {{Bredin}, Herv{\'e} and {Yin}, Ruiqing and {Coria}, Juan Manuel and {Gelly}, Gregory and {Korshunov}, Pavel and {Lavechin}, Marvin and {Fustes}, Diego and {Titeux}, Hadrien and {Bouaziz}, Wassim and {Gill}, Marie-Philippe},
Booktitle = {ICASSP 2020, IEEE International Conference on Acoustics, Speech, and Signal Processing},
Address = {Barcelona, Spain},
Month = {May},
Year = {2020},
}