10 examples of 'how to make speech recognition in python faster' in Python

Every line of 'how to make speech recognition in python faster' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
47def main():
48 """Run all checks and print status."""
49 if not os.path.exists(CREDENTIALS_PATH):
50 print(
51 """Please follow the Custom Voice User Interface instructions on the AIY website
52to download credentials:
53https://aiyprojects.withgoogle.com/voice-v1/#makers-guide-3-custom-voice-user-interface
54and save them to""", CREDENTIALS_PATH)
55 return
56
57 if not check_credentials_valid():
58 print(
59 CREDENTIALS_PATH, """is not valid, please check that you have downloaded JSON
60service credentials.""")
61 return
62
63 if not check_speech_reco():
64 print('Failed to test the Cloud Speech API. Please see error above.')
65 return
66
67 print("Everything is set up to use the Google Cloud.")
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

238def main():
239 """start bidirectional streaming from microphone input to speech API"""
240
241 client = speech.SpeechClient()
242 config = speech.types.RecognitionConfig(
243 encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
244 sample_rate_hertz=SAMPLE_RATE,
245 language_code='en-US',
246 max_alternatives=1)
247 streaming_config = speech.types.StreamingRecognitionConfig(
248 config=config,
249 interim_results=True)
250
251 mic_manager = ResumableMicrophoneStream(SAMPLE_RATE, CHUNK_SIZE)
252 print(mic_manager.chunk_size)
253 sys.stdout.write(YELLOW)
254 sys.stdout.write('\nListening, say "Quit" or "Exit" to stop.\n\n')
255 sys.stdout.write('End (ms) Transcript Results/Status\n')
256 sys.stdout.write('=====================================================\n')
257
258 with mic_manager as stream:
259
260 while not stream.closed:
261 sys.stdout.write(YELLOW)
262 sys.stdout.write('\n' + str(
263 STREAMING_LIMIT * stream.restart_counter) + ': NEW REQUEST\n')
264
265 stream.audio_input = []
266 audio_generator = stream.generator()
267
268 requests = (speech.types.StreamingRecognizeRequest(
269 audio_content=content)for content in audio_generator)
270
271 responses = client.streaming_recognize(streaming_config,
272 requests)
273
274 # Now, put the transcription responses to use.
275 listen_print_loop(responses, stream)
276
277 if stream.result_end_time > 0:
278 stream.final_request_end_time = stream.is_final_end_time
279 stream.result_end_time = 0
280 stream.last_audio_input = []
281 stream.last_audio_input = stream.audio_input
282 stream.audio_input = []
283 stream.restart_counter = stream.restart_counter + 1
284
285 if not stream.last_transcript_was_final:
286 sys.stdout.write('\n')
287 stream.new_stream = True
26def test_google_french(self):
27 r = sr.Recognizer()
28 with sr.AudioFile(self.AUDIO_FILE_FR) as source: audio = r.record(source)
29 self.assertEqual(r.recognize_google(audio, language="fr-FR"), u"et c'est la dictée numéro 1")
31def test_google_chinese(self):
32 r = sr.Recognizer()
33 with sr.AudioFile(self.AUDIO_FILE_ZH) as source: audio = r.record(source)
34 self.assertEqual(r.recognize_google(audio, language="zh-CN"), u"砸自己的脚")
85def speech_synthesis_with_voice():
86 """performs speech synthesis to the default speaker with specified voice"""
87 # Creates an instance of a speech config with specified subscription key and service region.
88 speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
89 # Sets the synthesis voice name.
90 # The full list of supported voices can be found here:
91 # https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support#text-to-speech
92 voice = "Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)"
93 speech_config.speech_synthesis_voice_name = voice
94 # Creates a speech synthesizer for the specified voice,
95 # using the default speaker as audio output.
96 speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
97
98 # Receives a text from console input and synthesizes it to speaker.
99 while True:
100 print("Enter some text that you want to speak, Ctrl-Z to exit")
101 try:
102 text = input()
103 except EOFError:
104 break
105 result = speech_synthesizer.speak_text_async(text).get()
106 # Check result
107 if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
108 print("Speech synthesized to speaker for text [{}] with voice [{}]".format(text, voice))
109 elif result.reason == speechsdk.ResultReason.Canceled:
110 cancellation_details = result.cancellation_details
111 print("Speech synthesis canceled: {}".format(cancellation_details.reason))
112 if cancellation_details.reason == speechsdk.CancellationReason.Error:
113 print("Error details: {}".format(cancellation_details.error_details))
111def transcribe_file_with_auto_punctuation():
112 """Transcribe the given audio file with auto punctuation enabled."""
113 # [START speech_transcribe_auto_punctuation_beta]
114 from google.cloud import speech_v1p1beta1 as speech
115 client = speech.SpeechClient()
116
117 speech_file = 'resources/commercial_mono.wav'
118
119 with io.open(speech_file, 'rb') as audio_file:
120 content = audio_file.read()
121
122 audio = speech.types.RecognitionAudio(content=content)
123 config = speech.types.RecognitionConfig(
124 encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
125 sample_rate_hertz=8000,
126 language_code='en-US',
127 # Enable automatic punctuation
128 enable_automatic_punctuation=True)
129
130 response = client.recognize(config, audio)
131
132 for i, result in enumerate(response.results):
133 alternative = result.alternatives[0]
134 print('-' * 20)
135 print('First alternative of result {}'.format(i))
136 print('Transcript: {}'.format(alternative.transcript))
107def main():
108 # test example address file
109 plaintext = 'resources/example.txt'
110 ssml_text = text_to_ssml(plaintext)
111 ssml_to_audio(ssml_text, 'resources/example.mp3')
54def speech_synthesis_with_language():
55 """performs speech synthesis to the default speaker with specified spoken language"""
56 # Creates an instance of a speech config with specified subscription key and service region.
57 speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
58 # Sets the synthesis language.
59 # The full list of supported languages can be found here:
60 # https://docs.microsoft.com/azure/cognitive-services/speech-service/language-support#text-to-speech
61 language = "de-DE";
62 speech_config.speech_synthesis_language = language
63 # Creates a speech synthesizer for the specified language,
64 # using the default speaker as audio output.
65 speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
66
67 # Receives a text from console input and synthesizes it to speaker.
68 while True:
69 print("Enter some text that you want to speak, Ctrl-Z to exit")
70 try:
71 text = input()
72 except EOFError:
73 break
74 result = speech_synthesizer.speak_text_async(text).get()
75 # Check result
76 if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
77 print("Speech synthesized to speaker for text [{}] with language [{}]".format(text, language))
78 elif result.reason == speechsdk.ResultReason.Canceled:
79 cancellation_details = result.cancellation_details
80 print("Speech synthesis canceled: {}".format(cancellation_details.reason))
81 if cancellation_details.reason == speechsdk.CancellationReason.Error:
82 print("Error details: {}".format(cancellation_details.error_details))
68def transcribe(speech_file):
69 """Transcribe the given audio file."""
70
71 speech_client = speech.Client()
72
73 with io.open(speech_file, 'rb') as audio_file:
74 content = audio_file.read()
75 audio_sample = speech_client.sample(
76 content=content,
77 source_uri=None,
78 encoding='OGG_OPUS',
79 sample_rate_hertz=16000)
80
81 alternatives = audio_sample.recognize('es-ES')
82 for alternative in alternatives:
83 return '{}'.format(alternative.transcript)
80def speak_google_tts(text):
81 """ This method implements Text to Speech using the Google Translate TTS.
82 It uses Google Speech Python Package.
83 :param text: Text which is needed to be spoken
84 :return: None
85 """
86 with tempfile.TemporaryDirectory() as tmpdirname:
87 fd, mpiii = tempfile.mkstemp(suffix='.mp3', dir=tmpdirname)
88 Speech(text=text, lang=susi_config["language"]).save(mpiii)
89 player.say(mpiii)

Related snippets