Views
No views yet
1import numpy as np
2import onnxruntime as ort
3from transformers import WhisperFeatureExtractor
4import librosa
5
6# Load model
7session = ort.InferenceSession("model.onnx")
8feature_extractor = WhisperFeatureExtractor.from_pretrained("openai/whisper-base")
9
10# Load and preprocess audio
11audio, sr = librosa.load("audio.wav", sr=16000)
12audio_chunk = audio[:480000] # 30 seconds
13
14# Extract features
15inputs = feature_extractor(
16 audio_chunk,
17 sampling_rate=16000,
18 return_tensors="np"
19)
20
21# Run inference
22outputs = session.run(None, {session.get_inputs()[0].name: inputs.input_features})
23predictions = outputs[0] # Shape: [1, 1500] - 1500 frames of 20ms each
24
25# Apply threshold
26speech_frames = predictions[0] > 0.5inference.py script with advanced features:1from inference import WhisperVADInference
2
3# Initialize model
4vad = WhisperVADInference(
5 model_path="model.onnx",
6 threshold=0.5, # Speech detection threshold
7 min_speech_duration=0.25, # Minimum speech segment duration
8 min_silence_duration=0.1 # Minimum silence between segments
9)
10
11# Process audio file
12segments = vad.process_audio("audio.wav")
13
14# Segments format: List of (start_time, end_time) tuples
15for start, end in segments:
16 print(f"Speech detected: {start:.2f}s - {end:.2f}s")1# Process audio stream in chunks
2vad = WhisperVADInference("model.onnx", streaming=True)
3
4for audio_chunk in audio_stream:
5 speech_active = vad.process_chunk(audio_chunk)
6 if speech_active:
7 # Handle speech detection
8 pass[1, 80, 3000] (batch size fixed to 1 - see note below)[1, 1500] (batch size fixed to 1)model.onnx: ONNX model filemodel_metadata.json: Model configuration and parametersinference.py: Ready-to-use inference script with post-processingrequirements.txt: Python dependencies1pip install onnxruntime # or onnxruntime-gpu for GPU support
2pip install librosa transformers numpy1@misc{whisper-vad,
2 title={Whisper-VAD: Whisper-based Voice Activity Detection},
3 author={Grider},
4 year={2025},
5 publisher={Hugging Face},
6 howpublished={\url{https://huggingface.co/TransWithAI/Whisper-Vad-EncDec-ASMR-onnx}}
7}