diff --git a/Voice recognition/README.md b/Voice recognition/README.md new file mode 100644 index 0000000..8ad333c --- /dev/null +++ b/Voice recognition/README.md @@ -0,0 +1,13 @@ +# Voice Recognition & Audio to Text Converter + +This script provides functionalities for **real-time speech recognition** and **converting audio files into text** using Google Speech Recognition. + +## 📌 Features +- **Speech Recognition**: Converts live speech into text. +- **Audio File to Text**: Supports `WAV`,`AIFF`, `AIFF` `AIFF-C`, `FLAC` (MP3 and others require conversion) +- **Multiple Languages**: Recognizes speech in different languages (English, Spanish, French, Japanese, etc.). +- **Error Handling**: Detects missing microphones, silent input, and API errors. + +## 🚀 Installation +### Install Dependencies +pip install -r requirements.txt diff --git a/Voice recognition/audio_to_text.py b/Voice recognition/audio_to_text.py new file mode 100644 index 0000000..520dd91 --- /dev/null +++ b/Voice recognition/audio_to_text.py @@ -0,0 +1,40 @@ +import speech_recognition as sr + + +def audio_to_text(audio_file, language="en-US"): + """ + Recognizes speech using Google Speech Recognition. + + Parameters: + language (str): The language code to be used for recognition. + Examples: + - "en-US" for English + - "es-ES" for Spanish + - "pt-BR" for Portuguese (Brazil) + - "fr-FR" for French + - "ja-JP" for Japanese + """ + + recognizer = sr.Recognizer() + + try: + with sr.AudioFile(audio_file) as source: + print("Processing audio...") + audio = recognizer.record(source) + + text = recognizer.recognize_google(audio, language=language) + print("Recognized text:", text) + + except sr.UnknownValueError: + print("Could not understand the audio.") + except sr.RequestError as e: + print(f"Could not request results; {e}") + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + # You can change the language code below as needed: + # Supported formats: WAV, AIFF, AIFF-C, FLAC (MP3 and others require conversion) + # Provide the full path to the audio file + audio_to_text("C:/Users/YourUser/Music/audio.wav", language="en-US") diff --git a/Voice recognition/requirements.txt b/Voice recognition/requirements.txt new file mode 100644 index 0000000..ad68389 --- /dev/null +++ b/Voice recognition/requirements.txt @@ -0,0 +1,6 @@ +audioop-lts==0.2.1 +PyAudio==0.2.14 +SpeechRecognition==3.14.1 +standard-aifc==3.13.0 +standard-chunk==3.13.0 +typing_extensions==4.12.2 diff --git a/Voice recognition/voice_recognition.py b/Voice recognition/voice_recognition.py new file mode 100644 index 0000000..b1c7e2f --- /dev/null +++ b/Voice recognition/voice_recognition.py @@ -0,0 +1,45 @@ +import speech_recognition as sr + + +def recognize_speech(language="en-US"): + """ + Recognizes speech using Google Speech Recognition. + + Parameters: + language (str): The language code to be used for recognition. + Examples: + - "en-US" for English + - "es-ES" for Spanish + - "pt-BR" for Portuguese (Brazil) + - "fr-FR" for French + - "ja-JP" for Japanese + """ + recognizer = sr.Recognizer() + recognizer.pause_threshold = 3 + + try: + with sr.Microphone() as source: + print("Calibrating ambient noise, please wait...") + recognizer.adjust_for_ambient_noise(source, duration=1) + print("Please speak now!") + + audio = recognizer.listen(source, timeout=5) + + text = recognizer.recognize_google(audio, language=language) + print("Google Speech Recognition thinks you said: " + text) + + except sr.WaitTimeoutError: + print("No speech was detected within the timeout period.") + except sr.UnknownValueError: + print("Google Speech Recognition could not understand the audio.") + except sr.RequestError as e: + print(f"Could not request results from Google Speech Recognition service; {e}") + except OSError: + print( + "No microphone detected. Please ensure your microphone is connected and configured properly." + ) + + +if __name__ == "__main__": + # You can change the language code below as needed: + recognize_speech(language="es-ES")