我正在使用 PyPi 代码(https://pypi.org/project/SpeechRecognition/)
但是,执行 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")
# 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 连接时间 ()。
# 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
这意味着问题与音频文件格式或网络速度无关,而是与 Raspberry Pi 设置和 Google Cloud Speech-to-Text API 之间的交互有关。
确保音频参数始终设置为 Google API 的最佳值(例如 16kHz 采样率、Linear16 格式)。
您可以在 Python 脚本中添加一个步骤,以便在将音频发送到 API 之前始终由于问题在不同的网络中仍然存在,问题可能在于 API 请求的构造或处理方式。尝试降低请求的复杂性或将其分解为更小的部分。Raspberry Pi 内可能存在特定配置或限制导致延迟。检查是否有任何可能影响性能的后台进程或资源限制。