0% found this document useful (0 votes)
9 views

Voice M

Uploaded by

kazimuskan569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Voice M

Uploaded by

kazimuskan569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Voice Modulation

Submitted in partial fulfillment of the requirements of the degree


BACHELOR OF ENGINEERING
in
Computer Science and Engineering
(Artificial Intelligence & Machine Learning)

Skill base Lab course: Python Programming

Sem - IV
by
AIML19_Khatre Aditi Anant
AIML67_Kazi Muskan Taufiq

Lokmanya Tilak College of Engineering


Koparkhairne, Navi Mumbai - 400 709
University of Mumbai
(AY 2021-22)
INDEX
No. Title Page No.
1 Introduction 3
2 Abstract 4
3 Source Code 5-16
4 Output 17-18
Introduction:
Over the years, humans have progress in inventing new technologies for
reducing human efforts and saving human life. Since the development of
iot reduce the human labour to nill the development of IoT (Internet of
Things) had been advanced in several study fields like home automation,
personal assistant AI, smart city, smart farming etc. so the personal
assistant help reducing the manual efforts being input by human in their
day-to-day task.

The voice controlled personal assistant receive the voice command as


input to perform the numerous tasks. Any voice command system needs
three basic components which are speech to text converter, query
processor and a text to speech converter. Voice has been a very integral
part of communication nowadays.

Text to speech conversion is the process of converting a machine


recognized text into any language which could be identified by a speaker
when the text is read out loud. It is two step processes which is divided
into front end and back end. First part is responsible for converting
numbers and abbreviations to a written word format. This is also referred
to as normalization of text. Second part involves the signal to be
processed into an understandable one. Speech Recognition is the ability
of machine for instance a computer to understand words and sentences
spoken in any language. These words or sentences are then converted to
a format that could be understood by the machine.
ABSTRACT:

Voice control is a major growing feature that change the way


people can live. The voice assistant is commonly being used in
smartphones and laptops. AI-based Voice assistants are the
operating systems that can recognize human voice and respond
via integrated voices.
This voice assistant will gather the audio from the microphone
and then convert that into text, later it is sent through GTTS
(Google text to speech). GTTS engine will convert text into audio
file in English language, then that audio is played using play
sound package of python programming Language. The voice
assistant will greet the user according to the time and will respond
to the user’s basic commands such as searching or opening a
window on the web browser, for example, YouTube, Google,
Reddit, etc.
CODE:
import datetime
import pyttsx3
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import smtplib
import random
import pyautogui
import pyowm
import requests
import wolframalpha

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

person = {"mezhu": "[email protected]", "rohit":


"[email protected]", "anushka": "[email protected]"}

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

def wishme():
hour = int(datetime.datetime.now().hour)
if hour > 0 and hour <=12:
speak("Good Morning!")
elif hour > 12 and hour < 18:
speak("Good Afternoon!")
elif hour > 18 and hour < 21:
speak("Good Evening!")
else:
speak("Good Night!")
speak("I am Assistant. How may I help you ?")

def Command():
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("You said: ", query, "\n")

except Exception as e:
print("Say that again...")
speak("Say that again...")
return "none"
return query

def sendEmail(to, content):


try:
from1 = "[email protected]"
f = open("psd.txt", "r")
pwd = f.read()
f.close()
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(from1, pwd)
server.sendmail(from1, to, content)
server.close()

except Exception as e:
print(e)
if _name_ == '_main_':
wishme()
while True:
query = Command().lower()

if 'who are you' in query:


print("I am Your Voice Assistant")
speak("I am Your Voice Assistant")

elif 'how are you' in query:


print("I am fine")
speak("I am fine")

elif 'how old are you' in query:


print("I am 10 days old")
speak("I am 10 days old")

elif 'your name' in query:


print("My name is Jarvis")
speak("My name is Jarvis")

elif 'tell me about yourself' in query:


print("Hello, myself Jarvis!! I am your voice assistant. I can perform several
task. How may I help you?")
speak("Hello, myself Jarvis!! I am your voice assistant. I can perform several
task. How may I help you?")

elif 'wikipedia' in query:


print("Searching Wikipedia...")
speak("Searching Wikipedia...")
query = query.replace('wikipedia', "")
result = wikipedia.summary(query, sentences=2)
print("According to Wikipedia...")
speak("According to Wikipedia...")
print(result)
speak(result)

elif 'open google chrome' in query:


speak("opening chrome")
pyPath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
os.startfile(pyPath)

elif 'open google' in query:


speak("opening google")
webbrowser.open("www.google.com")

elif 'open youtube' in query:


speak("opening youtube")
webbrowser.open("www.youtube.com")

elif 'open gmail' in query:


webbrowser.open("https://mail.google.com/mail/u/0/?tab=rm&ogbl#inbox")

elif 'screenshot' in query:


print("How you want to take screenshot? Full Screen or Specific Screen")
speak("How you want to take screenshot? Full Screen or Specific Screen")
ans = Command().lower()
print("taking screenshot")
speak("taking screenshot")

if 'full screen' in ans:


pyautogui.hotkey('win', 'prtsc')
else:
pyautogui.hotkey('win', 'shift', 's')

elif 'search' in query:


speak("searching")
query = query.replace("search", "")
taburl = "http://google.com/?#q="
webbrowser.open(taburl + query)

elif 'tell me about' in query:


speak("searching")
query = query.replace("tell me about ", "")
taburl = "http://google.com/?#q="
webbrowser.open(taburl + query)
elif 'how to reach' in query:
location = query.replace("how to reach ", "")
taburl = "http://google.com/?#q="
webbrowser.open(taburl + location)

elif 'how to' in query:


speak("searching")
taburl = "http://google.com/?#q="
webbrowser.open(taburl + query)

elif 'play music' in query:


print("Playing Music")
speak("Playing Music")
music_dir = "C:\\Users\\MEHZABEEN\\OneDrive\\Desktop\\M_music"
songs = os.listdir(music_dir)
end = int(len(songs))
num = random.randint(0, end-1)
print(num)
a=0
for i in songs:
print(a, i)
a += 1
os.startfile(os.path.join(music_dir, songs[num]))

elif 'song' in query:


print("Playing Music")
speak("Playing Music")
music_dir = "C:\\User\\MEHZABEEN\\OneDrive\\Desktop\\M_music"
songs = os.listdir(music_dir)
end = int(len(songs))
num = random.randint(0, end - 1)
print(num)
a=0
for i in songs:
print(a, i)
a += 1
os.startfile(os.path.join(music_dir, songs[num]))

elif 'open calendar' in query:


print("opening calendar")
speak("opening calendar")

os.startfile("C:\\Users\\MEHZABEEN\\AppData\\Roaming\\Microsoft\\Windows\\Start
Menu\\Programs\\Chrome Apps")

elif 'show calendar' in query:


print("opening calendar")
speak("opening calendar")

os.startfile("C:\\Users\\MEHZABEEN\\AppData\\Roaming\\Microsoft\\Windows\\Start
Menu\\Programs\\Chrome Apps")

elif 'open notepad' in query:


print("opening notepad")
speak("opening notepad")
os.startfile("C:\\WINDOWS\\system32\\notepad.exe")

elif "open settings" in query:


print("opening settings")
speak("opening settings")
os.startfile("C:\\Users\\MEHZABEEN\\Desktop\\Settings - Shortcut")

elif 'play' in query:


taburl = "https://www.youtube.com/results?search_query="
query = query.replace("play ", "")
searchTerm = query.replace(" ", "+")
webbrowser.open(taburl+searchTerm)

elif 'the time' in query:


strTime = datetime.datetime.now().strftime("%H:%M:%S")
print(strTime)
speak(f"The time is {strTime}")

elif 'the date' in query:


strDate = datetime.date.today().strftime("%d/%m/%Y")
print(strDate)
speak(f"Today's date is {strDate}")
elif "today's date" in query:
strDate = datetime.date.today().strftime("%d/%m/%Y")
print(strDate)
speak(f"Today's date is {strDate}")

elif 'the day' in query:


day = datetime.datetime.today().strftime("%A")
print(f"Today is {day}")
speak(f"Today is {day}")

elif 'send email to mehzu' in query:


try:
print("What should I send?")
speak("What should I send?")
content = Command()
to = person["mehzu"]
print("Are you sure?")
speak("Are you sure?")
ans = Command().lower()
if 'yes' in ans:
sendEmail(to, content)
print("Email has been sent")
speak("Email has been sent")
elif 'no' in ans:
print("Email discarded")
speak("Email discarded")
else:
print("Couldn't understand")
speak("Couldn't understand")
except Exception as e:
print(e)

elif 'send email to rohit' in query:


try:
print("What should I send?")
speak("What should I send?")
content = Command()
to = person["rohit"]
print("Are you sure?")
speak("Are you sure?")
ans = Command().lower()
if 'yes' in ans:
sendEmail(to, content)
print("Email has been sent")
speak("Email has been sent")
elif 'no' in ans:
print("Email discarded")
speak("Email discarded")
else:
print("Couldn't understand")
speak("Couldn't understand")
except Exception as e:
print(e)

elif 'send email to anushka' in query:


try:
print("What should I send?")
speak("What should I send?")
content = Command()
to = person["anushka"]
print("Are you sure?")
speak("Are you sure?")
ans = Command().lower()
if 'yes' in ans:
sendEmail(to, content)
print("Email has been sent")
speak("Email has been sent")
elif 'no' in ans:
print("Email discarded")
speak("Email discarded")
else:
print("Couldn't understand")
speak("Couldn't understand")
except Exception as e:
print(e)

elif 'send an email' in query:


try:
print("To whom do i send? Please provide email id")
speak("To whom do i send? Please provide email id")
people = Command()
to = people.replace(" ", "")
print("What should I send?")
speak("What should I send?")
content = Command()
print("Are you sure?")
speak("Are you sure?")
ans = Command().lower()
if 'yes' in ans:
sendEmail(to, content)
print("Email has been sent")
speak("Email has been sent")
elif 'no' in ans:
print("Email discarded")
speak("Email discarded")
else:
print("Couldn't understand")
speak("Couldn't understand")
except Exception as e:
print(e)

elif 'send email to' in query:


try:
print("What should I send?")
speak("What should I send?")
content = Command()
people = query.replace("send email to ", "")
to = people.replace(" ", "")
print("Are you sure?")
speak("Are you sure?")
ans = Command().lower()
if 'yes' in ans:
sendEmail(to, content)
print("Email has been sent")
speak("Email has been sent")
elif 'no' in ans:
print("Email discarded")
speak("Email discarded")
else:
print("Couldn't understand")
speak("Couldn't understand")
except Exception as e:
print(e)

elif 'email' in query:


try:
print("To whom do i send? Please provide email id")
speak("To whom do i send? Please provide email id")
people = Command()
to = people.replace(" ", "")
print("What should I send?")
speak("What should I send?")
content = Command()
print("Are you sure?")
speak("Are you sure?")
ans = Command().lower()
if 'yes' in ans:
sendEmail(to, content)
print("Email has been sent")
speak("Email has been sent")
elif 'no' in ans:
print("Email discarded")
speak("Email discarded")
else:
print("Couldn't understand")
speak("Couldn't understand")
except Exception as e:
print(e)

elif 'open calculator' in query:


cal_directory = "C:\\Windows\\System32\\calc.exe"
os.startfile(cal_directory)

elif 'open camera' in query:


print("opening camera")
speak("opening camera")
os.startfile("microsoft.windows.camera:")

elif 'open this pc' in query:


print("opening this pc")
speak("opening this pc")
os.startfile("C:\\Users\\hp\\Desktop\\This PC - Shortcut")

elif 'open mail' in query:


print("opening mail")
speak("opening mail")
os.startfile("gmail.google.com")

elif "weather" in query:


owm = pyowm.OWM(API_key="5929ea003f0f3f64dfdbb73dc8a4ddfc")
ob = owm.weather_at_place("Mumbai, IN")
w = ob.get_weather()
humidity = w.get_humidity()

def weather_data(query):
res = requests.get('http://api.openweathermap.org/data/2.5/weather?' +
query + '&APPID=b35975e18dc93725acb092f7272cc6b8&units=metric')
return res.json()

def print_weather(result, city):


print("{}'s Temperature is {}°C ".format(city, result['main']['temp']))
speak("{}'s temperature is {}°Celsius ".format(city, result['main']['temp']))
print("Wind speed is {} m/s".format(result['wind']['speed']))
speak("Wind speed: {} meter per second".format(result['wind']['speed']))
print("Description is {}".format(result['weather'][0]['description']))
speak("Description is {}".format(result['weather'][0]['description']))
print("Weather is {}".format(result['weather'][0]['main']))
speak("Weather is {}".format(result['weather'][0]['main']))
print(f"Humidity is {humidity} percentage")
speak(f"humidity is {humidity} percentage")

def main():
city = "Mumbai"
print()
try:
query = 'q=' + city;
w_data = weather_data(query)
print_weather(w_data, city)
print()
except:
print('City name not found...')
speak('City name not found...')
if _name_ == 'main':
main()

elif 'bye' in query:


speak("bye bye!!Have a nice day")
break

elif 'exit' in query:


speak("bye bye!!Have a nice day")
break

elif query != 'none':


try:
client = wolframalpha.Client('EGU8HX-YQ32EJWTV4');
res = client.query(query)
output = next(res.results).text
print(f'Answer is {output}')
speak(f'Answer is {output}')
except Exception as e:
print("Sorry I can't do that")
speak("Sorry I can't do that")
OUTPUT:
As you can see here when we asked the voice assistant what’s the time
and date, it replied with the current time and date.
When the command ‘open calculator’ was given to voice assistant it
responded by opening the calculator as shown below.

You might also like