mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 21:11:03 +08:00
108 lines
3.3 KiB
Rust
108 lines
3.3 KiB
Rust
// This example demonstrates how to use non-streaming Paraformer with
|
|
// sherpa-onnx's Rust API for offline speech recognition.
|
|
//
|
|
// Run it from the rust-api-examples directory with:
|
|
// ./run-paraformer.sh
|
|
|
|
use clap::Parser;
|
|
use sherpa_onnx::{
|
|
OfflineParaformerModelConfig, OfflineRecognizer, OfflineRecognizerConfig, Wave,
|
|
};
|
|
use std::{process, time::Instant};
|
|
|
|
/// Paraformer offline ASR example
|
|
#[derive(Parser, Debug)]
|
|
#[command(author, version, about, long_about = None)]
|
|
struct Args {
|
|
/// Path to WAV file
|
|
#[arg(long)]
|
|
wav: String,
|
|
|
|
/// Path to Paraformer ONNX model
|
|
#[arg(long)]
|
|
model: String,
|
|
|
|
/// Path to tokens file
|
|
#[arg(long)]
|
|
tokens: String,
|
|
|
|
/// Provider (default: cpu)
|
|
#[arg(long, default_value = "cpu")]
|
|
provider: String,
|
|
|
|
/// Enable debug logs
|
|
#[arg(long, default_value_t = false)]
|
|
debug: bool,
|
|
|
|
/// Number of threads
|
|
#[arg(long, default_value_t = 2)]
|
|
num_threads: i32,
|
|
}
|
|
|
|
fn main() {
|
|
let args = Args::parse();
|
|
|
|
let wave = Wave::read(&args.wav).expect("Failed to read WAV file");
|
|
let audio_duration = if wave.sample_rate() > 0 && !wave.samples().is_empty() {
|
|
wave.samples().len() as f64 / wave.sample_rate() as f64
|
|
} else {
|
|
0.0
|
|
};
|
|
|
|
let mut recognizer_config = OfflineRecognizerConfig::default();
|
|
|
|
recognizer_config.model_config.paraformer = OfflineParaformerModelConfig {
|
|
model: Some(args.model.clone()),
|
|
};
|
|
|
|
recognizer_config.model_config.tokens = Some(args.tokens.clone());
|
|
recognizer_config.model_config.provider = Some(args.provider.clone());
|
|
recognizer_config.model_config.debug = args.debug;
|
|
recognizer_config.model_config.num_threads = args.num_threads;
|
|
|
|
println!("Creating recognizer ...");
|
|
let start_creation = Instant::now();
|
|
|
|
let recognizer = OfflineRecognizer::create(&recognizer_config)
|
|
.expect("Failed to create OfflineRecognizer");
|
|
|
|
let creation_elapsed = start_creation.elapsed().as_secs_f64();
|
|
println!("Recognizer created in {:.3} seconds.", creation_elapsed);
|
|
|
|
let stream = recognizer.create_stream();
|
|
|
|
let start_recognition = Instant::now();
|
|
|
|
stream.accept_waveform(wave.sample_rate(), wave.samples());
|
|
recognizer.decode(&stream);
|
|
|
|
let recognition_elapsed = start_recognition.elapsed().as_secs_f64();
|
|
|
|
if let Some(result) = stream.get_result() {
|
|
println!("Decoded text: {}", result.text);
|
|
|
|
let total_time = creation_elapsed + recognition_elapsed;
|
|
let rtf = if audio_duration > 0.0 {
|
|
recognition_elapsed / audio_duration
|
|
} else {
|
|
0.0
|
|
};
|
|
|
|
println!("\n=== Performance Summary ===");
|
|
println!("Audio duration : {:.3} seconds", audio_duration);
|
|
println!("Recognizer creation time: {:.3} seconds", creation_elapsed);
|
|
println!("Recognition time : {:.3} seconds", recognition_elapsed);
|
|
println!("Total elapsed time : {:.3} seconds", total_time);
|
|
println!(
|
|
"Real-Time Factor (RTF): {:.3} (recognition_elapsed / audio_duration = {:.3} / {:.3})",
|
|
rtf, recognition_elapsed, audio_duration
|
|
);
|
|
println!(
|
|
"Number of threads : {}",
|
|
recognizer_config.model_config.num_threads
|
|
);
|
|
} else {
|
|
eprintln!("Failed to get recognition result");
|
|
process::exit(1);
|
|
}
|
|
} |