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")
Related
I'm creating a voice assistant with a graphical interface, I use speech_recognition to capture audio and recognize it into text, but if I use my script with the .py extension it works, instead if I use .pyw it doesn't work
I have searched a lot but can't find an answer to this problem
I don't get any errors if I use .py, so I don't understand why it doesn't work
import speech_recognition as sr
from speech_recognition import Microphone
device_index = 1
sample_rate = 48000
chunk_size = 1024
r = sr.Recognizer()
while True:
with Microphone(device_index=device_index, sample_rate=sample_rate, chunk_size=chunk_size) as source:
r.adjust_for_ambient_noise(source, duration=0.7)
r.pause_threshold = 400
try:
audio = r.listen(source,timeout=None,phrase_time_limit=5)
Input = str(r.recognize_google(audio,language="it-IT",pfilter=0,show_all=False,with_confidence=False)).lower()
with open("Inputs.txt", 'a') as fp:
fp.write(Input)
except Exception as e:
r = sr.Recognizer()
audio = r.listen(source,timeout=None,phrase_time_limit=5)
data = r.recognize_google(audio,language="it-IT",pfilter=0,show_all=True,with_confidence=True)
I found a solution to the problem, i dont know why but if show_all = False, it works only with .py, instead if show_all = True it also works with .pyw
Input = (data['alternative'][0]['transcript']).lower()
confidence = (data['alternative'][0]['confidence'])
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)
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?
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
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..')