Search code examples
pythongoogle-apispeech-recognitiongoogle-cloud-speech

Google speech to text API result is empty


I am using Cloud speech to text api to convert audio file to text file. I am executing it using python, Below is code.

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))

When i execute the sample/tested audio file in the name "audio.raw", the audio is converting and result is like below.

runfile('C:/Users/sandesh.p/CallADoc/GoogleSpeechtoText.py', wdir='C:/Users/sandesh.p/CallADoc')
Transcript: how old is the Brooklyn Bridge

But for same code, i am recording a audio and try to convert, it is giving empty result like below:

runfile('C:/Users/sandesh.p/CallADoc/GoogleSpeechtoText.py', wdir='C:/Users/sandesh.p/CallADoc')

I am trying to fix this from past 2 days and please help me to resolve this.


Solution

  • Try following the troubleshooting steps to have your audio with the appropriate settings.

    For instance, your audio file will have the following settings, which are required to have better results:

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