#🔒 VOSK library is not recognizing when I say something

21 messages · Page 1 of 1 (latest)

fluid ice
#

The idea of ​​this code is: when you say one of the keywords (the huge list of if) something happens in Minecraft Java Edition. I modified the code using VOSK-PT-BR so that I could speak the words in Portuguese, since in the original code, if I didn't speak the words in English, the program wouldn't recognize anything, so nothing would happen in Minecraft. The problem is that: in the initial code, the original, when I started the script I could send: "python mcrecog.py -m" in cmd and a list would appear with all the microphones that were connected to my PC (I could choose to change if the program hadn't recognized my audio). In this modified version that I made, when I start the game, my microphone is not recognized. Even if I use "python mcrecog.py -m" in cmd, a small list of 3 options appears for me to change the microphone and none of them work. The microphone continues to be unrecognized and not a single word is captured by the program. I would like to know how to fix it, or what could be causing this.

Below is the original code (first code) and the modified code (second code)

distant veldtBOT
#

@fluid ice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fluid ice
#

first code:

import speech_recognition

import mc_socket
from mc_socket import MCSocket
import speech_recognition as sr
import argparse




"""
https://github.com/ketan-ryan/MCRecog/wiki
"""
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--Microphone", help="Manually select microphone", action='store_true')
arguments = parser.parse_args()

r = sr.Recognizer()
r.energy_threshold = 300

mic_idx = None
if arguments.Microphone:
    for index, name in enumerate(sr.Microphone.list_microphone_names()):
        print(f"Microphone with name {name} found for `Microphone(device_index={index})`")

    mic_idx = int(input("Please input the device index of your primary microphone: "))

mic = sr.Microphone(mic_idx)
mc = MCSocket(7777)
print("Ready to begin speech recognition!")


def get_response(response):
    res = response
    response = str(resp).replace(" ", "").lower()
    print(response)

    ret = []
    if "crow" in response:
        ret.append("Explode and die")
    if "noshot" in response:
        ret.append("Lose 10 arrows")
    if "bear" in response:
        ret.append("Spawn 7 hostile polar bears")
    if "axolotl" in response:
        ret.append("Axolotl time")
    if "rot" in response:
        ret.append("Spawn 10 zombies")
    if "bone" in response:
        ret.append("Spawn 10 skeletons")
    if "pig" in response:
        ret.append("Lose all hunger")
    if "sub" in response:
        ret.append("Lose something random")
    if "creep" in response:
        ret.append("Spawn 7 creepers")
    if "rod" in response:
        ret.append("Spawn 7 blazes")
    if "end" in response:
        ret.append("Spawn 10 angry endermen")
    if "nether" in response:
        ret.append("Spawn 7 wither skeletons")
    if "cave" in response:
        ret.append("Mining fatigue")
    if 'follow' in response:
        ret.append("Big hole")
    if 'day' in response:
        ret.append("Set time to night")
#
    if 'bed' in response:
        ret.append("Spawn 7 phantoms")
    if 'boat' in response:
        ret.append("Fill inventory with boats")
    if 'dragon' in response:
        ret.append("Play dragon noise, spawn 10 endermite")
    if 'twitch' in response:
        ret.append("Spawn supercharged creeper")
    if 'coal' in response:
        ret.append("Set on fire")
    if 'iron' in response:
        ret.append("Spawn aggro iron golem")
    if 'gold' in response:
        ret.append("Spawn pigmen")
    if 'diamond' in response:
        ret.append("Set to half a heart")
    if 'mod' in response:
        ret.append("Shuffle inventory")
    if 'port' in response:
        ret.append("Teleport randomly")
    if 'water' in response:
        ret.append("In water")
    if 'block' in response:
        ret.append("Spawn killer rabbits")
    if 'high' in response:
        ret.append("Launched in the air")
    if 'craft' in response:
        ret.append("Surround in obsidian")
    if 'village' in response:
        ret.append("Spawn witches")
    if 'mine' in response:
        ret.append("Give something useless")
    if 'gam' in response:
        ret.append("Random explosion")
    if 'light' in response:
        ret.append("Lightning")
    if 'ink' in response:
        ret.append("Ink Splat")
    if 'bud' in response:
        ret.append("Knockback")
    if 'yike' in response:
        ret.append("Lava source block")
    if 'poggers' in response:
        ret.append("Heal 1 heart")
    if 'blessmepapi' in response:
        ret.append("No effects for 20 seconds")
    if 'dream' in response:
        ret.append("Instant death")
    if 'thing' in response:
        ret.append("Give iron nugget")
    if 'godlike' in response:
        ret.append("Strength effect")
    if 'troll' in response:
        ret.append("Drop inventory")
#
print(response, ret)

    ret.append(res)
    return ret


while 1:
    try:
        with mic as src:
            r.adjust_for_ambient_noise(src)
            audio = r.listen(src)
            resp = r.recognize_google(audio)
            cmd = get_response(resp)

            mc.stream(cmd)

    except speech_recognition.UnknownValueError:
        pass
#

now the code that i modify using VOSK-PT-BT:

import vosk
import mc_socket
from mc_socket import MCSocket
import argparse
import pyaudio

parser = argparse.ArgumentParser()
parser.add_argument("-m", "--Microphone", help="Selecione manualmente o microfone", action='store_true')
arguments = parser.parse_args()

modelo = vosk.Model(r"C:\models\vosk-model-pt-fb-v0.1.1-pruned")
reconhecedor = vosk.KaldiRecognizer(modelo, 16000)

p = pyaudio.PyAudio()

idx_microfone = None
if arguments.Microphone:
    p = pyaudio.PyAudio()
    info = p.get_host_api_info_by_index(0)
    numdevices = info.get('deviceCount')
    for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name'))
    idx_microfone = int(input("Por favor, insira o indice do dispositivo do microfone principal: "))
    stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8192, input_device_index=idx_microfone)
else:
    stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8192)


mc = MCSocket(7777)
print("Pronto para começar o reconhecimento de fala!")

def get_response(resposta):
    resposta = resposta.lower()
    print(resposta)

    ret = []
    if "corvo" in resposta:  # crow
        ret.append("Explodir e morrer")
    if "noshot" in resposta:
        ret.append("Perder 10 flechas")
    #... adicione mais palavras em português e respostas aqui...
    return ret
#
while 1:
    try:
        data = stream.read(4000)
        if len(data) == 0:
            if reconhecedor.AcceptWaveform(data):
                resultado = reconhecedor.Result()
                if resultado:
                    resp = resultado.text
                    cmd =  get_response(resp)
                    mc.stream(cmd)
    except vosk.Error as e:
        print(f'Erro: {e}')
    except Exception as e:
        print(f'Erro desconhecido: {e}')


stream.stop_stram()
stream.close()
p.terminate()
cyan lotus
#

Did you grant python permission to use the microphone?

frank fulcrum
#

!paste

distant veldtBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

fluid ice
cyan lotus
#

Any reason you aren't using SR with vosk?

fluid ice
#

Speech recognition does not support Brazilian Portuguese

cyan lotus
#

SR supports different backends including vosk

fluid ice
#

So could I use SR with Vosk for voice recognition in Portuguese?

cyan lotus
#

With recognize_vosk instead of recognize_google, and the models moved into the project folder

fluid ice
#

"models" that you say would be the PT BR model that I downloaded, right?

cyan lotus
#

Yea, there's some here too https://alphacephei.com/vosk/models if they're not the same

fluid ice
#

hmmmm thanks! i will try use SR ^^ thxxxxx

distant veldtBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.