2

How can I convert sound from website to a text? When I click the button in a website is play a sound but my problem is how can I convert it to a text without using microphone just the website and the python.

import speech_recognition as sr

r = sr.Recognizer()
with sr.AudioFile('my.wav') as source:

    audio_text = r.listen(source)

    try:
        text = r.recognize_google(audio_text)
        print('Converting audio transcripts into text ...')
        print(text)

    except:
         print('Sorry.. run again...')

Here is my code but I don't have a wav file just the voice coming from the website what I trying to convert.

Example of what I trying to make

when I click the button in the website it plays hello and the python will get the sound from the website and print it.

Jack Cruz
  • 55
  • 8

1 Answers1

1

Try downloading the file first, I don't know the location or format of your audio file so this is a guess:

EDIT: added a url to a real audio file and it works, it fails with poor quality audio though

import requests
import speech_recognition as sr

def download(url, path):
    response = requests.get(url)     # get the response of the url
    with open(path, 'wb') as file:   # create the file
        file.write(response.content) # write response contents to the file

def transcribe(path):
    r = sr.Recognizer()
    with sr.AudioFile(path) as source:
        audio_text = r.record(source)

        text = r.recognize_google(audio_text)
        print('Converting audio transcripts into text ...')
        return text


audio_url = 'https://google.github.io/tacotron/publications/parrotron/audio/norm_vctk/03_norm_input.wav'
audio_path = './speech.wav'

download(audio_url, audio_path)

audio_text = transcribe(audio_path)

print(audio_text)

Output

Converting audio transcripts into text ...
this is a huge confidence boost
bherbruck
  • 2,167
  • 1
  • 6
  • 17