Revolutionary Voice-Controlled Python Code: Speak, Command, and Witness Magic Happen

mdshamsfiroz
3 min readAug 2, 2023

--

Introduction: In this blog post, we will explore a revolutionary Python code that allows us to interact with our computer using just our voice! Gone are the days of typing commands; now, you can simply speak to your machine and witness the magic unfold before your eyes. We will demonstrate how to use this voice-controlled Python code to execute commands and perform tasks with ease.

GitHub Repository: Before we dive into the details, you can find the complete code for this voice-controlled project on my GitHub repository. GitHub Link

Requirements: To run this code, you’ll need to install the following Python module using pip:

pip install SpeechRecognition

Understanding the Code: Let’s take a closer look at the Python code responsible for recognizing our speech:

# Import the required module
import speech_recognition as sr

def recognize_speech():
recognizer = sr.Recognizer()

with sr.Microphone() as source:
print("Listening... Say Something")
audio = recognizer.listen(source)

try:
# Use Google Web Speech API to recognize the audio
recognized_text = recognizer.recognize_google(audio)
print(f"Recognized Text: {recognized_text}")

except sr.UnknownValueError:
print("Sorry, could not understand the audio.")
except sr.RequestError as e:
print(f"Error occurred: {e}")

if __name__ == "__main__":
recognize_speech()

Working with the Code:

  1. Import the speech_recognition module and create an instance of the Recognizer class to recognize our speech.
  2. Utilize a with statement to access the microphone as the audio source.
  3. Listen for the user’s voice input using the listen() method and store the audio data.
  4. Attempt to recognize the speech using the Google Web Speech API (recognize_google method) and print the recognized text.
  5. Handle exceptions for cases when the audio cannot be recognized (UnknownValueError) or if there's an issue with the API (RequestError).

Using the Voice-Controlled Code:

  1. Run the Python script, and you will see the message “Listening… Say Something.”
  2. Speak clearly into your microphone, and the code will recognize your speech.
  3. The recognized text will be printed on the screen.

Screenshots: Here are some screenshots showcasing the voice-controlled Python code in action:

Conclusion: With this revolutionary voice-controlled Python code, you can now communicate with your computer effortlessly. The possibilities are endless; you can use it for automation, launching applications, searching the web, and much more. Explore the code, experiment with it, and unlock the true potential of voice interaction with your machine. Feel free to contribute and improve the project on GitHub to make it even more powerful and user-friendly.

Remember to check out the GitHub repository for the complete code and stay tuned for more exciting projects!

So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all that it has to offer. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

--

--

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet