Saturday, November 14, 2020

Python Speech Recognition using Microphone or Audio File Example


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:

  1. WAV (must be in PCM/LPCM format)
  2. AIFF
  3. AIFF-C
  4. 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 :


Previous Post
Next Post

0 komentar: