Skip to content

Instantly share code, notes, and snippets.

@gucisz
Created March 16, 2020 17:36
Show Gist options
  • Save gucisz/a1267e37d1e55627040cfa84dce65d02 to your computer and use it in GitHub Desktop.
Save gucisz/a1267e37d1e55627040cfa84dce65d02 to your computer and use it in GitHub Desktop.
voice regognition app in Python
import speech_recognition as sr # import the library
import webbrowser
import time
import playsound
import os
import random
from gtts import gTTS
from time import ctime
r = sr.Recognizer() # initialize recognizer
def record_audio(ask = False):
with sr.Microphone() as source: # mention source it will be either Microphone or audio files.
if ask:
voiceapp(ask)
audio = r.listen(source) # listen to the source
voice_data = ''
try:
voice_data = r.recognize_google(audio, language="pl-PL") # use recognizer to convert our audio into text part.
except sr.UnknownValueError:
voiceapp('nie rozumiem')
except sr.RequestError:
voiceapp('Sorka, serwis padł ')
return voice_data
def voiceapp(audio_string):
tts = gTTS(text=audio_string, lang='pl')
r = random.randint(1, 10000000)
audio_file = 'audio-' + str(r) + '.mp3'
tts.save(audio_file)
playsound.playsound(audio_file)
print(audio_string)
os.remove(audio_file)
def respond(voice_data):
if 'imie' in voice_data:
voiceapp('My name is Marlena')
if 'date' in voice_data:
voiceapp(ctime())
if 'search' in voice_data:
search = record_audio('What do you want to search for?')
url = 'https://google.com/search?q=' + search
webbrowser.get().open(url)
voiceapp('here is what I found for ' + szukaj)
if 'place' in voice_data:
place = record_audio('What is the location?')
url = 'https://google.nl/maps/place/' + place + '/&'
webbrowser.get().open(url)
voiceapp('Here is the location' + place)
if 'exit' in voice_data:
exit()
time.sleep(1)
voiceapp('How can I help You?')
while 1:
voice_data = record_audio()
respond(voice_data)
@gucisz
Copy link
Author

gucisz commented Mar 16, 2020

unfortunetly my speech app does not recognise Polisch language. Any sugestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment