Skip to content

Instantly share code, notes, and snippets.

@BenHamm
Created April 3, 2023 17:26
Show Gist options
  • Save BenHamm/236c053fdb32be756244b4779c13b6e9 to your computer and use it in GitHub Desktop.
Save BenHamm/236c053fdb32be756244b4779c13b6e9 to your computer and use it in GitHub Desktop.
import speech_recognition as sr
import time
import sys
sys.stdout = open('/dev/null', 'w')
from text_to_speech import text_to_speech
import pygame
sys.stdout = sys.__stdout__
pygame.mixer.init()
def capture_speech():
start_time = time.time()
r = sr.Recognizer()
with sr.Microphone() as source: # use the default microphone as the audio source
pygame.mixer.Sound("open_mic.wav").play()
audio = r.listen(source) # listen for the first phrase and extract it into audio data
end_time = time.time()
print("Recording time: ", end_time - start_time)
# if True:
try:
start_time = time.time()
# text = r.recognize_whisper(audio)
sys.stdout = open('/dev/null', 'w')
text = r.recognize_google(audio)
sys.stdout = sys.__stdout__
end_time = time.time()
print("Recognition time: ", end_time - start_time)
except: # speech is unintelligible
text_to_speech('I did not hear that. Please try again.')
text = capture_speech()
return text
if __name__ == "__main__":
text = capture_speech()
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment