Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SpeechRecognition vs. Google Cloud Speech API

I am playing with Google Cloud Speech API. I was wondering if I use the python speech recognition library and call the google cloud speech API, is that still a valid way to use the API? I just want to transcribe the text.

I am confused about the difference between them and if there is any suggested way if I just want to transcribe the audio.

Using Python SpeechRecognition:

import speech_recognition as sr
r = sr.Recognizer()
r.recognize_google_cloud()
harvard = sr.AudioFile('harvard.wav')
with harvard as source:
   audio = r.record(source)
r.recognize_google(audio)

Not using Python SpeechRecognition:

from google.cloud import speech_v1 as speech


def speech_to_text(config, audio):
    client = speech.SpeechClient()
    response = client.recognize(config, audio)
    print_sentences(response)


def print_sentences(response):
    for result in response.results:
        best_alternative = result.alternatives[0]
        transcript = best_alternative.transcript
        confidence = best_alternative.confidence
        print('-' * 80)
        print(f'Transcript: {transcript}')
        print(f'Confidence: {confidence:.0%}')


config = {'language_code': 'en-US'}
audio = {'uri': 'gs://cloud-samples-data/speech/brooklyn_bridge.flac'}
like image 422
Nelly Yuki Avatar asked Aug 03 '26 17:08

Nelly Yuki


1 Answers

If you only plan to use Google Cloud Platform for speech recognition, then SpeechClient would be better because that is maintained by Google.

If you want to try out different speech recognition services, speech_recognition would help with that since it is more generic.

Any way of calling the api is fine. The libraries are just to make it easier for you.

like image 76
Brendan Avatar answered Aug 07 '26 11:08

Brendan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!