Пример #1
0
 def init_recognizer(self):
     r = sr.Recognizer()
     r.operation_timeout = 10
     return r
Пример #2
0
import speech_recognition as SRG
import requests
store = SRG.Recognizer()
import time


def startListening(timesec):
    time.sleep(int(timesec))
    with SRG.Microphone() as s:
        text_output = ""

        print("Speak...")
        nameCall = ""
        store.adjust_for_ambient_noise(s)
        audio_input = store.record(s, duration=3)
        try:
            nameCall = store.recognize_google(audio_input)
            print(nameCall)
            if nameCall.lower() == "jessica":
                print("word found")
                with SRG.Microphone() as s:
                    store.adjust_for_ambient_noise(s)
                    audio_input = store.record(s, duration=5)
                    text_output = store.recognize_google(audio_input)
                    text_output = text_output.lower()
                    if (len(text_output) > 1):
                        r = requests.post(
                            'http://localhost:8080/crisper/v1/exec',
                            json={'command': text_output})
                        print(r.text)
                print("Text converted from audio:\n")
Пример #3
0
																3:)pip install PyAudio # For using the different speech-to-text engines: We are using a google cloud based free service.
																4:)pip install PocketSpinx #for a local speech-to-text engine.

"""

import speech_recognition as sr

# This piece of code finds out the Ratio of Similarity between two different strings:
import difflib

# For GUI: I have assumed that you have downloaded the same:
from tkinter.ttk import Progressbar
from tkinter import *

# Global Variables:
r = sr.Recognizer()
s1 = 'I like sunny days'


# Main initializer class:
class SpeechRecognizerGUI:
    # This is where we detech the speech from the microphone. Once the microphone detects a sound, it sends it to google cloud where it is returned as Human understandable langauge.

    def clicked(self):

        # Sort of a progress bar: Doesnt work that great.
        self.bar['value'] = 0

        with sr.Microphone() as source:

            # This is where the code tells the system to listen to the sound. Please use a system that has a microphone enabled. I am using a laptop and there is no issue on my side.
def Tuesday():
    while True:
        try:
            r = sr.Recognizer()
            with sr.Microphone() as source:
                try:
                    print("What is your question?")
                except KeyboardInterrupt:
                    exit()
                    print("Exiting...")
                uinput = r.listen(source)  #Adds user input
            #uinput = input("Question: ")
            # for testing purposes, we're just using the default API key
            # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
            # instead of `r.recognize_google(audio)`
        #    try:
        #       uinput = r.recognize_google(uinput)

        #  except speech_recognition.UnknownValueError:
        #     tts = gTTS(text="I'm sorry, I don't know what you said.", lang='en')
        #    tts.save("tuesday-output.mp3")
        #   playsound('tuesday-output.mp3')

#
#           except sr.RequestError as e:
#              tts = gTTS(text="I need to be connected to the internet to understand what you say.", lang='en')
#               tts.save("tuesday-output.mp3")
#              playsound('tuesday-output.mp3')
#
#           except KeyboardInterrupt:
#              print("Exiting...")

            digits = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
            wolfA = False
            print(uinput)
            for number in digits[0:10]:
                if number in uinput:
                    something = False
                    wolfA = True

            if wolfA == True:
                try:
                    app_id = "G58JY9-WQ963T9EQV"  #to get the info
                    client = wolframalpha.Client(app_id)  #connecting to info
                    result = client.query(uinput)  #collecting result
                    answer = next(result.results).text  #processing answer
                    print(answer)

                    tts = gTTS(
                        text=answer,
                        lang='en',
                    )  ###### _Parameters:_ * `text` - String - Text to be spoken. * `lang` - String - [ISO 639-1 language code](#lang_list) (supported by the Google _Text to Speech_ API) to speak in. * `slow` - Boolean - Speak slowly. Default `False` (Note: only two speeds are provided by the API).

                    tts.save("tuesday-output.mp3")

                    playsound('tuesday-output.mp3')

                except sr.UnknownValueError:
                    print(
                        "Google Speech Recognition could not understand audio."
                    )

                except sr.RequestError as e:
                    print(
                        "Could not request results from Google Speech Recognition service; {0}"
                        .format(e))

            elif uinput == "exit":
                exit()

            elif "temperature" in uinput.lower():
                uinput = uinput.split(" ")
                placenum = len(uinput)
                placenum = placenum - 1
                place = uinput[placenum]
                observation = owm.weather_at_place(uinput[len(uinput) - 1])
                w = observation.get_weather()
                print(w)
                print(w.get_temperature("farenhe"))

            elif uinput == "jfhfh":
                return

            else:
                #WIKIPEDIA
                wikipedia.set_lang("en")  #Language!
                #        uinput = uinput.split(" ")
                #        uinput = " ".join(uinput[2:])
                try:
                    reply = wikipedia.summary(uinput, sentences=1)

                    print(reply)

                    tts = gTTS(
                        text=reply,
                        lang='en',
                    )  ###### _Parameters:_ * `text` - String - Text to be spoken. * `lang` - String - [ISO 639-1 language code](#lang_list) (supported by the Google _Text to Speech_ API) to speak in. * `slow` - Boolean - Speak slowly. Default `False` (Note: only two speeds are provided by the API).

                    tts.save("tuesday-output.mp3")

                    playsound('tuesday-output.mp3')

                except wikipedia.exceptions.DisambiguationError as e:
                    e = e.split("\n")
                    tts = gTTS(
                        text="Please narrow down your search." + e[0],
                        lang='en',
                    )  ###### _Parameters:_ * `text` - String - Text to be spoken. * `lang` - String - [ISO 639-1 language code](#lang_list) (supported by the Google _Text to Speech_ API) to speak in. * `slow` - Boolean - Speak slowly. Default `False` (Note: only two speeds are provided by the API).
                    tts.save("tuesday-output.mp3")
                    playsound('tuesday-output.mp3')
                    print(e)

                except wikipedia.exceptions.HTTPTimeoutError as e:
                    tts = gTTS(
                        text=
                        "Oh no! It looks like I have lost connection to the wikipedia server! Please try again later.",
                        lang='en',
                    )  ###### _Parameters:_ * `text` - String - Text to be spoken. * `lang` - String - [ISO 639-1 language code](#lang_list) (supported by the Google _Text to Speech_ API) to speak in. * `slow` - Boolean - Speak slowly. Default `False` (Note: only two speeds are provided by the API).
                    tts.save("tuesday-output.mp3")
                    playsound('tuesday-output.mp3')

        #        except wikipedia.exceptions.PageError as e:
        #            tts = gTTS(text="I can't seem to that page ID! Could you try another search?", lang='en',) ###### _Parameters:_ * `text` - String - Text to be spoken. * `lang` - String - [ISO 639-1 language code](#lang_list) (supported by the Google _Text to Speech_ API) to speak in. * `slow` - Boolean - Speak slowly. Default `False` (Note: only two speeds are provided by the API).
        #            tts.save("tuesday-output.mp3")
        #            playsound('tuesday-output.mp3')

                except wikipedia.exceptions.WikipediaException as e:
                    tts = gTTS(
                        text=e,
                        lang='en',
                    )  ###### _Parameters:_ * `text` - String - Text to be spoken. * `lang` - String - [ISO 639-1 language code](#lang_list) (supported by the Google _Text to Speech_ API) to speak in. * `slow` - Boolean - Speak slowly. Default `False` (Note: only two speeds are provided by the API).
                    tts.save("tuesday-output.mp3")
                    playsound('tuesday-output.mp3')

        except KeyboardInterrupt:
            tts = gTTS(
                text="Exiting...",
                lang='en',
            )  ###### _Parameters:_ * `text` - String - Text to be spoken. * `lang` - String - [ISO 639-1 language code](#lang_list) (supported by the Google _Text to Speech_ API) to speak in. * `slow` - Boolean - Speak slowly. Default `False` (Note: only two speeds are provided by the API).
            tts.save("tuesday-exit.mp3")
            playsound('tuesday-exit.mp3')
Пример #5
0
# Truy cập dữ liệu từ thư viện
import speech_recognition
import pyttsx3
from datetime import date, datetime
import wikipedia
import os
import playsound
import pyaudio
import webbrowser
import random
import requests, json  # Dùng để lấy data weather
from googletrans import Translator
from gtts import gTTS

# Khai báo biến
Lina_ear = speech_recognition.Recognizer()
Lina_mouth = pyttsx3.init()
Lina_brain = "Hello Sir, good day today!. Can I Help you"
print("Lina: " + Lina_brain)

Lina_mouth.say(Lina_brain)
Lina_mouth.runAndWait()

# Thay đổi giọng EN dùng cho câu trả lời tiếng anh
voices = Lina_mouth.getProperty('voices')
Lina_mouth.setProperty('voice', voices[0].id)  #0 for male
#meomeo_mouth.setProperty('voice', voices[1].id)   #1 for female


def sayvi(Lina_brain):  # Lina Nói Tiếng Việt
    tv = gTTS(text=Lina_brain, lang='vi')
Пример #6
0
    def __init__(self):
        self.r = sr.Recognizer()

        self.LANG = "ru-RU"
        self.TEXT = ""
Пример #7
0
# Fixed bugs
import speech_recognition as sr  # module to mic input
import pyttsx3 as tts  # text-to-speech
import pywhatkit as pwk  # finds songs through YouTube
import datetime  # retrieves dates and times
import wikipedia  # opens Wikipedia pages
import webbrowser as wb
import pyglet  # better audio output
from translate import Translator
import speech_recognition as ssr
import time
#import other funcs

# INITS
listener = sr.Recognizer()
engine = tts.init()
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)

#Calculator FUNCS:

#All in radius
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

#Basic
add = lambda a, b: a + b
minus = lambda a, b: a - b
times = lambda a, b: a * b
divide = lambda a, b: a / b

#More less Basic
Пример #8
0
import speech_recognition as speechRecognition
from gtts import gTTS
from playsound import playsound
import os

fin= False
while fin== False:
    reconocedor= speechRecognition.Recognizer()

    with speechRecognition.Microphone() as fuente:
        print ("Diga algo: ")
        audio = reconocedor.listen(fuente)

    try:
        #texto = reconocedor.recognize_google(audio)
        texto = reconocedor.recognize_google(audio, language="es-AR")
        print ("dijiste " + texto)
    except:
        print ("error durante la conversion")

    texto= texto.lower()
    texto= texto.replace('á', 'a')
    texto = texto.replace('é', 'e')
    texto = texto.replace('í', 'i')
    texto = texto.replace('ó', 'o')
    texto = texto.replace('ú', 'u')
    if texto== "hola como estas" or texto == "hola como te va":
        tts = gTTS("Muy bien, y vos?", lang='es-us')
    elif texto== "bien" or texto == "muy bien":
        tts = gTTS("Me alegro mucho", lang='es-us')
    elif texto== "mal" or texto == "mas o menos":
Пример #9
0
def downloadComplete():

    # coding: utf-8

    # In[8]:

    #!/usr/bin/env python
    # coding: utf-8

    # In[15]:

    import sys
    import nltk
    import numpy as np
    import speech_recognition as sr

    # In[16]:

    from nltk.tokenize import word_tokenize

    # In[17]:

    r = sr.Recognizer()
    mic = sr.Microphone()
    file = sr.AudioFile('sound.wav')
    # CHECK ADDRESS
    with file as source:
        r.adjust_for_ambient_noise(source)
        audio = r.record(source)
    text = r.recognize_google(audio)

    # In[101]:

    words = word_tokenize(text)
    words = np.array(words)
    words = words.reshape(1, -1)

    # In[124]:
    name = ''
    gender = ''
    address = ''
    age = ''
    phone = ''
    aadhar = ''
    g_name = ''
    g_relationship = ''
    g_number = ''

    for w in range(words[0].shape[0]):
        if words[0][w] == 'patient' and words[0][w + 1] == 'name':
            name = words[0][w + 2] + " " + words[0][w + 3]
        if words[0][w] == 'gender':
            gender = words[0][w + 1]
        if words[0][w] == 'age':
            age = words[0][w + 1]
        if words[0][w] == 'patient' and words[0][w + 1] == 'number':
            for a in range(10):
                phone += words[0][w + 2 + a]
        if (words[0][w] == 'Attendee'
                or words[0][w] == 'attendee') and words[0][w + 1] == 'name':
            g_name = words[0][w + 2] + " " + words[0][w + 3]
        if (words[0][w] == 'Attendee' or words[0][w]
                == 'attendee') and words[0][w + 1] == 'relationship':
            g_relationship = words[0][w + 4]
        if (words[0][w] == 'Attendee'
                or words[0][w] == 'attendee') and words[0][w + 1] == 'number':
            g_number = words[0][w + 2]
    output = {
        'patient_name': name,
        'patient_gender': gender,
        'patient_age': age,
        'patient_phone_number': phone,
        'Attendee_name': g_name,
        'Attendee_relationship_with_patient': g_relationship,
        'Attendee_phone_number': g_number
    }

    import json
    with open('result.json', 'w') as fp:
        json.dump(output, fp)
Пример #10
0
import speech_recognition as sr  # for speech recognition
#import pyaudio for microphone
import webbrowser as wb  # to search on the web

r1 = sr.Recognizer()  # recognizer class
r2 = sr.Recognizer()
r3 = sr.Recognizer()

with sr.Microphone() as source:
    print('[To Open Google: Speak search]')
    print('[To Search on Youtube: Speak video]')
    print('Speak now')
    audio = r3.listen(source)

if 'search' in r2.recognize_google(audio):
    r2 = sr.Recognizer()
    url = 'https://www.google.com/search?'
    with sr.Microphone() as source:
        print("Speak Google")
        audio = r2.listen(source)

        try:
            get = r2.recognize_google(audio)
            print(get)
            wb.get().open_new(url + get)
        except sr.UnknownValueError:
            print("Error")
        except sr.RequestError as e:
            print('failed'.format(e))

if 'video' in r1.recognize_google(audio):
Пример #11
0
import operator
import speech_recognition as s_r

r = s_r.Recognizer()
my_mic_device = s_r.Microphone(device_index=1)
with my_mic_device as source:
    print("Say what you want to calculate \n example: 3 plus 3")
    r.adjust_for_ambient_noise(source)
    audio = r.listen(source)
my_string = r.recognize_google(audio)
print(my_string)


def get_operator_fn(op):
    return {
        '+': operator.add,
        '-': operator.sub,
        'x': operator.mul,
        'multiplied by': operator.mul,
        'divided': operator.__truediv__,
        'Mod': operator.mod,
        'mod': operator.mod,
        '^': operator.xor,
        'add': operator.add,
        'subtract': operator.sub,
        '*': operator.mul,
        '/': operator.__truediv__,
    }[op]


def eval_binary_expr(op1, oper, op2):
Пример #12
0
import speech_recognition as sr
import webbrowser
import pyttsx
import time

eng = pyttsx.init()
voices = eng.getProperty('voices')
eng.setProperty('voice', voices[3].id)

while True:

    r = sr.Recognizer()
    mw = 'search for'
    slst = 'stop listening'

    with sr.Microphone() as source:

        audio = r.listen(source, timeout=None)

        try:

            if mw in r.recognize_google(audio):

                r2 = sr.Recognizer()

                print('What am I searching for?')
                eng.say('What am I searching for?')
                eng.runAndWait()

                with sr.Microphone() as source2:
Пример #13
0
 def __init__(self, logger, config):
     self.logger = logger
     self.config = config
     self.speech = sr.Recognizer()
     threading.Thread(target=self.run()).start()
Пример #14
0
def chatbot():
    engine = pyttsx3.init('espeak')

    def speak(audio):
        print('Computer: ' + audio)
        engine.say(audio)
        engine.runAndWait()

    path = '/home/p/Desktop/chatbot/pavan2.txt'
    f = open(path, 'r', errors='ignore')
    raw = f.read()
    raw = raw.lower()  # converts to lower case
    # punkt and wordnet should be downloaded
    # nltk.download('punkt')#punkt used to convert the list of words to strings
    # nltk.download('wordnet')#used as a dictionary where nltk find meanings and synonyms
    sent_tokens = nltk.sent_tokenize(raw)  # converts to list of sentenses
    word_tokens = nltk.word_tokenize(raw)  # converts to list of words
    # for testing the sent_tokens and word_tokens
    '''a = word_tokens[:2]
    print(a)
    b = sent_tokens[:2]
    print(b)
    '''

    # preprossesing of text
    lemmer = nltk.stem.WordNetLemmatizer()

    # word net is an dictionary  of english which is included in the nltk
    def LemTokens(tokens):
        return [lemmer.lemmatize(token) for token in tokens]

    remove_punct_dict = dict(
        (ord(punct), None) for punct in string.punctuation)

    def LemNormalize(text):  # here text is the user input
        return LemTokens(
            nltk.word_tokenize(text.lower().translate(remove_punct_dict)))

    # greeting
    GREETING_INPUT = (
        "hello",
        "hai",
        "hi",
        "sup",
        "greetings",
        "hey",
        "what'sap",
    )
    GREETING_OUTPUT = [
        "hai", "hi", "hello", "hi there", "i am gland you are taking to me"
    ]

    def greeting(sentence):
        for word in sentence.split():
            if word.lower() in GREETING_INPUT:
                return random.choice(GREETING_OUTPUT)

    # vectorization for this we need to modules Tfidvectorizer and cosine_similarity
    # defining response
    def response(user_response):

        chatbot_response = ''
        sent_tokens.append(user_response)
        TfidVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words='english')
        tfidf = TfidVec.fit_transform(
            sent_tokens
        )  # calling fit transform method and passing the sent tokens here the send tokens are converted into a vector form
        vals = cosine_similarity(
            tfidf[-1], tfidf
        )  # consine similarity for finding the similarity between the sentense tokens and user quations
        idx = vals.argsort()[0][-2]
        flat = vals.flatten(
        )  # for finding the matching btween quations and data stored in txt and converts it into a row matrix
        flat.sort()
        req_tfidf = flat[-2]
        if (
                req_tfidf != 0
        ):  # it means there is nothing matching between the quation and data
            chatbot_response = chatbot_response + sent_tokens[idx]
            return chatbot_response
        else:
            chatbot_response = chatbot_response + " i am sorry idont understand you"
            return chatbot_response

    flag = True
    print(
        "chatbot:My name is chatbot.I will answer your quaries about global warming. if you want to exit ,type bye"
    )
    while (flag == True):
        r = sr.Recognizer()
        # url2 = 'https://www.youtube.com/'

        with sr.Microphone() as source:
            print("Listening...")
            r.pause_threshold = 1
            url = r.listen(source)
            # get = str(input('Command: '))
            user_response = r.recognize_google(url, language='en')

        user_response = user_response.lower()
        if (user_response != 'bye' or 'exit'):
            if (user_response == 'thanks' or user_response == 'thank you'):
                flag = False
                print("chatbot:you are welcome..")
            else:
                if (greeting(user_response) != None):
                    print("chatbot:" + greeting(user_response))
                else:
                    print("chatbot:", end="")
                    print(response(user_response))
                    speak(response(user_response))

                    sent_tokens.remove(user_response)
        else:
            flag = False
            print("chatbot: Bye! take care..")
Пример #15
0
        print(
            "\n==============================================================\n"
        )
        gender = input(
            'What gender would you like your assistant to sound like?\n[ m ] Male\n[ f ] Female\n\n--> '
        )
        settings["gender"] = gender
        utilities.writeToJson(settings)


if __name__ == "__main__":

    # so here we start the shitty recognizer
    # code totally not stolen from GeeksForGeeks
    # :sweatsmile:
    recognizer = Recognizer.Recognizer()
    engine = pyttsx3.init()
    engine.setProperty('rate', 140)

    def main():

        utilities.startSlaves()

        # read the config which has already been defined
        # and use bloody use that LOL
        try:
            with open('settings.json', 'r') as settingsFile:
                settings = json.load(settingsFile)
                triggerWord = settings["trigger"]
                micId = int(settings["micID"])
                voiceGender = settings['gender']
Пример #16
0
Файл: Wit.py Проект: Boellis/pat
import speech_recognition as sr

recognizeSpeech = sr.Recognizer()

def talk():
    with sr.Microphone() as source:
        audio = recognizeSpeech.listen(source)
        user = recognizeSpeech.recognize_wit(audio,key='VSDKI2TOPBRIMVEWOCPGE32MMUWESQML')
        print(user)
    return user
Пример #17
0
 def __init__(self):
     super().__init__()
     self.platform = utils.get_platform()
     self.recognizer = sr.Recognizer()
Пример #18
0
        for i in r.json():
            bot_message = i['text']
            print(f"{bot_message}")

        #engine = pyttsx3.init()
        #engine.say(bot_message)
        #engine.runAndWait()
        myobj = gTTS(text=bot_message,lang='vi',slow = False)
        myobj.save("welcome.mp3")
        playsound.playsound('welcome.mp3', True)
        os.remove('welcome.mp3')
        # Playing the converted file
        #subprocess.call(['mpg321', "welcome.mp3", '--play-and-exit'])

        while True:
            r = sr.Recognizer()  # initialize recognizer
            with sr.Microphone() as source:  # mention source it will be either Microphone or audio files.
                r.adjust_for_ambient_noise(source) 
                audio = r.listen(source) # listen to the source
            try:
                message = r.recognize_google(audio,language='vi-VN')  # use recognizer to convert our audio into text part.
                print("Bạn nói : {}".format(message))
            except:
                print("Tôi không hiểu bạn nói gì")  # In case of voice not recognized  clearly
            if len(message)==0:
                continue
            print(".....")

            r = requests.post('http://localhost:5002/webhooks/rest/webhook', json={"message": message})

            print("Bot nói : ",end=' ')
import speech_recognition
#Khoi tao nen 1 ham nghe
robot_ear = speech_recognition.Recognizer()

#su dung with nghia la khi nghe xong roi se tat di
with speech_recognition.Microphone() as mic:
    print("Robot: I'm listening")
    audio = robot_ear.listen(mic)

you = robot_ear.recognize_google(audio)

print(you)
Пример #20
0
import telepot
from chatbot import Chatbot
from tempfile import mkstemp
import subprocess
import os
import speech_recognition as sr
from banco import *
from datetime import datetime
import os.path
import random

Bot = Chatbot('NOME BOT')
telegram = telepot.Bot("APIKEYTELEGRAM")

# iniciando Recognizer utilizado para reconhecer voz
rec = sr.Recognizer()
dataAtual = datetime.now().strftime('%Y-%m-%d')
Banco = Banco('host', 'port', 'username', 'password', 'database')
teste = Banco.teste()

# Validar se existe arquivo de log do dia
if (os.path.exists('log/log-' + dataAtual + '.json')):
    print('existe')
else:
    # Senão existe, ele vai ser criado
    print('Não existe')
    memoriaLog = open('log/log-' + dataAtual + '.json', 'w')
    memoriaLog = open('log/log-' + dataAtual + '.json', 'r')
    # Gravando dados no log
    log = []
    memoriaLog.close()
Пример #21
0
import sys

import pyttsx3 as tts
import speech_recognition
from neuralintents import GenericAssistant

recognizer = speech_recognition.Recognizer()

speaker = tts.init()
speaker.setProperty('rate', 150)

todo_list = ["Go shopping", "Go Code", "clean Room"]


def create_note():
    global recognizer

    speaker.say("What do you want to write onto your note?")
    speaker.runAndWait()

    done = False

    while not done:
        try:
            with speech_recognition.Microphone() as mic:
                recognizer.adjust_for_ambient_noise(mic, duration=0.2)
                audio = recognizer.listen(mic)

                note = recognizer.recognize_google(audio).lower()

                speaker.say("Choose a filename!")
Пример #22
0
    # Set the alpha variable to 0.5
    alpha=0.5)

plt.legend()
plt.show()

######Speech Recognition API

#install
#pip3 install SpeechRecognition

# Importing the speech_recognition library
import speech_recognition as sr

# Create an instance of the Recognizer class
recognizer = sr.Recognizer()

# Set the energy threshold
recognizer.energy_threshold = 300

# Create a recognizer class
recognizer = sr.Recognizer()

# Transcribe the support call audio
text = recognizer.recognize_google(audio_data=clean_support_call_audio,
                                   language='en-US')

print(text)

############Recognize from audio file to audio data
Пример #23
0
def chat():
    talkToMe(
        "Hello Master, I'm your personal assistant and I'm at your service")
    while True:

        #Speech Recognition Part:

        r = sr.Recognizer()

        with sr.Microphone() as source:
            r.pause_threshold = 0.7
            r.adjust_for_ambient_noise(source, duration=0.7)
            audio = r.listen(source)

        try:
            command = r.recognize_google(audio).lower()

            inp = command
            print(inp)  #For watching what we said

            results = model.predict([bag_of_words(inp, words)])[0]
            results_index = numpy.argmax(results)
            tag = labels[results_index]

            if results[results_index] > 0.9:
                for tg in data["intents"]:
                    if tg['tag'] == tag:
                        responses = tg['responses']

                talkToMe(random.choice(responses))
            else:
                talkToMe("I didn't get that, tell me again.")

            #Quit Commands:

            if inp.lower() == 'quit' or inp.lower() == 'exit' or inp.lower(
            ) == 'shut down':
                break

            #Browser Commands:

            elif 'open ' in inp:
                reg_ex = re.search('open (.+)', inp)
                if reg_ex:
                    domain = reg_ex.group(1)
                    url = 'https://www.' + domain + '.com'
                    webbrowser.open(url)
                else:
                    pass

            #Weather Commands:

            elif 'weather' in inp:
                res = requests.get('https://ipinfo.io/')
                info = res.json()
                country = info['country']
                zipcode = info['postal']

                degree_sign = u'\N{DEGREE SIGN}'
                owm = pyowm.OWM('58c31a987f2c5387dab8e3a11c9a18ae')
                observation = owm.weather_at_zip_code(zipcode, country)
                weather = observation.get_weather()
                status = str(weather.get_status())
                temperature = str(
                    round(weather.get_temperature('celsius')['temp']))

                talkToMe(
                    "The weather status is: " + status +
                    " and the temperature is: " + temperature + degree_sign +
                    ". You can look for more information from this website.")
                url = 'https://weather.com/weather/today'
                webbrowser.open(url)

            #Email Commands:

            elif 'email' in inp:
                talkToMe('Who is the recipient?')

                #Command Sub Function for Mailing:
                def myCommand():

                    r = sr.Recognizer()

                    with sr.Microphone() as source:
                        r.pause_threshold = 0.7
                        r.adjust_for_ambient_noise(source, duration=0.7)
                        audio = r.listen(source)

                    try:
                        command = r.recognize_google(audio).lower()
                        inp = command
                        print(inp)  #For watching what we said

                    except sr.UnknownValueError:
                        talkToMe("I didn't get that, tell me again.")
                        inp = myCommand()

                    return inp

                recipient = myCommand()

                if 'teacher' in recipient:
                    talkToMe('What should I say?')
                    content = myCommand()

                    #init gmail SMTP
                    mail = smtplib.SMTP('smtp.gmail.com', 587)

                    #identify to server
                    mail.ehlo()

                    #encrypt session
                    mail.starttls()

                    #login
                    mail.login('your gmail id', 'your gmail password')

                    #send message
                    mail.sendmail('name of the recipient',
                                  'mail of the recipient', content)

                    #end mail connection
                    mail.close()

                    talkToMe('Mail sent.')

                else:
                    talkToMe("I didn't get that")

        #loop back to continue to listen for commands if unrecognizable speech is received
        except sr.UnknownValueError:
            talkToMe("Hey are you here? I can't hear you")
Пример #24
0
def get_app(Q):
    current = Controller()
    # master
    if Q == "time":
        print(datetime.now())
        x = datetime.now()
        voice(x)
    elif Q == "news":
        speak_news()

    elif Q == "open notepad":
        subprocess.call(["Notepad.exe"])
    elif Q == "open calculator":
        subprocess.call(["calc.exe"])
    elif Q == "open stikynot":
        subprocess.call(["StikyNot.exe"])
    elif Q == "open shell":
        subprocess.call(["powershell.exe"])
    elif Q == "open paint":
        subprocess.call(["mspaint.exe"])
    elif Q == "open cmd":
        subprocess.call(["cmd.exe"])
    elif Q == "open discord":
        subprocess.call(["discord.exe"])
    elif Q == "open browser":
        subprocess.call(["C:\\Program Files\\Internet Explorer\\iexplore.exe"])
    # patch-1
    elif Q == "open youtube":
        webbrowser.open("https://www.youtube.com/")  # open youtube
    elif Q == "open google":
        webbrowser.open("https://www.google.com/")  # open google
    elif Q == "open github":
        webbrowser.open("https://github.com/")
    elif (
        Q == "email to other"
    ):  # here you want to change and input your mail and password whenver you implement
        try:
            speak("What should I say?")
            r = sr.Recognizer()
            with sr.Microphone() as source:
                print("Listening...")
                r.pause_threshold = 1
                audio = r.listen(source)
            to = "*****@*****.**"
            content = input("Enter content")
            sendEmail(to, content)
            speak("Email has been sent!")
        except Exception as e:
            print(e)
            speak("Sorry, I can't send the email.")
    # =======
    #   master
    elif Q == "Take screenshot":
        snapshot = ImageGrab.grab()
        drive_letter = "C:\\"
        folder_name = r"downloaded-files"
        folder_time = datetime.datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
        extention = ".jpg"
        folder_to_save_files = drive_letter + folder_name + folder_time + extention
        snapshot.save(folder_to_save_files)

    elif Q == "Jokes":
        speak(pyjokes.get_joke())

    elif Q == "start recording":
        current.add("Win", "Alt", "r")
        speak("Started recording. just say stop recording to stop.")

    elif Q == "stop recording":
        current.add("Win", "Alt", "r")
        speak("Stopped recording. check your game bar folder for the video")

    elif Q == "clip that":
        current.add("Win", "Alt", "g")
        speak("Clipped. check you game bar file for the video")
        with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
            listener.join()

    else:
        exit()

    # master

    apps = {
        "time": datetime.datetime.now(),
        "notepad": "Notepad.exe",
        "calculator": "calc.exe",
        "stikynot": "StikyNot.exe",
        "shell": "powershell.exe",
        "paint": "mspaint.exe",
        "cmd": "cmd.exe",
        "browser": "C:\\Program Files\Internet Explorer\iexplore.exe",
        "vscode": "C:\\Users\\Users\\User\\AppData\\Local\\Programs\Microsoft VS Code"
    }
Пример #25
0
class AgentCovia:
    greetings = ['hey there', 'hello', 'hi', 'hai', 'hey!', 'hey']
    question = ['how are you', 'how are you doing']
    var3 = ['time']
    cmdjokes = ['joke', 'funny']
    cmd2 = ['music', 'songs', 'song']
    jokes = [
        "can a kangaroo jump higher than a house? of course, a house doesn’t jump at all.",
        'my dog used to chase people on a bike a lot. it got so bad, finally i had to take his bike away.',
        'doctor: im sorry but you suffer from a terminal illness and have only 10 to live.patient: what do you mean, 10? 10 what? months? weeks?!"doctor: nine.'
    ]
    cmd4 = ['open youtube', 'i want to watch a video']
    cmd5 = ['weather']
    exitCmd = ['exit', 'close', 'goodbye', 'nothing']
    cmd7 = [
        'what is your color', 'what is your colour', 'your color',
        'your color?'
    ]
    colrep = [
        'right now its rainbow', 'right now its transparent',
        'right now its non chromatic'
    ]
    cmd8 = ['what is you favourite colour', 'what is your favourite color']
    cmd9 = ['thank you']
    rep9 = ["you're welcome", 'glad i could help you']
    webSearch: List[str] = ['firefox', 'internet', 'browser']
    sound = ['volume', 'sound']
    txtEdit = ['notepad']
    says = ['tell', 'say']
    newsText = ['news', 'headlines']
    engine = pyttsx3.init('sapi5')
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.setProperty('rate', 180)
    recog = sr.Recognizer()
    mic = sr.Microphone()
    logging.config.fileConfig('logger.config')
    logger = logging.getLogger('Admin_Client')
    posAnswers = ['yes', 'ok', 'yop']
    negAnswers = ['no', 'never', 'nope']
    mailCmd = ['mail', 'email']
    mypc = ['pc', 'laptop', 'system', 'computer']
    logoutCmd = ['log', 'sign', 'logout', 'signout']
    mydb = None
    usr = None
    permissions = {"email_access": 0, "internet_access": 0, "master_access": 0}

    def __init__(self):
        try:
            self.mydb = mysql.connector.connect(host="localhost",
                                                user="******",
                                                passwd="password",
                                                database="jarvis_data")
        except Exception as e:
            self.speak("MySQL not connected")
            self.logger.exception("MySQL not connected.")
            sys.exit()

    def getDatafromDb(self, Sqlquery):
        mycursor = self.setDatatoDb(Sqlquery)
        myresult = mycursor.fetchall()
        return myresult

    def setDatatoDb(self, Sqlquery):
        mycursor = self.mydb.cursor()
        mycursor.execute(Sqlquery)
        return mycursor

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

    def listenAudio(self):
        required = -1
        for index, name in enumerate(sr.Microphone.list_microphone_names()):
            if "External" in name:
                required = index
        if (required == -1):
            for index, name in enumerate(
                    sr.Microphone.list_microphone_names()):
                if "Internal" in name:  # (without earphone mic)
                    required = index
        print('say now')
        with sr.Microphone(device_index=required) as source:
            self.recog.adjust_for_ambient_noise(source)
            audio = self.recog.listen(source, phrase_time_limit=5)
        try:
            givenInput = self.recog.recognize_google(audio)
            return str(givenInput).lower()
        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")
            self.logger.error(
                "Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
            self.logger.error(
                "Could not request results from Google Speech Recognition service; {0}"
                .format(e))

    def listenAudioLong(self):
        required = -1
        for index, name in enumerate(sr.Microphone.list_microphone_names()):
            if "External" in name:
                required = index
        if (required == -1):
            for index, name in enumerate(
                    sr.Microphone.list_microphone_names()):
                if "Internal" in name:  # (without earphone mic)
                    required = index
        print('say now')
        with sr.Microphone(device_index=required) as source:
            self.recog.adjust_for_ambient_noise(source)
            audio = self.recog.listen(source, phrase_time_limit=10)
        try:
            givenInput = self.recog.recognize_google(audio)
            return str(givenInput).lower()
        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")
            self.logger.error(
                "Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
            self.logger.error(
                "Could not request results from Google Speech Recognition service; {0}"
                .format(e))

    def validateCommand(self, query, givenList):
        for word in query:
            for gword in givenList:
                if (word == gword):
                    return True
        return False

    def wishMe(self):
        hour = int(datetime.now().hour)
        if (hour >= 0 and hour < 12):
            return " Good morning!"
        elif (hour >= 12 and hour <= 16):
            return " Good afternoon!"
        else:
            return " Good evening!"

    def setPassword(self):
        if (self.permissions["master_access"] == 1):
            self.speak("setting new passcode.... enter the new password")
            npass = str(input())
            try:
                updateQuery = "update credentials set password ='******' where username='******'"
                self.setDatatoDb(updateQuery)
                self.logger.debug("passcode changed successfully.")
                self.speak('I Successfully changed your passcode....')
            except Exception as e:
                self.logger.error("Exception in password changing. message: " +
                                  str(e))
                self.speak('Unable to change password.')
        else:
            self.speak("Insufficient permissions to change password")
            self.logger.error("Insufficient permissions to change password")

    def authUser(self, rpass, pcode):
        if (pcode == rpass[0][1]):
            self.speak("Passcode matched.... ")
            self.logger.debug(
                "in authUser().Passcode matched & agent activated.")
            greeting = random.choice(self.greetings)
            wish = greeting + " " + rpass[0][0] + self.wishMe()
            self.speak(wish)
            self.getTime()
            self.usr = rpass[0][0]
            Sqlquery = 'SELECT email_access, internet_access, master_access FROM `permissions` WHERE `user`=(SELECT cred_id FROM `credentials` WHERE username="******")'
            userAccess = self.getDatafromDb(Sqlquery)
            self.permissions["master_access"] = int(userAccess[0][2])
            self.permissions["internet_access"] = int(userAccess[0][1])
            self.permissions["email_access"] = int(userAccess[0][0])
            self.processRequests()
        else:
            self.speak("Invalid passcode.....Exiting.")
            self.logger.error("Invalid Passcode.Exiting.")
            sys.exit()

    def processRequests(self):
        while (1):
            try:
                time.sleep(2)
                self.speak('Listening now')
                self.logger.debug('Listening......')
                query = self.listenAudio()
                if (query == None):
                    query = ''
                self.logger.debug('query: ' + str(query))
                self.processCommands(query)
            except Exception as e:
                self.speak("sorry.....unable to process...say again")
                print(str(e))
                self.logger.exception(str(e))

    def processCommands(self, query):
        queryx = query.split()
        queryx = [
            word for word in queryx
            if not word in set(stopwords.words('english'))
        ]
        if (self.validateCommand(queryx, self.webSearch)
                and (self.permissions["internet_access"] == 1
                     or self.permissions["master_access"] == 1)):
            if (r'search' in query):
                self.getInfoWebSearch(query)
            elif (r'go to'):
                comp = regex.compile(r'(go\s*to)\s*([\w\s\.]*)')
                match = regex.search(comp, query)
                try:
                    squery = match.group(2)
                except AttributeError:
                    squery = ''
                url = 'https://' + squery
                self.openFirefox(url)
        elif ('change your password' == query):
            self.setPassword()
        elif (self.validateCommand(queryx, self.says)
              and (r'something' in query)
              and (self.permissions["internet_access"] == 1
                   or self.permissions["master_access"] == 1)):
            self.tellInfoFromWiki(query)
        elif self.validateCommand(queryx, self.var3):
            self.getTime()
        elif self.validateCommand(queryx,
                                  self.mailCmd) and self.validateCommand(
                                      queryx, ['send']):
            self.sendEmail()
        elif query in self.question:
            self.speak('I am fine')

        elif query in self.cmd9:
            self.speak(random.choice(self.rep9))

        elif query in self.cmd7:
            self.speak('It keeps changing every second')

        elif query in self.cmd8:
            self.speak(random.choice(self.colrep))
            self.speak('It keeps changing every second')
        elif self.validateCommand(queryx, self.cmd5):
            self.getWeatherInfo()
        elif self.validateCommand(queryx, self.cmd2):
            self.playMusic()

        elif (r'open' in query):
            # open [foldername] inside [name of drive] drive in file explorer
            if (r'file explorer' in query):
                self.openFileExplorer(query)
            else:
                openApp = self.openAnyApplication(query)
                if (openApp == 'notepad'):
                    self.openNotepad()
                else:
                    self.speak('app not found')

        elif (self.validateCommand(queryx, self.newsText)
              and (self.permissions["internet_access"] == 1
                   or self.permissions["master_access"] == 1)):
            self.speak("fetching headlines from IndiaToday")
            url = 'https://www.indiatoday.in/top-stories'
            self.getHeadines(url)
        elif self.validateCommand(queryx, self.cmdjokes):
            self.speak(random.choice(self.jokes))
        elif (query == 'who are you' or query == 'what is your name'):
            self.speak('I am covia, your personal assistant')
        elif ('who is your developer' in query) or (r'who made you' in query):
            self.speak('Mriganka Goswami developed me.')
        elif (r'shutdown' in query) and self.validateCommand(
                queryx, self.mypc):
            os.system("shutdown /s /t 1")

        elif (r'restart' in query) and self.validateCommand(
                queryx, self.mypc) and self.permissions["master_access"] == 1:
            os.system("shutdown /r /t 1")
        elif (r'lock' in query) and self.validateCommand(queryx, self.mypc):
            ctypes.windll.user32.LockWorkStation()

        elif (self.validateCommand(queryx, self.logoutCmd) or
              (r'out' in query)) and self.validateCommand(
                  queryx,
                  self.mypc) and self.permissions["master_access"] == 1:
            os.system("shutdown -l")
        elif (query in self.exitCmd):
            self.speak('Good bye!')
            sys.exit()
        else:
            self.speak("sorry....invalid voice command...say again")

    def getTime(self):
        now = datetime.now()
        self.speak(now.strftime("The time is %H:%M"))

    def openAnyApplication(self, query):
        app = regex.compile(r'(open)\s*([\w\s]*)')
        match = regex.search(app, query)
        try:
            openApp = match.group(2)
        except:
            self.speak('unable to open the application')
            self.logger.exception("Error in opening application")
            openApp = ''
        return openApp

    def openFileExplorer(self, query):
        comp = regex.compile(r'(open)\s*([\w\s]*)\s*inside\s*([\w])\s')
        match = regex.search(comp, query)
        try:
            drivePath = match.group(3)
            folderPath = match.group(2)
        except:
            self.speak("Drive name is missing.....try again")
            self.logger.exception("Drive name missing in 'open drive'")
        path = drivePath + ":/" + folderPath + "/"
        path = os.path.realpath(path)
        os.startfile(path)

    def getInfoWebSearch(self, query):
        comp = regex.compile(r'(search)\s*([\w\s]*)')
        match = regex.search(comp, query)
        try:
            squery = match.group(2)
        except AttributeError:
            squery = ''
        squery = squery.replace('about', '').replace(' ', '+')
        url = 'https://www.google.com/search?q=' + squery
        self.openFirefox(url)

    def tellInfoFromWiki(self, query):
        comp = regex.compile(r'(something\s*about)\s*([\w\s]+)')
        match = regex.search(comp, query)
        try:
            self.getDataFromWiki(match)
        except AttributeError:
            self.speak('unable to process your request now')
            self.logger.exception("Exception in 'say something'")

    def playMusic(self):
        path = 'X://Covia//'
        folder = path
        for the_file in os.listdir(folder):
            file_path = os.path.join(folder, the_file)
            try:
                if os.path.isfile(file_path):
                    os.unlink(file_path)
            except Exception as e:
                self.logger.exception("Unable to find directory")
                self.speak('unable to find music directory')
        self.speak('Which song shall I play?')
        mysong = self.listenAudio()
        if mysong:
            flag = 0
            url = "https://www.youtube.com/results?search_query=" + mysong.replace(
                ' ', '+')
            response = urlopen(url)
            html = response.read()
            soup1 = BeautifulSoup(html, "lxml")
            url_list = []
            for vid in soup1.findAll(attrs={'class': 'yt-uix-tile-link'}):
                if ('https://www.youtube.com' + vid['href']
                    ).startswith("https://www.youtube.com/watch?v="):
                    flag = 1
                    final_url = 'https://www.youtube.com' + vid['href']
                    url_list.append(final_url)
            if flag == 1:
                url = url_list[0]
                ydl_opts = {}
                os.chdir(path)
                with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                    ydl.download([url])
                for the_file in os.listdir(folder):
                    path = os.path.join(folder, the_file)
                player = vlc.MediaPlayer(path)
                player.play()
                duration = player.get_length() / 1000
                duration = time.time() + duration
                while True:
                    exitQuery = self.listenAudio()
                    if exitQuery != None:
                        exitQuery = exitQuery.split()
                        if self.validateCommand(exitQuery, self.exitCmd):
                            player.stop()
                            break
                    continue
        if flag == 0:
            self.speak('I have not found anything in Youtube ')

    def getWeatherInfo(self):
        owm = pyowm.OWM('57a134899fde0edebadf307061e9fd23')
        observation = owm.weather_at_place('Barpeta, IN')
        w = observation.get_weather()
        self.speak(w.get_wind())
        self.speak('humidity')
        self.speak(w.get_humidity())
        self.speak('temperature')
        self.speak(w.get_temperature('celsius'))

    def openNotepad(self):
        path = 'C:\\Windows\\notepad.exe'
        os.startfile(path)

    def getHeadines(self, url):
        response = rq.get(url)
        status_code = response.status_code
        if (status_code == 200):
            self.topNewsHeadlines(response)
        else:
            self.speak("Error in fetching data from internet")
            self.logger.error("error in fetching data")

    def topNewsHeadlines(self, response):
        elems = []
        soup = BeautifulSoup(response.text, 'html.parser')
        for i in range(1, 11):
            selector = '#content > div.view.view-category-wise-content-list.view-id-category_wise_content_list.view-display-id-section_wise_content_listing.view-dom-id-.custom > div.view-content > div:nth-child(' + str(
                i) + ') > div.detail > h2 > a'
            selected = soup.select(selector)
            if (len(selected) and selected[0].text != '\n'):
                selected = selected[0].text
                elems.append(selected)
        for i in range(len(elems)):
            news = str(i + 1) + '..........' + elems[i]
            self.speak(news)

    def getDataFromWiki(self, match):
        squery = match.group(2)
        gotText = wikipedia.summary(squery)
        gotText = regex.sub('(\/[\w]+\/)', '', gotText)
        gotText = regex.sub('(\[[\d]+\])', '', gotText)
        gotText = regex.sub('[^A-Za-z0-9\s\(\)\-\,\.]+', '', gotText)
        gotText = gotText.replace('\n', '')
        self.speak(gotText)
        time.sleep(2)
        self.speak('Do you want to save this information?')
        answer = self.listenAudio()
        answer = answer.split()
        if self.validateCommand(answer, self.posAnswers):
            raNum = squery + str(random.randint(1, 1000))
            drive = 'X:\\Docs\\' + raNum + ".txt"
            file = open(drive, 'w+')
            file.write(gotText)
            file.close()
            self.speak('information saved in docs folder with ' + raNum +
                       ' name.')
        elif self.validateCommand(answer, self.negAnswers):
            self.speak('As your wish!')

    def openFirefox(self, url):
        browser = self.initializeBrowser(url)
        xquery = 'running'
        while (xquery != 'close browser'):
            xquery = self.listenAudio()
            if (xquery == 'close browser'):
                browser.close()

    def sendEmail(self):
        self.speak('Who is the recipient?')
        recipient = self.listenAudio()
        recipient = recipient.replace(' ', '')

        if (recipient != None and (self.permissions["master_access"] == 1
                                   or self.permissions["email_access"] == 1)):
            SqlQueryRecipient = 'SELECT email_id FROM email_list WHERE shortname = "' + recipient + '"'
            EmailIdList = self.getDatafromDb(SqlQueryRecipient)
            if (len(EmailIdList)):
                emailId = EmailIdList[0][0]
                self.speak('What should I say to him?')
                content = self.listenAudioLong()
                if (content == None):
                    content = "Hii there"
                self.speak('can i send the message?')
                resp = self.listenAudio()
                if (resp == None):
                    resp = "yes"
                if (resp == 'yes'):
                    userSql = 'SELECT email, epass FROM email_auth WHERE cred_id=(SELECT cred_id FROM credentials WHERE username= "******")'
                    userEmailCred = self.getDatafromDb(userSql)
                    username = userEmailCred[0][0]
                    password = userEmailCred[0][1]
                    mail = smtplib.SMTP('smtp.gmail.com', 587)
                    mail.ehlo()
                    mail.starttls()
                    mail.login(username, password)
                    mail.sendmail(username, emailId, content)
                    mail.close()
                    self.speak('Email has been sent successfully')
                else:
                    self.speak('Email sending cancelled')
            else:
                self.speak('no recipients found')

    def initializeBrowser(self, url):
        browser = Firefox()
        browser.get(url)
        return browser

    def driverFunc(self):
        try:
            self.speak("Agent started")
            self.logger.info("Agent started")
            self.speak("please verify your identity!")
            username = str(input())
            self.logger.info('USERNAME: '******'select username,password from credentials where username="******"'
            userData = self.getDatafromDb(Sqlquery)
            if (len(userData)):
                self.speak('Enter the password')
                passcode = str(input())
                self.logger.debug(passcode)
                self.speak('Processing passcode')
                self.authUser(userData, passcode)
            else:
                self.logger.error("user not found")
                self.speak('user not found!')
                sys.exit()
        except Exception as e:

            self.logger.exception(
                "Exception in main function(before authentication). message: "
                + str(e))
        finally:
            logging.shutdown()
            self.mydb.close()

    def quit(self):
        sys.exit()
def rex_start():

    r = sr.Recognizer()
    engine1 = pyttsx3.init()
    engine1.setProperty('rate', 150)
    engine1.say("Hello, I am Rex the voice assistant.")
    engine1.runAndWait()
    with sr.Microphone() as source:
        print('Say Something!')
        engine1.say("Say Something that i can do for you")
        engine1.runAndWait()
        audio = r.listen(source)
        print('Done!')

    try:
        text = r.recognize_google(audio)
        print('You said:\n' + text)
        lang = 'en'
        engine1 = pyttsx3.init()
        engine1.setProperty('rate', 150)

        engine1.say("You Searched " + text)
        engine1.runAndWait()

        if text == 'YouTube':
            wb.open('https://www.youtube.com')

        elif text == 'tell a joke':
            z = """A man asks a farmer near a field, Sorry sir, would you mind if I crossed your field instead of going around it? You see, I have to catch the 4:23 train.\n\
              The farmer says, Sure, go right ahead. And if my bull sees you, you’ll even catch the 4:11 one."""
            engine1.say(z)
            print(z)
            engine1.runAndWait()

        elif text == 'tell me a weather':
            api_address = 'http://api.openweathermap.org/data/2.5/weather?appid=c3b39dac5374cedd511992b18e7fb675&q=Sahiwal'
            # city = input('City Name :')
            # url = api_address + city
            json_data = requests.get(api_address).json()
            format_add = json_data['weather'][0]['description']
            engine1.say(format_add)
            print(format_add)
            engine1.runAndWait()

        elif text == 'tell me a time':
            z = datetime.datetime.now()
            z1 = z.strftime("%I hours %M minutes %S seconds %p")
            z2 = z.strftime("%A, %B %d, %Y")
            engine1.say(z2)
            engine1.say(z1)
            print(z1)
            print(z2)
            engine1.runAndWait()
        elif text == 'dictionary':
            dictionary = PyDictionary()
            engine1.say("say a word")
            engine1.runAndWait()
            with sr.Microphone() as source1:
                audio1 = r.listen(source1)
                text1 = r.recognize_google(audio1)
                lang = 'en'
                print(text1)
                engine1.say("You Said " + text1)
                engine1.say("Meaning")
                print("Meanings:")
                print(dictionary.meaning(text1))
                print(dictionary.translate(text1, 'ur-PK'))
                engine1.say(dictionary.meaning(text1))
                engine1.say(dictionary.translate(text1, 'ur-PK'))
                engine1.runAndWait()

        else:
            f_text = 'https://www.google.com.pk/search?hl=en&source=hp&ei=x80UXKqXMK6QmgXI8oDwAg&q=' + text
            wb.open(f_text)

    except Exception as e:
        print(e)
# pip install moviepy

import speech_recognition as sr
import moviepy.editor as mp
from pydub import AudioSegment
from nltk import sent_tokenize
from nltk import word_tokenize
from nltk.corpus import stopwords

# Retirando o texto de uma videoaula
aula = mp.VideoFileClip('aula.mp4').subclip(0,120)
aula.audio.write_audiofile('aula.wav')

with sr.AudioFile('aula.wav') as source:
     arquivo = sr.Recognizer()

     audio = arquivo.record(source)   

     # Passa a variável para a API do Google
     frase = arquivo.recognize_google(audio,language='pt-BR')
     
     
print("\n Texto da VideoAula: ")
print(frase)

# Separa o texto da ementa em sentenças/períodos
periodos = sent_tokenize(frase)
print("\n Texto separado em Períodos: ")
print(periodos)
print("\n")
Пример #28
0
import speech_recognition as sr
import pyttsx3
import subprocess
import time
from googlesearch import search 

AI_hear = sr.Recognizer()
AI_understand = ''
AI_speak = pyttsx3.init()

running = True

a = []
n = 0

while running :
    #hear 
    with sr.Microphone() as mic:
        print("AI: How can i help you?")
        audio = AI_hear.listen(mic)
        print('AI: ...')
    try:        
        me = AI_hear.recognize_google(audio)
        print("Me: "+me)
    except:
        me = ""
    
    #knowledge    
    if me is not None:
        n+=1
        for i in range (0,n) :
def game_loop():
    global pontos
    gameExit = False
    global falouAlf

    if falouAlf == False:
        falouAlf = falaAlfabeto()
    else:
        time.sleep(0.01)

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        imagem(loopImg, 0, 0)

        letraAtual = random.choice(alf)
        if letraAtual == "z":
            letraSeg = "a"
            letraSegMaisc = "A"
        else:
            letraSeg = alf[alf.find(letraAtual) + 1]
            letraSegMaisc = alfMaisc[alf.find(letraAtual) + 1]

        frase1 = 'A letra é         ' + "          " + letraAtual + " ...         "
        pcLetra = gTTS(text=frase1, lang="pt", slow=False)
        pcLetra.save(letraAtual + ".mp3")
        mixer.init()
        mixer.music.load(letraAtual + ".mp3")
        mixer.music.play()
        time.sleep(1.5)

        frase2 = 'Qual é a letra seguinte?'
        pcLetra = gTTS(text=frase2, lang="pt", slow=False)
        pcLetra.save("perguntaLetra.mp3")
        mixer.init()
        mixer.music.load("perguntaLetra.mp3")
        mixer.music.play()

        time.sleep(0.2)
        pontuacao(pontos)
        mostra_letra_atual(letraAtual)

        microfone = sr.Recognizer()

        with sr.Microphone() as source:
            #Chama um algoritmo de reducao de ruidos no som
            microfone.adjust_for_ambient_noise(source)
            time.sleep(0.01)
            audio = microfone.listen(source)
            time.sleep(0.752)

            try:
                pessoaFalou = microfone.recognize_google(audio,
                                                         language="pt-BR")

                letraFalada = format(pessoaFalou)

                print("pessoaFalou: " + pessoaFalou)

                if letraFalada == letraSeg or letraFalada == letraSegMaisc:
                    pontos += 1
                    acertou(pontos)
                    print("Total : " + pontos)
                elif letraFalada != letraSeg or letraFalada != letraSegMaisc:
                    if letraSeg == "a":
                        if letraFalada == "ah" or letraFalada == "A" or letraFalada == "á" or letraFalada == "Á":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "b":
                        if letraFalada == "be" or letraFalada == "bee" or letraFalada == "bê" or letraFalada == "Be" or letraFalada == "Bê" or letrafalada == "BE" or letraFalada == "BÊ":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "c":
                        if letraFalada == "ce" or letraFalada == "ssê" or letraFalada == "cê" or letraFalada == "Ce" or letraFalada == "Cê" or letrafalada == "CE" or letraFalada == "CÊ":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "d":
                        if letraFalada == "de" or letraFalada == "dê" or letraFalada == "De" or letraFalada == "Dê" or letrafalada == "DE" or letraFalada == "DÊ":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "e":
                        if letraFalada == "ee" or letraFalada == "É" or letraFalada == "é" or letraFalada == "ê" or letraFalada == "eh" or letraFalada == "Ê" or letraFalada == "EE":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "f":
                        if letraFalada == "ef" or letraFalada == "éf" or letraFalada == "éfi" or letraFalada == "EF" or letraFalada == "EFE":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "g":
                        if letraFalada == "ge" or letraFalada == "gê" or letraFalada == "Ge" or letraFalada == "Gê" or letraFalada == "GÊ" or letrafalada == "GE":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "h":
                        if letraFalada == "agá" or letraFalada == "AGÁ":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "i":
                        if letraFalada == "ii" or letraFalada == "ih" or letraFalada == "í" or letraFalada == "íh" or letraFalada == "Í":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "j":
                        if letraFalada == "jota" or letraFalada == "joota" or letraFalada == "Jota" or letraFalada == "JOTA" or letraFalada == "jóta" or letraFalada == "Jóta" or letrafalada == "JÓTA":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "k":
                        if letraFalada == "ca" or letraFalada == "cá" or letraFalada == "CA" or letraFalada == "CÁ" or letraFalada == "ka" or letraFalada == "kah" or letrafalada == "KA" or letrafalada == "K":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "l":
                        if letraFalada == "éli" or letraFalada == "ÉLI" or letraFalada == "éle" or letraFalada == "ÉLE":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "m":
                        if letraFalada == "ême" or letraFalada == "Ême" or letraFalada == "ÊME" or letraFalada == "eme" or letraFalada == "Eme" or letrafalada == "EME":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "n":
                        if letraFalada == "êne" or letraFalada == "Êne" or letraFalada == "ÊNE" or letraFalada == "ene" or letraFalada == "Ene" or letrafalada == "ENE":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "o":
                        if letraFalada == "o" or letraFalada == "O" or letraFalada == "oh" or letraFalada == "ó" or letraFalada == "Ó":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "p":
                        if letraFalada == "pe" or letraFalada == "Pe" or letraFalada == "PE" or letraFalada == "pê" or letraFalada == "Pê" or letrafalada == "PÊ":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "q":
                        if letraFalada == "quê" or letraFalada == "Q" or letraFalada == "quÊ" or letraFalada == "Quê" or letraFalada == "QUÊ" or letraFalada == "QUE" or letraFalada == "que":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "r":
                        if letraFalada == "eeri" or letraFalada == "érre" or letraFalada == "erre" or letraFalada == "ERRE" or letraFalada == "Erre" or letraFalada == "Érre" or letrafalada == "ÉRRE":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "s":
                        if letraFalada == "ésse" or letraFalada == "Ésse" or letraFalada == "ÉSSE" or letraFalada == "éssi" or letraFalada == "Éssi" or letraFalada == "ÉSSI":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "t":
                        if letraFalada == "té" or letraFalada == "tê" or letraFalada == "Té" or letraFalada == "Tê" or letraFalada == "TÉ" or letraFalada == "TÊ":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "v":
                        if letraFalada == "vê" or letraFalada == "Vê" or letraFalada == "VÊ" or letraFalada == "ver":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "w":
                        if letraFalada == "dáblio" or letraFalada == "Dáblio" or letraFalada == "DÁblio" or letraFalada == "DÁBLIO":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "y":
                        if letraFalada == "ípsilon" or letraFalada == "Ípsilon" or letraFalada == "ÍPsilon" or letraFalada == "ÍPSILON" or letraFalada == "ipsilon" or letraFalada == "ipsilon" or letraFalada == "IPsilon" or letraFalada == "IPSILON":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    if letraSeg == "z":
                        if letraFalada == "zê" or letraFalada == "Zê" or letraFalada == "ZÊ":
                            pontos += 1
                            acertou(pontos)
                            print("Total : " + pontos)
                    pontos -= 1
                    errou(pontos, letraSegMaisc)
                    print("Total : " + pontos)

            except:
                mixer.init()
                mixer.music.load("hein.mp3")
                mixer.music.play()
                time.sleep(1)

        pygame.display.update()
        clock.tick(60)
Пример #30
0
import nltk
from nltk.stem.lancaster import LancasterStemmer
# import Rpi.GPIO as GPIO
stemmer = LancasterStemmer()

import speech_recognition as sr
import pyttsx3

import numpy
import tensorflow
import json
import tflearn
import random
import pickle

speech = sr.Recognizer()

try:
    engine = pyttsx3.init()

except ImportError:
    print('Requested driver is not found')

except RuntimeError:
    print('Driver failed to initialize')

engine.setProperty('voice', 'english')
engine.setProperty("rate", 150)

with open("/home/rahul/Documents/v2/json file/intents.json") as f:
    data = json.load(f)