1

私は音声認識プロジェクトに取り組んでいます。Google音声認識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)

次のエラーが表示されます: Assertion Error - Device index out of range (0 devices available; device index should be between 0 and -1 inclusive).

# 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 アプリから音声を検出する必要があります。この問題を解決するにはどうすればよいですか?

4

1 に答える 1