31
loading...
This website collects cookies to deliver better user experience
recognize_bing: Microsoft Bing Speech
recognize_google: Google Web Speech API This is the one we will use in this article.
recognize_google_cloud: Google Cloud Speech - requires installation of the google-cloud-speech package
recognize_houndify: Houndify by SoundHound
recognize_ibm: IBM Speech to Text
recognize_sphinx: CMU Sphinx - requires installing PocketSphinx
recognize_wit: Wit.ai
$ pip install SpeechRecognition
$ sudo apt-get install python-pyaudio python3-pyaudio
$ brew install portaudio
$ pip install pyaudio
$ pip install pyaudio
# Importing the libraries that will do the magic part 🐵
import speech_recognition as sr
import webbrowser as wb
def fn_speech_recognition():
sr.Microphone(device_index = 0)
print(f"MICs Found on this Computer: \n {sr.Microphone.list_microphone_names()}")
# Creating a recognition object instance
r = sr.Recognizer()
r.energy_threshold=4000
r.dynamic_energy_threshold = False
with sr.Microphone() as source:
print('Please Speak Loud and Clear:')
#reduce noise
r.adjust_for_ambient_noise(source)
#take voice input from the microphone
audio = r.listen(source)
try:
phrase = r.recognize_google(audio)
print(f"Did you just say: {phrase} ?")
url = "https://www.google.com/search?q="
search_url = url+phrase
wb.open(search_url)
except TimeoutException as msg:
print(msg)
except WaitTimeoutError:
print("listening timed out while waiting for phrase to start")
quit()
# speech is unintelligible
except LookupError:
print("Could not understand what you've requested.")
else:
print("Your results will appear in the default browser. Good bye for now...")
fn_speech_recognition()
# Basic Speech Recognition Demonstration Routine for my medium blog 😊
# Made with ❤️ in Python 3 by Alvison Hunter - November 14th, 2021
# JavaScript, Python and Web Development tips at: https://bit.ly/3p9hpqj
# Importing the libraries that will do the magic part 🐵
import speech_recognition as sr
import webbrowser as wb
def fn_speech_recognition():
sr.Microphone(device_index = 0)
print(f"MICs Found on this Computer: \n {sr.Microphone.list_microphone_names()}")
# Creating a recognition object
r = sr.Recognizer()
r.energy_threshold=4000
r.dynamic_energy_threshold = False
with sr.Microphone() as source:
print('Please Speak Loud and Clear:')
#reduce noise
r.adjust_for_ambient_noise(source)
#take voice input from the microphone
audio = r.listen(source)
try:
phrase = r.recognize_google(audio)
print(f"Did you just say: {phrase} ?")
url = "https://www.google.com/search?q="
search_url = url+phrase
wb.open(search_url)
except TimeoutException as msg:
print(msg)
except WaitTimeoutError:
print("listening timed out while waiting for phrase to start")
quit()
# speech is unintelligible
except LookupError:
print("Could not understand what you've requested.")
else:
print("Your results will appear in the default browser. Good bye for now...")
fn_speech_recognition()