How to Convert Speech to Text in Python - CNDRO Website
3276
post-template-default,single,single-post,postid-3276,single-format-standard,bridge-core-2.9.4,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-child-theme-ver-1.0.0,qode-theme-ver-27.8,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.7.0,vc_responsive,elementor-default,elementor-kit-2634

How to Convert Speech to Text in Python

  • Automatic speech recognition (ASR): This performs the task of transcribing the audio file.
  • Natural language processing (NLP): It works on deriving meaning from the speech data and each text converted.
  • Text-to-speech (TTS): This converts text to human-like speech.
1 pip install speechrecognition

 

1 pip install pydub
1 pip install pyaudio

1 #import Library
2 import speech_recognition as sr
3
4 # this will be used to get audio from the microphone 5 v = sr.Recognizer()
6 #Here, we represent our microphone as source 7 with sr.Microphone() as source: 8 print("Speak:")
9 #This is where it listens to our speech before going further to recognize it 10 the_audio = v.listen(source)
11
12 try:
13 print("Your Speech was:" + v.recognize_google(the_audio))
14 except sr.UnknownValueError:
15 print("Could not understand audio")

 

Speech to text in Python

1 #we import our libraries here
2 from pydub import AudioSegment
3 from pydub.utils import make_chunks
4 import os
5 import speech_recognition as sr
6 import warnings
7 warnings.filterwarnings("ignore")
8
9 def process_audio(filename):
10 #We open a text file here to write in our Audio file
that has been converted to text
11 txtf = open("the_audio.txt", "w+")
12 #we use the AudioSegment to open our audio file, this
file is in .wav format
13 myaudio = AudioSegment.from_wav(filename)
14 #we specify our chunk length we want to use
15 chunks_length_ms = 7000
16 chunks = make_chunks(myaudio, chunks_length_ms)
17 #Here, we loop through our chunk object, which we
already pass our Audio file in and the length
18 for i, chunk in enumerate(chunks):
19 #we save this chunked file individually in a folder
in same wav format
20 chunkName = './chunked/'+filename+"_{0}.wav".format(i)
21 print('I am exporting', chunkName)
22 chunk.export(chunkName, format="wav")
23 #From here, we pass in the file individually to
be recognized via speech recognition library
24 file = chunkName
25 #we assign an object method from the speech
recognition library
26 r = sr.Recognizer()
27 #We make use of the speech recognition library
here, listening to each of our files
28 with sr.AudioFile(file) as source:
29 audio_listened = r.listen(source)
30 try:
31 #Here it tries to recognize and convert to text
32 rec = r.recognize_google(audio_listened)
33 #Now, we use our earlier text file we opened and
pass in the result of our conversion inside
34 txtf.write(rec+".")
35 #This handles error incase, the speech recognition
library doesn't understand your audio
36 except sr.UnknownValueError:
37 print("I don't recognize your audio")
38 except sr.RequestError as e:
39 print("could not get the result.check your internet") 40 #we created a folder where all those Audio files broken down will be saved
41 try:
42 os.makedirs("chunked")
43 except:
44 pass
45
46 #we call our function here
47
48 process_audio("hello.wav")
49
50
51

 

No Comments

Post A Comment

AllEscort