# parte do chatbot from chatterbot.trainers import ListTrainer from chatterbot import ChatBot # speech recognition import speech_recognition as sr # speech syntesis import pyttsx3 bot = ChatBot('Black') # inicia bot speak = pyttsx3('sapi5') chats = ['hi', 'oi', 'hello', 'how are you?', 'I' 'm fine.', 'Thanks.'] # conversas bot.set_trainer(ListTrainer) # Define o metodo de treinamento bot.train(chats) # define a lista de conversas r = sr.Recognizer() with sr.Microphone() as s: r.adjust_for_ambient_noise(s) # se adaptar ao ruido while True: print("Say something: ") audio = r.listen(s) # escutar speech = r.recognize_google(audio) # falar print(bot.get_response(speech))
def play(): text = None out_file = r"D:/Projects/Internship/samtest/file_out.wav" rootdir = os.path.join(os.getcwd(), 'samples') attendance_file_path = os.path.join(os.getcwd(), 'Attendance_data\out.csv') def print_data(info): with open(r'\Attendance_data\out.csv', 'rb') as handle: unserialized_data = csv.reader(handle) print(info, unserialized_data) # if data doesn't exist if not os.path.exists(attendance_file_path) and not os.path.isfile( attendance_file_path): if not os.path.exists('Attendance_data'): os.makedirs('Attendance_data') d = { 'Date': [], 'EmpName': [], 'EmpID': [], 'In': [], 'Out': [], 'Duration': [], 'Attendance': [] } df = pd.DataFrame(data=d) print('\nCreating New Attendance DataFrame : ') print(df) df.to_csv(r'Attendance_data\out.csv', index=False) #print_data('Data is created : \n') # compression_opts = dict(method='zip', # archive_name='out.csv') # df.to_csv('out.zip', index=False, # compression=compression_opts) names = [] for subdir, dirs, files in os.walk(rootdir): for dir_name in dirs: names.append(dir_name) class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKCYAN = '\033[96m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' def pyttsx3(text): # obtain voice property voices = engine.getProperty('voices') # voice id 1 is for female and 0 for male engine.setProperty('voice', voices[1].id) # convert to audio and play engine.say(text) engine.runAndWait() print( bcolors.OKGREEN + "\n\nWelcome to Attendance System based on Speaker Recognition.\n\nRules are simple, say your name and roll num and the attendance will be updated.\n" ) pyttsx3( "Welcome to Attendance System based on Speaker Recognition. Rules are simple, say your name and roll num and the attendance will be updated. Warning: Don't try to give proxy" ) print(bcolors.WARNING + "Warning: Don't try to give proxy" + bcolors.ENDC + "\n") audio = pyaudio.PyAudio() FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 CHUNK = 1024 RECORD_SECONDS = 12 # start Recording stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) r = sr.Recognizer() print("Speak something...\n") pyttsx3( "The recording has started, please say Hello ewarn,along with your name and employee ID and if you are signing in or out" ) frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) pyttsx3( "The recording has completed, and now your information will be updated, please be patient and if you feel there is an error kindly contact the adminstrator" ) print("Recording saved\n") # stop Recording stream.stop_stream() stream.close() audio.terminate() waveFile = wave.open(out_file, 'wb') waveFile.setnchannels(CHANNELS) waveFile.setsampwidth(audio.get_sample_size(FORMAT)) waveFile.setframerate(RATE) waveFile.writeframes(b''.join(frames)) waveFile.close() with sr.AudioFile(out_file) as source: #print("Say something!") audio = r.record(source) # read the entire audio file try: # 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)` #print("Did you say? " + r.recognize_google(audio)) text = r.recognize_google(audio) except sr.UnknownValueError: print("eWarn could not understand audio") if "hello" not in text: print("Trigger word missing, Please try again") pyttsx3("Trigger word missing, Please try again") exit(0) # Reproducible results. np.random.seed(123) random.seed(123) # Define the model here. model = DeepSpeakerModel() # Load the checkpoint. model.m.load_weights('Model.h5', by_name=True) mfcc_005 = sample_from_mfcc(read_mfcc(out_file, SAMPLE_RATE), NUM_FRAMES) # Call the model to get the embeddings of shape (1, 512) for each file. predict_005 = model.m.predict(np.expand_dims(mfcc_005, axis=0)) #names = [] select = dict() from statistics import mean for subdir, dirs, files in os.walk(rootdir): for dir_name in dirs: #names.append(dir_name) #print('person dir : ', dir_name) #print('person dir files : \n', os.listdir(os.path.join(rootdir, dir_name))) select_list = list() for file_name in os.listdir(os.path.join(rootdir, dir_name)): #print(file_name) #print('person dir files seperate : \n', os.path.join(rootdir, dir_name, file_name)) mfcc_001 = sample_from_mfcc( read_mfcc(os.path.join(rootdir, dir_name, file_name), SAMPLE_RATE), NUM_FRAMES) predict_001 = model.m.predict(np.expand_dims(mfcc_001, axis=0)) select_list.append( batch_cosine_similarity(predict_005, predict_001)[0]) #print(select_list) select[dir_name] = mean(select_list) select_list.clear() #print('Names : ', names) print('\nPredcitions :', select) Keymax = max(select, key=select.get) if (select[Keymax]) >= 0.5: print('The Speaker is: ', Keymax.split('+')[0]) pyttsx3('The Speaker is ' + str(Keymax.split('+')[0])) time_in = None time_out = None #'EmpName': [], 'EmpID':[], 'In':[], 'Out':[], 'Duration':[], 'Attendance':[]} if text.lower().split().count('in') == 1: #print('text has in', text) time_in = datetime.datetime.now() print("Current time for in:-", time_in) df_in = pd.read_csv(attendance_file_path, parse_dates=['Date']) temp_in = {'Date': datetime.datetime.date(time_in), 'EmpName': Keymax.split('+')[0], 'EmpID': Keymax.split('+')[1], \ 'In': time_in, 'Out': 'zero', 'Duration': 'zero', 'Attendance': 'zero'} temp_df = pd.DataFrame(temp_in, index=[0]) #print("temp_in", temp_in) #print("temp_df", temp_df) if not df_in.empty: print('DataFrame is not empty!') #df_in.append(temp_df, ignore_index = True) print('\n\nIN Before Update\n', df_in) df3 = pd.concat([df_in, temp_df], ignore_index=True) df3.reset_index() df3.to_csv(r'Attendance_data\out.csv', index=False) print('\n\ndf3\n', df3.tail(5)) if df_in.empty: print('DataFrame is empty!') #df_new = pd.DataFrame(temp_in) temp_df.to_csv(r'Attendance_data\out.csv', index=False) print('After IN Update', temp_df) exit(0) if text.lower().split().count('out') == 1: #print('Text has out') df_out = pd.read_csv(attendance_file_path, parse_dates=['Date']) #print(df_out) time_out = datetime.datetime.now() print("Current time for out:-", time_out) in1 = df_out['In'].loc[ (df_out['Date'] == pd.to_datetime( datetime.datetime.date(datetime.datetime.now()))) & (df_out['EmpName'] == Keymax.split('+')[0]) & (df_out['EmpID'] == int(Keymax.split('+')[1]))] #print(in1) df_out['Out'].loc[(df_out['Date'] == pd.to_datetime( datetime.datetime.date(datetime.datetime.now()))) & (df_out['EmpName'] == Keymax.split('+')[0]) & (df_out['EmpID'] == int( Keymax.split('+')[1]))] = time_out out1 = df_out['Out'].loc[ (df_out['Date'] == pd.to_datetime( datetime.datetime.date(datetime.datetime.now()))) & (df_out['EmpName'] == Keymax.split('+')[0]) & (df_out['EmpID'] == int(Keymax.split('+')[1]))] #print(out1) delta = pd.to_datetime(out1) - pd.to_datetime(in1) #print(delta) df_out['Duration'].loc[ (df_out['Date'] == pd.to_datetime( datetime.datetime.date(datetime.datetime.now()))) & (df_out['EmpName'] == Keymax.split('+')[0]) & (df_out['EmpID'] == int(Keymax.split('+')[1]))] = delta day1 = df_out['Attendance'].loc[ (df_out['Date'] == pd.to_datetime( datetime.datetime.date(datetime.datetime.now() - datetime.timedelta(days=1)))) & (df_out['EmpName'] == Keymax.split('+')[0]) & (df_out['EmpID'] == int(Keymax.split('+')[1]))] #print(day1.empty) if day1.empty: df_out['Attendance'].loc[ (df_out['Date'] == pd.to_datetime( datetime.datetime.date(datetime.datetime.now()))) & (df_out['EmpName'] == Keymax.split('+')[0]) & (df_out['EmpID'] == int(Keymax.split('+')[1]))] = 1 else: df_out['Attendance'].loc[ (df_out['Date'] == pd.to_datetime( datetime.datetime.date(datetime.datetime.now()))) & (df_out['EmpName'] == Keymax.split('+')[0]) & (df_out['EmpID'] == int( Keymax.split('+')[1]))] = int(day1[0]) + 1 df_out.to_csv(r'Attendance_data\out.csv', index=False) print(df_out.tail(5)) exit(0) else: print("Don't try to give proxy") pyttsx3("Don't try to give proxy") exit(0)
def audio(text): nltk_tokens = nltk.word_tokenize(text) #print(nltk_tokens) c1 = c2 = c3 = c4 = c5 = c6 = 0 level1 = [ "choose", "define", 'find', 'how', 'label', 'list', 'match', 'name', 'omit', 'recall', 'relate', 'select', 'show', 'spell', 'tell', 'what', 'why', 'when', 'who', 'where', 'which' ] level2 = [ 'classify', 'compare', 'contrast', 'demonstrate', 'explain', 'extend', 'illustrate', 'infer', 'interpret', 'outline', 'relate', 'rephrase', 'show', 'summarize', 'translate' ] level3 = [ 'apply', 'build', 'choose', 'construct', 'develop', 'experiment with', 'identify', 'interview' 'make use of', 'model', 'organize', 'plan', 'select', 'solve', 'utilize' ] level4 = [ 'analyze', 'assume', 'categorize', 'classify', 'compare', 'conclusion', 'contrast', 'discover', 'dissect', 'distinguish', 'divide', 'examine', 'function', 'inference', 'inspect', 'list', 'motive', 'relationships', 'simplify', 'survey', 'take part in', 'test for', 'theme' ] level5 = [ 'agree', 'appraise', 'assess', 'award', 'choose', 'compare', 'conclude', 'criteria', 'criticize', 'decide', 'deduct', 'defend', 'determine', 'disprove', 'dispute', 'estimate', 'evaluate', 'explain', 'importance', 'influence', 'interpret', 'judge', 'justify', 'mark', 'measure', 'opinion', 'perceive', 'prioritize', 'prove', 'rate', 'recommend', 'rule on', 'select', 'support', 'value' ] level6 = [ 'adapt', 'build', 'change', 'choose', 'combine', 'compile', 'compose', 'construct', 'create', 'delete', 'design', 'develop', 'discuss', 'elaborate', 'estimate', 'formulate', 'happen', 'imagine', 'improve', 'invent', 'make up', 'maximize', 'minimize', 'modify', 'original', 'originate', 'plan', 'predict', 'propose', 'solution', 'solve', 'suppose', 'test', 'theory' ] for i in nltk_tokens: if i in level1: c1 += 1 elif i in level2: c2 += 1 elif i in level3: c3 += 1 elif i in level4: c4 += 1 elif i in level5: c5 += 1 elif i in level6: c6 += 1 l1 = [c1, c2, c3, c4, c5, c6] print("\n") print( "The paper has %d questions that come under the category of remember." % c1) print( "The paper has %d questions that come under the category of understand." % c2) print( "The paper has %d questions that come under the category of application." % c3) print( "The paper has %d questions that come under the category of analysis." % c4) print( "The paper has %d questions that come under the category of evaluate." % c5) print( "The paper has %d questions that come under the category of creation." % c6) l2 = sorted(l1) audio_ouput = None if l2[-1] == c1: audio_ouput = "So, the paper is of level 1, you can remember the subject and attempt" if l2[-1] == c2: audio_ouput = "Hence, the paper is of level 2, you have to understand the concept" if l2[-1] == c3: audio_ouput = "So, paper is of level 3, you have to know the application." if l2[-1] == c4: audio_ouput = "Hence, paper is of level 4, you need to be able to analyze the subject" if l2[-1] == c5: audio_ouput = "So,the paper is of level 5, you need to be thorough with the concepts for evaluation." if l2[-1] == c6: audio_ouput = "Hence, paper is of level 6, you can only attempt if you master the subject" pyttsx3(audio_ouput)
elif ((("launch" in x) or ("open" in x) or ("start" in x) or ("execute" in x) or ("run" in x)) and ("map" in x) and (not (("why" in x) or ("tell" in x) or ("what" in x))) and (not (("don't" in x) or ("dont" in x) or ("do not" in x)))): os.system("start bingmaps:") print("Okay! Starting bingmaps:..") pyttsx3.speak("starting bingmaps") elif ((("launch" in x) or ("open" in x) or ("start" in x) or ("execute" in x) or ("run" in x)) and ("message" in x) and (not (("why" in x) or ("tell" in x) or ("what" in x))) and (not (("don't" in x) or ("dont" in x) or ("do not" in x)))): os.system("start ms-chat:") print("Okay! Starting ms-chat:..") pyttsx3("starting ms-chat") elif ((("launch" in x) or ("open" in x) or ("start" in x) or ("execute" in x) or ("run" in x)) and (("microsoft explorer" in x) or ("edge" in x)) and (not (("why" in x) or ("tell" in x) or ("what" in x))) and (not (("don't" in x) or ("dont" in x) or ("do not" in x)))): os.system("start microsoft-edge:") print("Okay! microsoft-edge:..") pyttsx3.speak("starting microsoft-edge") elif ((("launch" in x) or ("open" in x) or ("start" in x) or ("execute" in x) or ("run" in x)) and ("news" in x) and (not (("why" in x) or ("tell" in x) or ("what" in x))) and (not (("don't" in x) or ("dont" in x) or ("do not" in x)))): os.system("start bingnews:")
import pyttsx3 import PyPDF2 import Translator book = open('oop.pdf', 'rb') pdfReader = PyPDF2.PdfFileReader(book) all='' pages = pdfReader.numPages speaker = pyttsx3.init() for num in range(7, pages): page = pdfReader.getPage(num) text = page.extractText() all=text speaker.say(text) speaker.runAndWait() trans=Translator() t= trans.translate(all,src='en',dest='hi') obj=pyttsx3(text=t.text,slow='False',lang='hi') obj.save("audio.mp3")
elif ("open" in p) and ("facebook" in p): pyttsx3.speak("opening facebook.com") os.system("chrome facebook.com") elif ("open" in p) and ("compose" in p) and ("mail" in p): pyttsx3.speak("opening email") os.system("chrome https://mail.google.com/mail/u/0/#inbox?compose=new") elif ("run" in p) and ("zoom" in p): pyttsx3.speak("opening zoom") os.system("zoom") elif ("clear" in p) and ("screen" in p): pyttsx3.speak("screen cleared") os.system("cls") elif ("close" in p) and ("command" in p) and ("prompt" in p): pyttsx3.speak("exiting command terminal") os.system("exit") elif ("exit" in p) or ("quit" in p): print("\t\t Have A Good Day") print("\t\t ~~~~~~~~~~~~~~~") pyttsx3.speak("Have a Good Day") break else: print("\t\t Couldn't find what you are looking for. Try Again!") pyttsx3("Couldn't find what you are looking for. Try Again!")
os.system("start explorer") elif ((("run" in p) or ("execute" in p) or ("open" in p) or ("start" in p)) and (("controlpanel" in p) or ("control panel" in p) or ("cpanel" in p) or ("control" in p))): pyttsx3.speak("Opening Control Panel") os.system("control") elif ((("run" in p) or ("execute" in p) or ("open" in p) or ("start" in p)) and (("cmd" in p) or ("commandprompt" in p) or ("command prompt" in p))): pyttsx3.speak("Opening Command Prompt") os.system("start cmd") elif ((("run" in p) or ("execute" in p) or ("open" in p) or ("start" in p)) and ("calendar" in p)): pyttsx3.speak("Opening Calendar") os.system("start outlookcal:") elif ((("run" in p) or ("execute" in p) or ("open" in p) or ("start" in p) or ("launch" in p)) and (("msedge" in p) or ("microsoft edge" in p))): pyttsx3.speak("Opening Microsoft Edge") os.system("start microsoft-edge:") elif ((("run" in p) or ("execute" in p) or ("open" in p) or ("start" in p) or ("change" in p)) and (("settings" in p) or ("setting" in p))): pyttsx3.speak("Opening Settings") os.system("start ms-settings:") else: pyttsx3.speak("This application is not supported yet!") print("This is not supported till now") pyttsx3("Thank you ! ") print("I hope we were helpful")