0

The following is my code (I made some slight changes to the original example code):

import io
import os

# 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(__file__),
    'C:\\Users\\louie\\Desktop',
    'TOEFL2.mp3')

# 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-US')

# Detects speech in the audio file
response = client.recognize(config, audio)

for result in response.results:
    print('Transcript: {}'.format(result.alternatives[0].transcript))
    text_file = open("C:\\Users\\louie\\Desktop\\Output.txt", "w")
    text_file.write('Transcript: {}'.format(result.alternatives[0].transcript))
    text_file.close()

I can only directly run this code in my windows prompt command since otherwise, the system cannot know the GOOGLE_APPLICATION_CREDENTIALS. However, when I run the code, nothing happened. I followed all the steps and I could see the request traffic changed on my console. But I cannot see any transcript. Could someone help me out?

Louie Lee
  • 231
  • 1
  • 11
  • You are joining too many path components. Print `file_name` to check the result. – Klaus D. Oct 16 '18 at 04:34
  • @KlausD. I deleted os.path.dirname(__file__) but I still have no returned result. I only saw a window for like several sec and then it disappeared and then nothing happened. – Louie Lee Oct 16 '18 at 04:51
  • Possible duplicate of [Google Speech Recognition API Result is Empty](https://stackoverflow.com/questions/38906527/google-speech-recognition-api-result-is-empty) – Nikolay Shmyrev Oct 16 '18 at 23:27
  • @NikolayShmyrev I saw that post also has the issue of file formats. However, I cannot get more helpful info from that since I don't know C. – Louie Lee Oct 17 '18 at 02:35

1 Answers1

0

You are trying to decode TOEFL2.mp3 file encoded as MP3 while you specify LINEAR audio encoding with

encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16

You have to convert mp3 to wav first, see information about AudioEncoding

Nikolay Shmyrev
  • 23,769
  • 4
  • 37
  • 84