断言错误:设备索引超出范围0个可用设备;设备索引应介于0和-1之间包括0和-1

问题描述

我正在研究语音识别项目。我正在使用Google Speechrecognition API。我已经使用dockerfile在django项目上部署了GCP flex环境。

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之间(包括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时无法检测到音频设备。我需要检测来自托管Webapp的声音。我该怎么办才能解决此问题?

解决方法

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

此问题标有标签#include <stdio.h> #include <stdlib.h> typedef struct node { double data; struct node * next; }NODE; NODE * add(NODE * head,double data) { NODE * new = NULL; new = malloc(sizeof(NODE)); new->next = NULL; new->data = data; if( head == NULL) { head = new; } else { NODE * temp = head; while( temp->next) { temp = temp->next; } temp->next = new; } return head; } NODE ** mat2arr ( int n,int m,double **matrix) { NODE ** array_list = NULL; array_list = calloc(n,sizeof(NODE *)); for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { array_list[i] = add(array_list[i],matrix[i][j]); } } return array_list; } int main ( void) { double **myArray = NULL; NODE ** array_list = NULL; myArray = calloc ( sizeof *myArray,5); for ( int i = 0; i < 5; ++i) { myArray[i] = calloc ( sizeof **myArray,3); for ( int j = 0; j < 3; ++j) { myArray[i][j] = ( i + 1) * ( j + 1); } } array_list = mat2arr( 5,3,myArray); for(int i = 0; i < 5; i++) { NODE * each = array_list[i]; while ( each) { printf ( "data %f\n",each->data); each = each->next; } printf ( "\n"); } for(int i = 0; i < 5; i++) { NODE * each = array_list[i]; while ( each) { NODE *temp = each; each = each->next; free ( temp); } free ( myArray[i]); } free ( array_list); free ( myArray); return 0; } ,但您共享的代码中未使用Speech API Client Libraries。而是使用python软件包SpeechRecognition。假设您要使用语音API客户端库,则需要使用google-speech-api,而且恐怕您需要更改代码以从Web用户的麦克风而不是本地设备的麦克风获取语音输入。 / p>

this link中,我们可以找到一个从文件流传输的示例,请注意,流识别将即时转换语音数据,而不会像其他方法。我不是python专家,但是从此示例中,您需要更改此行以从其他来源(从网络用户的麦克风)读取:

streaming_recognize()

您需要在网络应用中执行以下操作(with io.open('./hello.wav','rb') as stream: ),才能从用户的麦克风中读取内容,请参阅this link以获取更多参考信息:

audio: true

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...