I tried to use the following code to recognize a 5 min audio
import speech_recognition as sr
from os import path
from pydub import AudioSegment
import wavio
from scipy.io import wavfile
test_file = open("test.txt", "w+")
sound = AudioSegment.from_mp3("001.mp3")
sound.export("001.wav", format="wav")
AUDIO_FILE = "001.wav"
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print(text)
test_file.write(text+". ")
except:
print('Run again')
But it only return the very first couple words: "listening diagnostic pretest page 143", do you guys know why it didn't recognize the whole audio?
Related
I'm having trouble with this code, i have all the needed requirements downloaded, butt it hust gives me a blinking pointer.
import speech_recognition as sr
r=sr.Recognizer()
print(sr.Microphone.list_microphone_names())
with sr.Microphone() as source:
# r.energy_threshold()
print("say anything : ")
r.adjust_for_ambient_noise(source,duration=2)
audio= r.listen(source)
try:
text = r.recognize_google(audio)
print(text)
except:
print("sorry, could not recognise")
I want to use recognize_google to analyze the number in the wav file
try:
temp = r.recognize_google(".//splitAudio//split.wav",language="zh-TW")
print("You have said \n" + temp )
print("Audio Recorded Successfully \n ")
except Exception as e:
print("Error : " + str(e))
but I got the following error: Error : audio_data must be audio data
I have found this answer for a while, but I don't figure out.
Who can help me,i really appreciate it,thanks.
I do not have time to install stuff and find a WAV file but here's what The Ultimate Guide To Speech Recognition With Python says re "Using record() to Capture Data From a File"
import speech_recognition as sr
r = sr.Recognizer()
harvard = sr.AudioFile('harvard.wav')
with harvard as source:
audio = r.record(source)
r.recognize_google(audio)
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.
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
I am attempting to write to a text file. It just seems to fail every time. I can write .write("test") but writing the google transcription output to file seems to fail.
Any advice would be greatly appreciated.
import speech_recognition as sr
from os import path
from pprint import pprint
audio_file = path.join(path.dirname(path.realpath(__file__)), "RobertP.wav")
r = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = r.record(source)
try:
txt = r.recognize_google(audio, show_all=True)
pprint (txt)
except:
print("Didn't work.")
try:
f = open("tester.txt", "w+")
f.write(txt)
f.close()
except:
print("Couldn't write to file")```
It Looks like txt is not a string. You can try to convert it to a string with str(txt).
btw:
It's better to remove the try/except for testing reasons to get the error. After the program works you can add it again,.
speech to text in python using audio file.
This is answer for this question.
You have install pyaudio and SpeechRecognition.
and audio file format should be in WAV file.
Its code for speech to text (input from audio file).
import speech_recognition as sr
r = sr.Recognizer()
audio = 'trial.wav'
with sr.AudioFile(audio) as source:
audio = r.record(source)
print ('Done!')
try:
text = r.recognize_google(audio)
print (text)
except Exception as e:
print (e)
If you want different languages to be converted. You can use below code.
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile('Audio.wav') as source:
audio = r.listen(source)
try:
text = (r.recognize_google(audio, language="IN_HI"))
print('working on...')
print(text)
except:
print('Sorry.. run again..')