Skip to content

Commit d673a27

Browse files
authored
Merge pull request #1800 from Shivansh-Jain-github/SJ_PR3
Adding python script to convert audio input to text input.
2 parents 8fafc50 + a966283 commit d673a27

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Audio to Text Convertor/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Speech to Text Conversion
2+
3+
This Python script demonstrates how to convert audio input into text using the `speech_recognition` library. It utilizes the `recognize_google` function from the library to convert the speech into text.
4+
5+
## Prerequisites
6+
- Python
7+
- `speech_recognition` library (install using `pip install SpeechRecognition`)
8+
- `pyaudio` library (install using `pip install pyaudio`)
9+
10+
Note: The `pyaudio` library is required for microphone access and recording audio.
11+
12+
## Usage
13+
1. Ensure that you have a working microphone connected to your computer.
14+
2. Run the script and speak into the microphone when prompted.
15+
3. The script will convert the audio to text using the Google Speech Recognition service.
16+
4. If the speech is recognized, the converted text will be displayed.
17+
5. Now, you can do whatever operations you want with the input text.
18+
19+
20+
21+
Feel free to use this code as a starting point for your speech-to-text conversion tasks.

Audio to Text Convertor/code.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import speech_recognition as sr
2+
3+
def convert_audio_to_text():
4+
# Initialize the recognizer
5+
r = sr.Recognizer()
6+
7+
# Open the microphone and start recording
8+
with sr.Microphone() as source:
9+
print("Speak something...")
10+
audio = r.listen(source) # Record audio from the microphone
11+
12+
try:
13+
# Use the recognizer to convert audio to text
14+
text = r.recognize_google(audio) # Convert audio to text using Google Speech Recognition service
15+
return text
16+
except sr.UnknownValueError:
17+
print("Unable to recognize speech.") # Handle the case when speech cannot be recognized
18+
except sr.RequestError as e:
19+
print("Error: {0}".format(e)) # Handle any errors that occur during speech recognition
20+
21+
# Example usage
22+
audio_text = convert_audio_to_text() # Call the function to convert audio to text
23+
if audio_text:
24+
print("You said:", audio_text) # Print the converted text
25+
26+
# Manipulate the text for further operations
27+
if audio_text:
28+
# Convert the text to lowercase
29+
lowercase_text = audio_text.lower()
30+
print("Lowercase text:", lowercase_text)
31+
32+
# Split the text into words
33+
words = lowercase_text.split()
34+
print("Words:", words)
35+
36+
# Perform other operations on the text as needed
37+

0 commit comments

Comments
 (0)