[Python] "Microphone access error for real-time voice recognition in Google Colab...

Discussão em 'Python' iniciado por Stack, Janeiro 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to implement real-time voice recognition using Google Colab. However, I am encountering an issue where Colab cannot access the microphone, resulting in the following error:

    PortAudioError: Error querying device -1

    From my understanding, Colab might not support direct microphone access. I want to process live audio input rather than recording a file and processing it later.

    Is there a workaround to enable microphone access in Colab, or is there an alternative approach to achieve real-time speech recognition in a cloud-based notebook environment?

    Below is my current code:

    import sounddevice as sd
    import queue
    import json
    from vosk import KaldiRecognizer

    q = queue.Queue()


    def callback(indata, frames, time, status):
    if status:
    print(status, flush=True)
    q.put(bytes(indata))

    def recognize_speech():
    device_info = sd.query_devices(None, "input")
    samplerate = int(device_info["default_samplerate"])

    rec = KaldiRecognizer(model, samplerate)
    rec.SetWords(True)

    with sd.RawInputStream(samplerate=samplerate, blocksize=8000, dtype="int16",
    channels=1, callback=callback):
    print(" ")
    while True:
    data = q.get()
    if rec.AcceptWaveform(data):
    result = json.loads(rec.Result())
    print(result["text"])


    recognize_speech()


    The error

    PortAudioError Traceback (most recent call last)
    <ipython-input-10-83e5d8e96a1a> in <cell line: 0>()
    31
    32
    ---> 33 recognize_speech()

    1 frames
    /usr/local/lib/python3.11/dist-packages/sounddevice.py in query_devices(device, kind)
    570 info = _lib.Pa_GetDeviceInfo(device)
    571 if not info:
    --> 572 raise PortAudioError(f'Error querying device {device}')
    573 assert info.structVersion == 2
    574 name_bytes = _ffi.string(info.name)

    PortAudioError: Error querying device -1

    Continue reading...

Compartilhe esta Página