首页 > 解决方案 > 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 天开始解决此问题,请帮助我解决此问题。

标签: pythongoogle-apispeech-recognitiongoogle-cloud-speech

解决方案


尝试按照故障排除步骤使您的音频具有适当的设置。

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

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

推荐阅读