alphacep / vosk-server

WebSocket, gRPC and WebRTC speech recognition server based on Vosk and Kaldi libraries
Apache License 2.0
896 stars 243 forks source link

Example for use within a python program #114

Closed MikeyBeez closed 3 years ago

MikeyBeez commented 3 years ago

The test_microphone.py shows how to get speech to text printed on the screen but not passed back into a variable. I'm sure this is a silly question. Sorry, but I don't know how to call this code and get back a json object with the text. Is this easy? Can an example be added to the repository? Thanks for all your help. Vosk is fantastic. I have it working as an installed module, but for ease of installation, I want to use your docker container. Below is my current code that I understand very well.
I just don't understand how to convert this to using your websocket.

import pyaudio
import json
from vosk import Model, KaldiRecognizer
#, SetLogLevel

#SetLogLevel(-10)

def myCommand():
    # "listens for commands"
    # We imported vosk up above.
    p = pyaudio.PyAudio()
    # MyFormat = pyaudio.paInt16
    # MyChannels = 1
    # MyRate = 16000
    # MyInput = True
    # MyFPB = 8000
    stream = p.open(format=pyaudio.paInt16, channels=1, rate=1
6000, input=True, frames_per_buffer=8000)
    stream.start_stream()
    model = Model("model")
    rec = KaldiRecognizer(model, 16000)
    while True:
        data = stream.read(2000)
        if len(data) == 0:
            break
        if rec.AcceptWaveform(data):
            myResult = rec.Result()
            myList = myResult.split("text")
            command = myList[1]
            stream.stop_stream()
            stream.close()
            p.terminate()
            return command

I appreciate any guidance you can give on this. If it's at all helpful, please feel free to modify and publish this snippet of code for an example. Cheers!

MikeyBeez commented 3 years ago

Nevermind. Duh! It was so easy:

I changed

#print(await ws.recv())

to

myText = (await ws.recv())
 print("My Text " + myText)

I should be able to

return myText