在 RPi3B+ 上使用 Google Cloud SpeechRecognition 和 python 3.9 实现 80 秒延迟

问题描述 投票:0回答:1

我正在使用 PyPi 代码(https://pypi.org/project/SpeechRecognition/

  • 已清理以仅使用Google Cloud SpeechRecognition。
  • shell 环境中的 Google Json 凭证,并且正在工作。
我已启用 Cloud Speech-to-Text API,获取了 Json 凭据,并且服务调用正在访问 API。麦克风很好,录音速度很快。

但是,执行 API 调用需要整整 80 秒!

我监控了网络流量,可以看到 API 连接闲置了 78 秒,然后在最后 2 秒内 TX/RX 速度非常快。我怎样才能加快速度?

我可以修复慢速认证握手吗?

谢谢一百万!

延迟发生在倒数第二行:

print("0 seconds") try: print("Google Cloud Speech thinks you said " + r.recognize_google_cloud(audio) print("80th second")
    
python google-cloud-platform raspberry-pi speech
1个回答
0
投票
我假设 Raspberry Pi 具有稳定的网络连接。不过,请检查 DNS 设置是否适合快速解决问题。

# Check current DNS configuration cat /etc/resolv.conf # Change DNS server if necessary (e.g., to Google's 8.8.8.8) echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
您还可以测量与 Google API 的 SSL 连接时间 (

openssl s_client

)。

# Check SSL handshake time openssl s_client -connect speech.googleapis.com:443
如果音频文件很大,请尝试

压缩音频文件,这可能会减少上传时间。 # Example: Compressing audio file before upload (pseudo-code) import audio_compression_library compressed_audio = audio_compression_library.compress(audio)

并切换到
异步识别方法

(如果尚未使用)。 # Example: Asynchronous call to Google Speech-to-Text API (pseudo-code) recognize_future = r.recognize_google_cloud_async(audio) result = recognize_future.result()

确认认证方式高效且不会造成延迟。

分析 Python 脚本

以识别任何瓶颈。 # Install profiling tool pip install line_profiler # Run the profiler kernprof -l -v your_script.py



我使用 PyPi 语音识别代码直接从麦克风获取音频 - 通常为 2 或 3 秒长。我还尝试使用 Google Library 示例传递约 170KB 大小的 Google API 16,000hz WAV 文件 (Linear16)。两者都工作。两者都需要 80 秒。使用 WiFi 在最大距离 + 使用有线以太网到光纤路由器进行测试:每次 80 秒 - 每次!

这意味着问题与音频文件格式或网络速度无关,而是与 Raspberry Pi 设置和 Google Cloud Speech-to-Text API 之间的交互有关。

确保音频参数始终设置为 Google API 的最佳值(例如 16kHz 采样率、Linear16 格式)。

您可以在 Python 脚本中添加一个步骤,以便在将音频发送到 API 之前始终

将音频转换为此格式。
# Example: Converting audio to 16kHz Linear16 format (pseudo-code) import audio_format_converter formatted_audio = audio_format_converter.convert_to_linear16(audio, sample_rate=16000)

由于问题在不同的网络中仍然存在,问题可能在于 API 请求的构造或处理方式。尝试降低请求的复杂性或将其分解为更小的部分。
Raspberry Pi 内可能存在特定配置或限制导致延迟。检查是否有任何可能影响性能的后台进程或资源限制。

© www.soinside.com 2019 - 2024. All rights reserved.