-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstt.py
33 lines (25 loc) · 810 Bytes
/
stt.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
32
33
import json
import pyaudio
from params import session
import vosk
import os
model = vosk.Model(os.path.join(os.path.dirname(__file__), session.VOSK_MODEL))
rec = vosk.KaldiRecognizer(model, 16000)
p = pyaudio.PyAudio()
try:
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8000)
stream.start_stream()
except:
pass
class SpeechToText:
MICROPHONE = False
def listen(self):
while self.MICROPHONE:
data = stream.read(4000, exception_on_overflow=False)
if rec.AcceptWaveform(data) and len(data) > 0:
anwser = json.loads(rec.Result())
if anwser['text']:
yield anwser['text']
def status(self, status):
self.MICROPHONE = status
stt = SpeechToText()