This is simple tutorial for Speech Recognition using Python. You can speech recognize from Microphone or Audio File.
Supported File Types
Currently, SpeechRecognition supports the following file formats:
- WAV (must be in PCM/LPCM format)
- AIFF
- AIFF-C
- FLAC (must be native FLAC format; OGG-FLAC is not supported)
Installation
$ pip install SpeechRecognition $ pip install pyaudio
** If you using windows & getting an error while installing PyAudio, following this step:
$ pip install pipwin $ pipwin install pyaudio
If you've finished installing the libraries, let's we code for the next step.
Speech Recognition using Microphone
speech-recognition-from-microphone.py
import speech_recognition as sr r = sr.Recognizer() # Print list of microphone #print(sr.Microphone.list_microphone_names()) with sr.Microphone() as source: r.adjust_for_ambient_noise(source) while True: audio = r.listen(source) result = r.recognize_google(audio, language = 'en-US', show_all=True) print('Your voice : ', result) if len(result) > 0: for alt in result['alternative']: print('Transcript : ', alt['transcript']) # Save your voice into a file with open("my_voice.wav", "wb") as b: b.write(audio.get_wav_data())
Speech Recognition using Audio File
speech-recognition-from-audio-file.py
import speech_recognition as sr r = sr.Recognizer() audio_file = sr.AudioFile('my_voice.wav') with audio_file as source: r.adjust_for_ambient_noise(source) audio = r.record(source) result = r.recognize_google(audio, language = 'en-US', show_all=True) if len(result) > 0: print("Result : ", result) for alt in result['alternative']: print("Transcript : ", alt['transcript'])
How to Run the code?
py {filename}.py
Reference :
- Realpython.com - Python Speech Recognition
- Pypi.org - SpeechRecognition
- Github.com - Uberi/speech_recognition
- TowardsDataScience.com - Building a Speech Recognizer in Python
- Medium.com - Speech Recognition using Python part 1 Working with Microphone
- Medium.com - Speech Recognition using Python part 2 Working with Audio File
0 komentar: