Iâm not sure exactly how youâd go about turning the Anvil Media object into sr.AudioData
but you can get the bytes by doing media_object.get_bytes()
you might also be able to turn it into sr.AudioFile
I get âAssertionError: audio_data must be audio dataâ
Code:
import speech_recognition as sr @anvil.server.callable
def Audio2Text(AudioFile):
with anvil.media.TempFile(AudioFile) as file_name:
audio_file = sr.AudioFile(file_name)
r = sr.Recognizer()
response = r.recognize_google(audio_file)
print(âYou said 'â + response + âââ)
The second example on the speech_recognition home page looks like it would do what you want. In particular, it seems that you need to ârecordâ the contents of the audio file using the Recognizer:
r = sr.Recognizer()
with anvil.media.TempFile(AudioFile) as file_name:
with sr.AudioFile(file_name) as source:
audio = r.record(source)
response = r.recognize_google(audio)
Hopefully that should do roughly what you want! If you still see errors, then @stucork is right - please post a clone link to a minimal example here.