python - Google 语音转文本 API 结果为空
问题描述
我正在使用 Cloud Speech to text api 将音频文件转换为文本文件。我正在使用 python 执行它,下面是代码。
import io
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="D:\\Sentiment_Analysis\\My Project 59503-717155d6fb4a.json"
# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
file_name = os.path.join(os.path.dirname('D:\CallADoc_VoiceImplementation\audioclip154173607416598.amr'),'CallADoc_VoiceImplementation','audioclip154173607416598.amr')
# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file: content = audio_file.read()
audio = types.RecognitionAudio(content=content)
config = types.RecognitionConfig(encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,sample_rate_hertz=16000,language_code='en-IN')
# Detects speech in the audio file
response = client.recognize(config, audio)
for result in response.results: print('Transcript: {}'.format(result.alternatives[0].transcript))
当我执行名为“audio.raw”的样本/测试音频文件时,音频正在转换,结果如下所示。
runfile('C:/Users/sandesh.p/CallADoc/GoogleSpeechtoText.py', wdir='C:/Users/sandesh.p/CallADoc')
Transcript: how old is the Brooklyn Bridge
但是对于相同的代码,我正在录制音频并尝试转换,它给出了如下所示的空结果:
runfile('C:/Users/sandesh.p/CallADoc/GoogleSpeechtoText.py', wdir='C:/Users/sandesh.p/CallADoc')
我正在尝试从过去 2 天开始解决此问题,请帮助我解决此问题。
解决方案
尝试按照故障排除步骤使您的音频具有适当的设置。
例如,您的音频文件将具有以下设置,这些设置需要获得更好的结果:
Encoding: FLAC
Channels: 1 @ 16-bit
Sampleratehertz: 16000Hz
推荐阅读
- node.js - 在 Heroku 上启动 node.js 服务器时传递参数很热?
- android - 在 Android Studio 中切换 Activity 后表格移动
- c++ - 将参数推送到调用堆栈的便携式方法 (C++)
- animation - 补间最大值和动画下拉菜单
- javascript - 将图像与 webpack react 一起使用是行不通的
- ruby-on-rails - 回调在 Rails 5 中表现为事务
- r - as.graphicsAnnot(legend) 中的错误:缺少参数“legend”,没有默认值
- c - CS50 Pset 5 拼写器 Seg 错误
- python - pipenv:仅适用于已安装的文件夹?
- php - 从连接的时间戳变量中分离日期和时间 - PHP