-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvosk_recognizer.py
43 lines (35 loc) · 1.13 KB
/
vosk_recognizer.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
34
35
36
37
38
39
40
41
42
43
'''
Implements Vosk speech recognition
'''
import json
import vosk
import pyaudio
import numpy as np
class SpeechRecognize:
def __init__(self):
with open('vosk_config.json', 'r') as FP:
self.config = json.load(FP)
vosk.SetLogLevel(-1)
model = vosk.Model(self.config['model'])
self.recognizer = vosk.KaldiRecognizer(model, 16000)
def speech_to_text(self):
print('\rListening... ', end='')
mic = pyaudio.PyAudio()
stream = mic.open(format=pyaudio.paInt16,
channels = self.config['channels'],
rate=self.config['rate'],
input=True,
frames_per_buffer=self.config['chunk'] * 2)
stream.start_stream()
while True:
data = stream.read(self.config['chunk'])
if self.recognizer.AcceptWaveform(data):
result = json.loads(self.recognizer.Result())
break
return result['text']
def test():
sr = SpeechRecognize()
text = sr.speech_to_text()
print(text)
if __name__ == '__main__':
test()