我正在做一个语音识别项目。我正在使用谷歌语音识别 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 应用程序的声音。我可以做些什么来解决这个问题?