27 lines
766 B
Python
27 lines
766 B
Python
import sounddevice as sd
|
|
import queue
|
|
import sys
|
|
import json
|
|
from vosk import Model, KaldiRecognizer
|
|
model = Model("./vosk-model-small-en-us-0.15")
|
|
|
|
rec = KaldiRecognizer(model, 16000)
|
|
q = queue.Queue()
|
|
|
|
def callback(indata, frames, time, status):
|
|
if status:
|
|
print(status, file=sys.stderr)
|
|
q.put(bytes(indata))
|
|
|
|
with sd.RawInputStream(samplerate=16000, blocksize=8000, dtype='int16',
|
|
channels=1, callback=callback):
|
|
print("Listening... Speak into the mic.")
|
|
while True:
|
|
data = q.get()
|
|
if rec.AcceptWaveform(data):
|
|
result = json.loads(rec.Result())
|
|
print(".", result)
|
|
else:
|
|
partial = json.loads(rec.PartialResult())
|
|
print("...", partial, end='\r')
|