-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSpeechRecognitionRework.py
More file actions
42 lines (35 loc) · 1.13 KB
/
SpeechRecognitionRework.py
File metadata and controls
42 lines (35 loc) · 1.13 KB
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
32
33
34
35
36
37
38
39
40
41
42
import speech_recognition as sr
import threading
import queue
import readline
class SpeechRecognizer:
def __init__(self):
self.r = sr.Recognizer()
self.q = queue.Queue()
self.mic = sr.Microphone()
def start(self):
self.stop_listening = self.r.listen_in_background(self.mic, self.handle_audio)
def stop(self):
if hasattr(self, 'stop_listening') and self.stop_listening is not None:
self.stop_listening(wait_for_stop=False)
def handle_audio(self, recognizer, audio):
try:
text = recognizer.recognize_google(audio,language="tr-tr")
self.q.put(text)
except sr.UnknownValueError:
pass
except sr.RequestError:
pass
if __name__ == '__main__':
recognizer = SpeechRecognizer()
recognizer.start()
while True:
try:
text = recognizer.q.get_nowait()
print(f"Recognized: {text}")
except queue.Empty:
pass
if readline.get_line_buffer():
# A non-empty line was entered, so stop the recognizer
recognizer.stop()
break