Creating a Note Taking App In Python With Speech Recognition

PYTHON PROJECT ("MAKE A NOTE MAKER SOFTWARE BY PYTHON USING 'speech_recognition' "):

reload ! diplomawaale.blogspot.com
Speech Recognition In Python


CODE DESCRIPTION:       

1. Importing Required Libraries: The code begins with importing the necessary library, `speech_recognition`, which is used for speech recognition functionality. This library provides an interface to various speech recognition engines and APIs.


2. Defining the Function `listen_and_type()`: The code defines a function named `listen_and_type()` that encapsulates the process of listening to spoken words, recognizing them, and saving them to a text file.


3. Initializing Recognizer and Microphone Objects:

- The `Recognizer` object from the `speech_recognition` library is instantiated as `recognizer`.

- The `Microphone` object from the same library is instantiated as the `microphone`. This represents the input source (microphone) used to capture audio.


4. Capturing Audio:

- The program prints "Listening..." to indicate that it is ready to capture audio.

- The `with` statement is used to manage the microphone as a context. It ensures that the microphone is properly opened and closed.

- The `adjust_for_ambient_noise()` method is called on the `recognizer` to account for any background noise and adjust the energy threshold for audio capture.

- The `listen()` method of the `recognizer` is used to capture audio from the microphone and store it in the `audio` variable.


5. Speech Recognition and Processing:

- Inside a `try` block, the program prints "Recognizing..." to indicate that it is processing the captured audio.

- The `recognize_google()` method of the `recognizer` is used to convert the captured audio into text. This method uses Google's Web Speech API for speech recognition.

- The recognized text is stored in the `text` variable.


6. Handling Recognition Results:

- If the speech recognition process is successful and text is recognized:

- The recognized text is printed using the `print()` function.

- The recognized text is appended to a file named "notes.txt" using the `open()` function in "append" mode. This allows you to store multiple notes in the same file.

- A message indicating that the note has been saved is printed.

- If the recognition process encounters an `sr.UnknownValueError`:

- The code catches this exception and prints "Sorry, could not understand audio." This happens when the speech recognition engine cannot understand the spoken words.

- If the recognition process encounters an `sr.RequestError` (from the `recognize_google()` call):

- The code catches this exception and prints an error message. This error occurs when the speech recognition engine encounters an issue while requesting the recognition service (e.g., no internet connection or API errors).


7. Main Execution:

- The `if __name__ == "__main__":` block ensures that the `listen_and_type()` function is only executed when the script is run directly (not when it's imported as a module in another script).

- The `listen_and_type()` function is called to initiate the speech recognition process.


DOWNLOAD/ COPY SOURCE CODE:




import speech_recognition as sr


def listen_and_type():
recognizer = sr.Recognizer()
microphone = sr.Microphone()

print("Listening...")

with microphone as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)

try:
print("Recognizing...")
text = recognizer.recognize_google(audio)
print("You said:", text)

# Save the recognized text to a file
with open("notes.txt", "a") as file:
file.write(text + "\n")

print("Note saved to 'notes.txt'")
except sr.UnknownValueError:
print("Sorry, could not understand audio.")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))


if __name__ == "__main__":
listen_and_type()



OUTPUT:

Creating a Note Taking App In Python With Speech Recognition  | diplomawaale.blogspot.com





Post a Comment

0 Comments