-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeechRecognition.py
47 lines (40 loc) · 1.26 KB
/
SpeechRecognition.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
44
45
46
47
# sudo apt-get install pip
# sudo apt-get install -y python3-pyaudio
# sudo pip3 install vosk
import os
import sys
import json
import contextlib
import pyaudio
import io
from vosk import Model, KaldiRecognizer
# Path to the Vosk model
#model_path = "models/vosk-model-small-pl-0.22/"
model_path = "models/vosk-model-small-en-us-0.15/"
if not os.path.exists(model_path):
print(f"Model '{model_path}' was not found. Please check the path.")
exit(1)
model = Model(model_path)
# Settings for PyAudio
sample_rate = 16000
chunk_size = 8192
format = pyaudio.paInt16
channels = 1
# Initialization of PyAudio and speech recognition
p = pyaudio.PyAudio()
stream = p.open(format=format, channels=channels, rate=sample_rate, input=True, frames_per_buffer=chunk_size)
recognizer = KaldiRecognizer(model, sample_rate)
os.system('clear')
print("\nSpeak now...")
while True:
data = stream.read(chunk_size)
if recognizer.AcceptWaveform(data):
result_json = json.loads(recognizer.Result())
text = result_json.get('text', '')
if text:
print("\r" + text, end='\n')
else:
partial_json = json.loads(recognizer.PartialResult())
partial = partial_json.get('partial', '')
sys.stdout.write('\r' + partial)
sys.stdout.flush()