-
Notifications
You must be signed in to change notification settings - Fork 73
/
record.py
31 lines (23 loc) · 1.04 KB
/
record.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
import time
import speech_recognition as sr
class SpeechRecognizer:
def __init__(self, ambient_duration=2):
self.recognizer = sr.Recognizer()
with sr.Microphone() as source:
self.recognizer.adjust_for_ambient_noise(source, duration=ambient_duration)
def listen(self, model="base.en"):
with sr.Microphone() as source:
audio = self.recognizer.listen(source)
try:
t = time.time()
text = self.recognizer.recognize_whisper(audio, language="english", model=model)
time_parsing = time.time() - t
if time_parsing > 2:
print(f"Recognized in {time.time() - t} seconds - maybe try using a small model. "
f"See https://github.com/openai/whisper#available-models-and-languages.")
return text
except sr.UnknownValueError:
print("Whisper could not understand audio")
except sr.RequestError as e:
print("Could not request results from Whisper")