Google语音文本API结果为空

时间:2018-11-13 05:09:57

标签: python google-api speech-recognition google-cloud-speech

我正在使用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天,我正在尝试解决此问题,请帮助我解决该问题。

1 个答案:

答案 0 :(得分:1)

尝试按照troubleshooting steps进行音频设置和适当的设置。

例如,您的音频文件将具有以下设置,需要这些设置才能获得更好的效果:

Encoding: FLAC
Channels: 1 @ 16-bit
Sampleratehertz: 16000Hz