【问题标题】:Assertion Error: Device index out of range (0 devices available; device index should be between 0 and -1 inclusive)断言错误:设备索引超出范围(0 个设备可用;设备索引应介于 0 和 -1 之间)
【发布时间】:2020-12-12 17:48:10
【问题描述】:

我正在做一个语音识别项目。我正在使用谷歌语音识别 api。我已经使用 dockerfile 在 GCP flex 环境中部署了 django 项目。

Dockerfile:

FROM gcr.io/google-appengine/python

RUN apt-get update
RUN apt-get install libasound-dev portaudio19-dev libportaudio2 libportaudiocpp0 -y
RUN apt-get install python3-pyaudio
RUN virtualenv -p python3.7 /env

ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt

ADD . /app

CMD gunicorn -b :$PORT main:app

app.yaml 文件:

runtime: custom
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 3

获取语音输入的代码。

import speech_recognition as sr
r = sr.Recognizer()

with sr.Microphone(device_index=0) as source:
        print("speak")
        audio = r.listen(source)
        try:
            voice_data =" " + r.recognize_google(audio)

我收到错误:断言错误 - 设备索引超出范围(0 个设备可用;设备索引应介于 0 和 -1 之间)。

# set up PyAudio
        self.pyaudio_module = self.get_pyaudio()
        audio = self.pyaudio_module.PyAudio()
        try:
            count = audio.get_device_count()  # obtain device count
            if device_index is not None:  # ensure device index is in range
                assert 0 <= device_index < count, "Device index out of range ({} devices available; device index should be between 0 and {} inclusive)".format(count, count - 1) …
            if sample_rate is None:  # automatically set the sample rate to the hardware's default sample rate if not specified
                device_info = audio.get_device_info_by_index(device_index) if device_index is not None else audio.get_default_input_device_info()
                assert isinstance(device_info.get("defaultSampleRate"), (float, int)) and device_info["defaultSampleRate"] > 0, "Invalid device info returned from PyAudio: {}".format(device_info)
                sample_rate = int(device_info["defaultSampleRate"])
        except Exception:
            audio.terminate()

当我转到 url 时,它无法检测到音频设备。我需要检测来自托管 web 应用程序的声音。我该怎么做才能解决这个问题?

【问题讨论】:

    标签: google-cloud-platform dockerfile pyaudio google-speech-api audio-device


    【解决方案1】:

    似乎出现错误是因为 AppEngine 的 VM 实例中没有声卡。即使安装了声卡/驱动程序,我想知道如何将麦克风设备连接到实例。

    此问题标有标签google-speech-api,但您共享的代码中未使用Speech API Client Libraries。相反,它使用 python 包SpeechRecognition。假设你要使用 Speech API Client Libraries,你需要使用streaming_recognize(),恐怕你需要更改从网络用户麦克风而不是本地设备麦克风获取语音输入的代码。

    this link 中,我们可以找到一个从文件流式传输的示例,请注意流式识别将即时转换语音数据,并且不会像在其他方法中那样等待操作完成.我不是 python 专家,但在这个例子中,你需要更改这一行以从其他来源(来自网络用户的麦克风)读取:

    with io.open('./hello.wav', 'rb') as stream:
    

    您需要在网络应用程序中执行以下操作 (audio: true) 才能从用户的麦克风中读取信息,请参阅 this link 以获取更多参考:

    navigator.mediaDevices.getUserMedia({ audio: true, video: false })
          .then(handleSuccess);
      
    

    使用这种方法的完整示例是Google Cloud Speech Node with Socket Playground 指南。您可能希望重用一些 NodeJS 代码以将其连接到您当前的 python 应用程序。顺便说一句,NodeJS is also available in AppEngine Flex

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-19
      • 2012-09-20
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 2016-12-16
      • 1970-01-01
      相关资源
      最近更新 更多