1The best library because you dont have to save the
2text file or open the file to start the speech
3
4pip install pyttsx3
5
6import pyttsx3
7engine = pyttsx3.init()
8engine.say("Hello world")
9engine.runAndWait()
1#pip install SpeechRecognition
2#in case of error use 'pip install pyaudio' or...
3#in case of error use 'pip install pipwin' then 'pipwin install pyaudio'
4#if error continued you may need to use python 3.6 or lower as the latest
5#python may not support pyaudio...
6import speech_recognition as sr
7import pyttsx3
8
9#audio of system to respond
10engine = pyttsx3.init('sapi5')
11voices = engine.getProperty('voices')
12engine.setProperty('voice', voices[0].id)
13engine.setProperty('rate',180)
14
15def speak(audio):
16 engine.say(audio)
17 engine.runAndWait()
18
19# simple function to recognise speech from user
20def takecommand():
21 #it takes microphone input and returns string output
22 r = sr.Recognizer()
23 with sr.Microphone() as source:
24 print('Listening.....')
25 r.pause_threshold = 1
26 r.energy_threshold = 4000
27 audio = r.listen(source)
28
29 try:
30 print('Recognising...')
31 query = r.recognize_google(audio, language='en-in')
32 print('User Said : ' , query)
33
34 except Exception as e:
35 print('exception : ',e)
36
37 speak("Sorry, I didn't hear that, Say that again Please")
38 return "None"
39 return query
40while True:
41 query = takecommand() # whatever user says will be stored in this variable
42 print("The Test got in program is : "+query)
43
1import speech_recognition as sr
2
3def take_command():
4 r = sr.Recognizer()
5 with sr.Microphone() as source:
6 print('Listening...')
7 r.pause_threshold = 1
8 r.energy_threshold = 50
9 audio = r.listen(source)
10
11 try:
12 print('Recognizing...')
13 qry = r.recognize_google(audio, language='en-in')
14 print(f"user said: {qry}\n")
15
16# if any error occurs this line will run
17 except Exeption as e:
18 # if you don't want to print the error comment the bottom line
19 print(e)
20 print('Say that again please\n')
21 return 'None'
22
23 return qry
24
25if __name__ == '__main__':
26 while True:
27 qry = takecommand().lower()
28
29# now you can use the takecommand function where you want to recognize speech
30# And please experiment with the above code
31# like what pause_threshold and energy_threshold do
32/\/\/\/\/\/\/\/\/\/\/\--- *HAPPYCODING* ---/\/\/\/\/\/\/\/\/\/\/\
1 import speech_recognition as sr
2
3
4 def main():
5
6 r = sr.Recognizer()
7
8 with sr.Microphone() as source:
9 r.adjust_for_ambient_noise(source)
10
11 audio = r.listen(source)
12
13 try:
14
15 print(r.recognize_google(audio))
16
17 except Exception as e:
18 print("Error : " + str(e))
19
20
21 with open("recorded.wav", "wb") as f:
22 f.write(audio.get_wav_data())
23
24
25 if __name__ == "__main__":
26 main()
1# importing libraries
2import speech_recognition as sr
3import os
4from pydub import AudioSegment
5from pydub.silence import split_on_silence
6
7# create a speech recognition object
8r = sr.Recognizer()
9
10# a function that splits the audio file into chunks
11# and applies speech recognition
12def get_large_audio_transcription(path):
13 """
14 Splitting the large audio file into chunks
15 and apply speech recognition on each of these chunks
16 """
17 # open the audio file using pydub
18 sound = AudioSegment.from_wav(path)
19 # split audio sound where silence is 700 miliseconds or more and get chunks
20 chunks = split_on_silence(sound,
21 # experiment with this value for your target audio file
22 min_silence_len = 500,
23 # adjust this per requirement
24 silence_thresh = sound.dBFS-14,
25 # keep the silence for 1 second, adjustable as well
26 keep_silence=500,
27 )
28 folder_name = "audio-chunks"
29 # create a directory to store the audio chunks
30 if not os.path.isdir(folder_name):
31 os.mkdir(folder_name)
32 whole_text = ""
33 # process each chunk
34 for i, audio_chunk in enumerate(chunks, start=1):
35 # export audio chunk and save it in
36 # the `folder_name` directory.
37 chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
38 audio_chunk.export(chunk_filename, format="wav")
39 # recognize the chunk
40 with sr.AudioFile(chunk_filename) as source:
41 audio_listened = r.record(source)
42 # try converting it to text
43 try:
44 text = r.recognize_google(audio_listened)
45 except sr.UnknownValueError as e:
46 print("Error:", str(e))
47 else:
48 text = f"{text.capitalize()}. "
49 print(chunk_filename, ":", text)
50 whole_text += text
51 # return the text for all chunks detected
52 return whole_text