Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Googles Text to speech
#1
Does anyone know how to get google to say back what the search result was? code provided:

import pyttsx3 #pip install pyttsx3
import speech_recognition as sr #pip install speechRecognition
import datetime
import wikipedia #pip install wikipedia
import webbrowser
import os
import smtplib
from googlesearch import *

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[1].id)
engine.setProperty('voice', voices[0].id)


def speak(audio):
    engine.say(audio)
    engine.runAndWait()


def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        print("Good Morning.")
        speak("Good Morning!")
        
    elif hour>=12 and hour<18:
        print("Good Afternoon.")
        speak("Good Afternoon!")
       
    else:
        print("Good Evening.")
        speak("Good Evening!")
    
    print("Initializing SAM...")
    speak("Initializing SAM...")
    print("Waiting for your command...")
    speak("Waiting for your command...")

def takeCommand():
    #It takes microphone input from the user and returns string output

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")    
        query = r.recognize_google(audio, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:
        # print(e)    
        print("Say that again please...")  
        return "None"
    return query

def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('', '')
    server.sendmail('', to, content)
    server.close()

if __name__ == "__main__":
    wishMe()
    while True:
    # if 1:
        query = takeCommand().lower()

        # Logic for executing tasks based on query
        if 'who' in query:
            speak('Searching Google...')
            query = query.replace("google", "")
            chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
            for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
                webbrowser.open("https://google.com/search?q=%s" % query)
            print("This is what I found according to your search")
            speak("This is what I found according to your search")

        elif 'what' in query:
            speak('Searching Google...')
            query = query.replace("google", "")
            chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
            for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
                webbrowser.open("https://google.com/search?q=%s" % query)
            print("This is what I found according to your search")
            speak("This is what I found according to your search")

        elif 'when' in query:
            speak('Searching Google...')
            query = query.replace("google", "")
            chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
            for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
                webbrowser.open("https://google.com/search?q=%s" % query)
            print("This is what I found according to your search")
            speak("This is what I found according to your search")

        elif 'where' in query:
            speak('Searching Google...')
            query = query.replace("google", "")
            chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
            for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
                webbrowser.open("https://google.com/search?q=%s" % query)
            print("This is what I found according to your search")
            speak("This is what I found according to your search")

        elif 'why' in query:
            speak('Searching Google...')
            query = query.replace("google", "")
            chrome_path = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe %s'
            for url in search(query, tld="co.in", num=1, stop = 1, pause = 2):
                webbrowser.open("https://google.com/search?q=%s" % query)
            print("This is what I found according to your search")
            speak("This is what I found according to your search")
           
        elif 'open youtube' in query:
            webbrowser.open("https://www.youtube.com/")

        elif 'open google' in query:
            webbrowser.open("google.com")

        elif 'goodbye' in query:
            speak('Goodbye Justin')
            quit() 

        elif 'play music' in query:
            webbrowser.open("")

        elif 'what\'s the time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")    
            speak(f"Sir, the time is {strTime}")

        elif 'open code' in query:
            codePath = "C:\\Users\\Owner\\Desktop\\SAM"
            os.startfile(codePath)

        elif 'email danny' in query:
            try:
                speak("What should I say?")
                content = takeCommand()
                to = ""    
                sendEmail(to, content)
                speak("Email sent.")
            except Exception as e:
                print(e)
                speak("Sorry Justin. I was not able to send this email")    
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Controlling text-to-speech pauses with pyttsx3 pmac1300 4 4,433 Mar-14-2022, 06:47 AM
Last Post: Coricoco_fr
  Speech Recognition with timestamps DeanAseraf1 3 6,565 Jun-27-2021, 06:58 PM
Last Post: gh_ad
  Continous Speech Recognition dell91 0 1,828 Oct-29-2020, 10:51 AM
Last Post: dell91
  How to Split Output Audio on Text to Speech Code Base12 2 6,857 Aug-29-2020, 03:23 AM
Last Post: Base12
  text to speech Heyjoe 11 6,789 Jul-02-2020, 01:32 AM
Last Post: Heyjoe
  Simulation of Text to speech (without using mic) to Voice recognition suported hardwa Helloworld20 2 2,216 Apr-08-2020, 02:13 AM
Last Post: Helloworld20
  Python Speech Engines? Robo_Pi 2 2,090 Mar-12-2020, 02:46 PM
Last Post: Robo_Pi
  Speech Recognition Ash23733 1 8,605 Dec-12-2018, 10:00 PM
Last Post: nilamo
  Speech (audio file, wav) to Text - Broken pipe Shobha 1 3,763 Nov-27-2018, 12:41 PM
Last Post: Larz60+
  Need Help With Text to Speech App Lethe 0 1,975 Oct-24-2018, 10:03 PM
Last Post: Lethe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020