Last active
November 9, 2023 12:30
-
-
Save spinningcat/16fbb2dff9d2d05233d01172a2416797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sounddevice as sd | |
import SpeechRecognition as sr # Note the capitalization | |
def record_and_transcribe(): | |
duration = 5 # Recording duration in seconds | |
sample_rate = 44100 # Standard audio sample rate | |
channels = 1 # Mono audio (change to 2 for stereo) | |
# Record audio using sounddevice | |
print("Recording...") | |
audio_data = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=channels) | |
sd.wait() # Wait for recording to finish | |
# Initialize the recognizer | |
recognizer = sr.Recognizer() | |
# Convert audio data to speech recognition audio data | |
audio_data = audio_data.squeeze() # Remove extra dimension (if present) | |
audio_data = audio_data.tobytes() # Convert to bytes | |
# Recognize speech from the recorded audio | |
with sr.AudioData(audio_data, sample_rate, sample_width=2) as source: | |
try: | |
text = recognizer.recognize_google(source) | |
print("Transcribed text: ", text) | |
# Save the transcribed text to a file | |
with open("transcribed_text.txt", "w") as file: | |
file.write(text) | |
print("Transcribed text saved to 'transcribed_text.txt'") | |
except sr.UnknownValueError: | |
print("Speech recognition could not understand audio.") | |
except sr.RequestError as e: | |
print(f"Could not request results from Google Web Speech API; {e}") | |
if __name__ == "__main__": | |
record_and_transcribe() | |
Import "sounddevice" could not be resolved | |
Import "SpeechRecognition" could not be resolved | |
Traceback (most recent call last): | |
File "/home/spnnngcat/sounddevice/sdevice.py", line 2, in <module> | |
import SpeechRecognition as sr # Note the capitalization | |
ModuleNotFoundError: No module named 'SpeechRecognition' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment