-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Microphone testing for noisy environments (#214)
* fix: incorrect remapping keys for handover * fix: remove speech server in launch file * feat: add script to test speech energy thresholds * Update common/speech/lasr_speech_recognition_whisper/scripts/microphone_tuning_test.py Co-authored-by: Jared Swift <[email protected]> --------- Co-authored-by: Jared Swift <[email protected]>
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
common/speech/lasr_speech_recognition_whisper/scripts/microphone_tuning_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python3 | ||
import argparse | ||
import os | ||
import torch | ||
import numpy as np | ||
from pathlib import Path | ||
import speech_recognition as sr | ||
from lasr_speech_recognition_whisper import load_model # type: ignore | ||
import sounddevice # needed to remove ALSA error messages | ||
from typing import Dict | ||
|
||
|
||
def parse_args() -> Dict: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--device_index", type=int, default=None) | ||
return vars(parser.parse_args()) | ||
|
||
|
||
def configure_whisper_cache() -> None: | ||
"""Configures the whisper cache directory.""" | ||
whisper_cache = os.path.join(str(Path.home()), ".cache", "whisper") | ||
os.makedirs(whisper_cache, exist_ok=True) | ||
# Environemntal variable required to run whisper locally | ||
os.environ["TIKTOKEN_CACHE_DIR"] = whisper_cache | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
recognizer = sr.Recognizer() | ||
microphone = sr.Microphone(device_index=args["device_index"], sample_rate=16000) | ||
threshold = 100 | ||
recognizer.dynamic_energy_threshold = False | ||
recognizer.energy_threshold = threshold | ||
transcription_model = load_model( | ||
"medium.en", "cuda" if torch.cuda.is_available() else "cpu", True | ||
) | ||
transcription_result = "The quick brown fox jumps over the lazy dog." | ||
while transcription_result != "": | ||
print(f"Listening...") | ||
with microphone as source: | ||
wav_data = recognizer.listen(source).get_wav_data() | ||
print(f"Processing...") | ||
# Magic number 32768.0 is the maximum value of a 16-bit signed integer | ||
float_data = ( | ||
np.frombuffer(wav_data, dtype=np.int16).astype(np.float32, order="C") | ||
/ 32768.0 | ||
) | ||
|
||
# Cast to fp16 if using GPU | ||
transcription_result = transcription_model.transcribe( | ||
float_data, fp16=torch.cuda.is_available() | ||
)["text"] | ||
|
||
print( | ||
f"Transcription: {transcription_result} at energy threshold {recognizer.energy_threshold}" | ||
) | ||
threshold += 100 | ||
recognizer.energy_threshold = threshold | ||
|
||
|
||
if __name__ == "__main__": | ||
configure_whisper_cache() | ||
main() |