-
Notifications
You must be signed in to change notification settings - Fork 16
Microphone testing for noisy environments #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a267ee9
fix: incorrect remapping keys for handover
m-barker 6cf7ee8
fix: remove speech server in launch file
m-barker 1500302
Merge branch 'LASR-at-Home:main' into main
m-barker 73addfa
Merge branch 'LASR-at-Home:main' into main
m-barker 7740d2f
Merge branch 'LASR-at-Home:main' into main
m-barker 878372f
Merge branch 'LASR-at-Home:main' into main
m-barker 7aeb018
Merge branch 'LASR-at-Home:main' into main
m-barker 17836ef
Merge branch 'LASR-at-Home:main' into main
m-barker dc88b27
Merge branch 'LASR-at-Home:main' into main
m-barker 6dfc63b
Merge branch 'LASR-at-Home:main' into main
m-barker a743e8b
Merge branch 'LASR-at-Home:main' into main
m-barker 0edf032
Merge branch 'LASR-at-Home:main' into main
m-barker 8b0f298
feat: add script to test speech energy thresholds
m-barker 337df30
Update common/speech/lasr_speech_recognition_whisper/scripts/micropho…
m-barker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
63 changes: 63 additions & 0 deletions
63
common/speech/lasr_speech_recognition_whisper/scripts/microphone_tuning_test.py
This file contains hidden or 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,63 @@ | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.