Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added voice recognition and audio-to-text features #406

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Voice recognition/README.md
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions Voice recognition/audio_to_text.py
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 6 additions & 0 deletions Voice recognition/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions Voice recognition/voice_recognition.py
Original file line number Diff line number Diff line change
@@ -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")