pyannote_rs

Struct EmbeddingExtractor

Source
pub struct EmbeddingExtractor { /* private fields */ }

Implementations§

Source§

impl EmbeddingExtractor

Source

pub fn new<P: AsRef<Path>>(model_path: P) -> Result<Self>

Examples found in repository?
examples/infinite.rs (line 43)
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
fn main() -> Result<(), eyre::Report> {
    let audio_path = std::env::args().nth(1).expect("Please specify audio file");
    let search_threshold = 0.5;

    let embedding_model_path = "wespeaker_en_voxceleb_CAM++.onnx";
    let segmentation_model_path = "segmentation-3.0.onnx";

    let (samples, sample_rate) = pyannote_rs::read_wav(&audio_path)?;
    let mut embedding_extractor = EmbeddingExtractor::new(embedding_model_path)?;
    let mut embedding_manager = EmbeddingManager::new(usize::MAX);

    let segments = pyannote_rs::get_segments(&samples, sample_rate, segmentation_model_path)?;

    for segment in segments {
        if let Ok(segment) = segment {
            if let Err(error) = process_segment(
                segment,
                &mut embedding_extractor,
                &mut embedding_manager,
                search_threshold,
            ) {
                eprintln!("Error processing segment: {:?}", error);
            }
        } else if let Err(error) = segment {
            eprintln!("Failed to process segment: {:?}", error);
        }
    }

    Ok(())
}
More examples
Hide additional examples
examples/max_speakers.rs (line 8)
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
fn main() {
    let audio_path = std::env::args().nth(1).expect("Please specify audio file");
    let (samples, sample_rate) = pyannote_rs::read_wav(&audio_path).unwrap();
    let max_speakers = 6;

    let mut extractor = EmbeddingExtractor::new("wespeaker_en_voxceleb_CAM++.onnx").unwrap();
    let mut manager = EmbeddingManager::new(6);

    let segments =
        pyannote_rs::get_segments(&samples, sample_rate, "segmentation-3.0.onnx").unwrap();

    for segment in segments {
        match segment {
            Ok(segment) => {
                if let Ok(embedding) = extractor.compute(&segment.samples) {
                    let speaker = if manager.get_all_speakers().len() == max_speakers {
                        manager
                            .get_best_speaker_match(embedding.collect())
                            .map(|s| s.to_string())
                            .unwrap_or("?".into())
                    } else {
                        manager
                            .search_speaker(embedding.collect(), 0.5)
                            .map(|s| s.to_string())
                            .unwrap_or("?".into())
                    };
                    println!(
                        "start = {:.2}, end = {:.2}, speaker = {}",
                        segment.start, segment.end, speaker
                    );
                } else {
                    println!(
                        "start = {:.2}, end = {:.2}, speaker = ?",
                        segment.start, segment.end
                    );
                }
            }
            Err(error) => eprintln!("Failed to process segment: {:?}", error),
        }
    }
}
Source

pub fn compute(&mut self, samples: &[i16]) -> Result<impl Iterator<Item = f32>>

Examples found in repository?
examples/infinite.rs (line 17)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn process_segment(
    segment: pyannote_rs::Segment,
    embedding_extractor: &mut EmbeddingExtractor,
    embedding_manager: &mut EmbeddingManager,
    search_threshold: f32,
) -> Result<(), eyre::Report> {
    let embedding_result: Vec<f32> = embedding_extractor
        .compute(&segment.samples)
        .unwrap()
        .collect();

    let speaker = embedding_manager
        .search_speaker(embedding_result.clone(), search_threshold)
        .ok_or_else(|| embedding_manager.search_speaker(embedding_result, 0.0)) // Ensure always to return speaker
        .map(|r| r.to_string())
        .unwrap_or("?".into());

    println!(
        "start = {:.2}, end = {:.2}, speaker = {}",
        segment.start, segment.end, speaker
    );

    Ok(())
}
More examples
Hide additional examples
examples/max_speakers.rs (line 17)
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
fn main() {
    let audio_path = std::env::args().nth(1).expect("Please specify audio file");
    let (samples, sample_rate) = pyannote_rs::read_wav(&audio_path).unwrap();
    let max_speakers = 6;

    let mut extractor = EmbeddingExtractor::new("wespeaker_en_voxceleb_CAM++.onnx").unwrap();
    let mut manager = EmbeddingManager::new(6);

    let segments =
        pyannote_rs::get_segments(&samples, sample_rate, "segmentation-3.0.onnx").unwrap();

    for segment in segments {
        match segment {
            Ok(segment) => {
                if let Ok(embedding) = extractor.compute(&segment.samples) {
                    let speaker = if manager.get_all_speakers().len() == max_speakers {
                        manager
                            .get_best_speaker_match(embedding.collect())
                            .map(|s| s.to_string())
                            .unwrap_or("?".into())
                    } else {
                        manager
                            .search_speaker(embedding.collect(), 0.5)
                            .map(|s| s.to_string())
                            .unwrap_or("?".into())
                    };
                    println!(
                        "start = {:.2}, end = {:.2}, speaker = {}",
                        segment.start, segment.end, speaker
                    );
                } else {
                    println!(
                        "start = {:.2}, end = {:.2}, speaker = ?",
                        segment.start, segment.end
                    );
                }
            }
            Err(error) => eprintln!("Failed to process segment: {:?}", error),
        }
    }
}

Trait Implementations§

Source§

impl Debug for EmbeddingExtractor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more