add transcription structs
This commit is contained in:
parent
2920b61828
commit
7ec77f7fac
@ -10,10 +10,12 @@ build = "build.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
cblas = "0.2.0"
|
cblas = "0.2.0"
|
||||||
openblas-src = { version = "0.9.0", features = ["cache", "static"] }
|
openblas-src = { version = "0.9.0", features = ["cache", "static"] }
|
||||||
|
serde = {version = "1.0", features = ["derive"]}
|
||||||
|
serde_json = "1.0"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
bindgen = "0.54.1"
|
bindgen = "0.54"
|
||||||
cc = { version = "1.0.58", features = ["parallel"] }
|
cc = { version = "1.0", features = ["parallel"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
audrey = "0.2.0"
|
audrey = "0.2"
|
||||||
|
@ -40,13 +40,13 @@ pub fn main() {
|
|||||||
println!("feed {}", buff.len());
|
println!("feed {}", buff.len());
|
||||||
|
|
||||||
if model.feed(&mut sess, buff.as_slice()) {
|
if model.feed(&mut sess, buff.as_slice()) {
|
||||||
println!("{}", model.get_result(&mut sess));
|
println!("{:?}", model.get_result(&mut sess));
|
||||||
} else {
|
} else {
|
||||||
println!("{}", model.get_partial_result(&mut sess));
|
println!("{:?}", model.get_partial_result(&mut sess));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{}", model.get_final_result(&mut sess));
|
println!("{:?}", model.get_final_result(&mut sess));
|
||||||
|
|
||||||
// let audio_buf :Vec<_> = if desc.sample_rate() == SAMPLE_RATE {
|
// let audio_buf :Vec<_> = if desc.sample_rate() == SAMPLE_RATE {
|
||||||
// .map(|s| s.unwrap()).collect()
|
// .map(|s| s.unwrap()).collect()
|
||||||
|
@ -17,7 +17,9 @@ mod ffi {
|
|||||||
mod model;
|
mod model;
|
||||||
mod session;
|
mod session;
|
||||||
mod speaker;
|
mod speaker;
|
||||||
|
mod transcription;
|
||||||
|
|
||||||
pub use model::VoskModel;
|
pub use model::VoskModel;
|
||||||
pub use session::{VoskSession, VoskSessionConfig, VoskSessionConfigBuilder};
|
pub use session::{VoskSession, VoskSessionConfig, VoskSessionConfigBuilder};
|
||||||
pub use speaker::SpeakerModel;
|
pub use speaker::SpeakerModel;
|
||||||
|
pub use transcription::{TranscriptionResult, TranscriptionPartialResult, TranscriptionWord};
|
12
src/model.rs
12
src/model.rs
@ -28,24 +28,24 @@ impl VoskModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_result(&self, sess: &mut VoskSession) -> String {
|
pub fn get_result(&self, sess: &mut VoskSession) -> crate::TranscriptionResult {
|
||||||
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_result(sess.inner)) };
|
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_result(sess.inner)) };
|
||||||
|
|
||||||
cstr.to_string_lossy().to_owned().to_string()
|
serde_json::from_str(cstr.to_str().unwrap()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_partial_result(&self, sess: &mut VoskSession) -> String {
|
pub fn get_partial_result(&self, sess: &mut VoskSession) -> crate::TranscriptionPartialResult {
|
||||||
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_partial_result(sess.inner)) };
|
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_partial_result(sess.inner)) };
|
||||||
|
|
||||||
cstr.to_string_lossy().to_owned().to_string()
|
serde_json::from_str(cstr.to_str().unwrap()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_final_result(&self, sess: &mut VoskSession) -> String {
|
pub fn get_final_result(&self, sess: &mut VoskSession) -> crate::TranscriptionResult {
|
||||||
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_final_result(sess.inner)) };
|
let cstr = unsafe { CStr::from_ptr(ffi::vosk_recognizer_final_result(sess.inner)) };
|
||||||
|
|
||||||
cstr.to_string_lossy().to_owned().to_string()
|
serde_json::from_str(cstr.to_str().unwrap()).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
21
src/transcription.rs
Normal file
21
src/transcription.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct TranscriptionWord {
|
||||||
|
pub conf: f32,
|
||||||
|
pub end: f32,
|
||||||
|
pub start: f32,
|
||||||
|
pub word: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct TranscriptionResult {
|
||||||
|
pub text: String,
|
||||||
|
#[serde(default = "Vec::new")]
|
||||||
|
pub result: Vec<TranscriptionWord>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct TranscriptionPartialResult {
|
||||||
|
pub partial: String,
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user