Files
pyannote-audio/tutorials/speaker_verification.ipynb
Hervé BREDIN 2623cf1980 BREAKING(io): update Audio behavior for multi-channel audio
- BREAKING(io): channels are now 0-indexed (used to be 1-indexed)
- BREAKING(io): multi-channel audio is no longer downmixed to mono by default.
  You should update how `pyannote.audio.core.io.Audio` is instantiated:
  * replace `Audio()` by `Audio(mono="downmix")`;
  * replace `Audio(mono=True)` by `Audio(mono="downmix")`;
  * replace `Audio(mono=False)` by `Audio()`.
2023-04-21 15:14:56 +02:00

1.7 KiB

Speaker verification

import torch
from pyannote.audio.pipelines.speaker_verification import PretrainedSpeakerEmbedding
model = PretrainedSpeakerEmbedding(
    "speechbrain/spkrec-ecapa-voxceleb",
    device=torch.device("cuda"))

from pyannote.audio import Audio
from pyannote.core import Segment
audio = Audio(sample_rate=16000, mono="downmix")

# extract embedding for a speaker speaking between t=3s and t=6s
speaker1 = Segment(3., 6.)
waveform1, sample_rate = audio.crop("audio.wav", speaker1)
embedding1 = model(waveform1[None])

# extract embedding for a speaker speaking between t=7s and t=12s
speaker2 = Segment(7., 12.)
waveform2, sample_rate = audio.crop("audio.wav", speaker2)
embedding2 = model(waveform2[None])

# compare embeddings using "cosine" distance
from scipy.spatial.distance import cdist
distance = cdist(embedding1, embedding2, metric="cosine")