class NLP: def __init__(self, audio_stream): self.speech_recognizer = SpeechRecognizer(audio_stream) self.natural_language_processor = NaturalLanguageProcessor() def run(self): self.speech_recognizer.run()
def listen_commands(self): text = "How I can help You." speech = Speech() speech.create_voice(text) r = SpeechRecognizer() command = (r.recognize()).lower() print 'Recognized command: {0}'.format(command) if 'weatcher' in command: self.command_weatcher() if 'autopilot' in command: self.command_autopilot() if 'dance' in command: self.command_dance() if 'exterminate' in command: self.command_exterminate()
def __init__(self, language = "en_GB"): dialog_context_xml_path=os.path.join(os.path.dirname(__file__), '../../homodeus_external/xml_folder/dialog_context.xml') self.dialog_context = ET.parse(dialog_context_xml_path).getroot() self.language = language # initialisation of global class variables self.selected_context = "" self.repeat = 0 self.relevant_info = "" self.reach_deadline = False self.timer = None self.dialoguing_now = False self.dialog_state = Dialog_state.begin # the code works differently if it's run on the robot or not param_name = rospy.search_param('on_robot') self.on_robot = rospy.get_param(param_name,False) # Goal input self.input_bhvr_goal_context = rospy.Subscriber("/bhvr_input_goal_dialContext",data_class=String, callback=self.set_context_Cb,queue_size=10) # Output self.output_bhvr_result = rospy.Publisher("/bhvr_output_res_dialBool", Bool, queue_size=10) self.output_bhvr_relevant = rospy.Publisher("/bhvr_output_res_dialRelevant", String, queue_size=10) # dialog only use tts_server when it's run on the robot if self.on_robot: self.connect_to_tts_server() self.speech_recognizer = sr.SpeechRecognizer()
def __init__(self, language='en-US'): """ This method initializes the perception module by initializing the output topic and the Speech_recognition object Arguments --------- language : string The language used for the speech recognition. By default its value is set to american english. """ #The output of the module self.output_perc = rospy.Publisher("/proc_output_listenText", String, queue_size=10) self.language = language self.speech_recognizer = sr.SpeechRecognizer()
def search_by_voice(user_email, name): text = SpeechRecognizer.get_instance().record_and_decipher_audio() while True: if text is None: break else: search = Search.get_instance() while True: # Ask user for input print("Please enter a book's title, author, or ISBN ") print("Leave blank and press Enter to view all books") results = search.search_books(text.strip()) if "message" in results[0].keys(): print("No matches! Returning to main menu") time.sleep(3) break else: search.print_results(results) try: print("Choose an option") # Provide user with options to keep going or return to main menu choice = int(input( "1. Borrow a book" + "\n" "2. Return to main menu" + "\n" "Your choice: " ).strip()) if choice == 1: try: book_id = int(input("Enter the ID of the book you'd like to borrow: ").strip()) book = search.check_book_exist(book_id) if book is None: print("Book ID does not exist! Returning to main menu...") time.sleep(3) break else: search.confirmation(book, user_email, name) except ValueError as val: print(val) else: print("Returning to main menu...") time.sleep(2) break except Exception as e: print(e)
def __init__(self): Follwer.__init__(self) self.state = State.NORMAL self.recognizer = SpeechRecognizer() self.follow(self.recognizer) self.answer = str() self.recommend_packages = [] self.interesting_package = None Speaker.speak('Please tell your phone number.') number = raw_input('Phone number : ') self.current_user = Database.find_one('customers', 'phone_number', number) self.user_account = Database.find_user_account(self.current_user.id) # If phone no is exist, Greeting if self.current_user is not None: Speaker.speak('Good morning mister %s. How can i help you.' % self.current_user.fullname()) self.recognizer.start() # Otherwise, Goodbye else: Speaker.speak('Sorry. Your number is not in our system.')
def listen_text(self): r = SpeechRecognizer() text = (r.recognize()).lower() print 'Recognized text: {0}'.format(text) if 'shadow' in text: self.listen_commands()
def get_mean_of_smiles(self): ret = 0 for face in self.faces: ret += int(face.is_smiling) return ret/(len(self.faces)+1e-6) if __name__ == '__main__': word_analyze = WordAnalyze() capture = cv2.VideoCapture(0) face_recognizer = FaceRecognizer(capture=capture) speech_recognizer = SpeechRecognizer() speech_recognizer.start() w=int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH )) h=int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT )) fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') graph_drawer = GraphDrawer() graph_drawer.start() all_omorosa = OmoroiData() all_graph = Graph(color=(1.0,0.0,1.0),ylim=[all_omorosa.omoroi_min-1.0,all_omorosa.omoroi_max+1.0],ylabel="Omorosa") #if os.path.exists('movie.avi'): # os.remove('movie.avi') #out = cv2.VideoWriter('movie.avi',fourcc,7.5,(w,h))
class Callcenter(Follwer): def __init__(self): Follwer.__init__(self) self.state = State.NORMAL self.recognizer = SpeechRecognizer() self.follow(self.recognizer) self.answer = str() self.recommend_packages = [] self.interesting_package = None Speaker.speak('Please tell your phone number.') number = raw_input('Phone number : ') self.current_user = Database.find_one('customers', 'phone_number', number) self.user_account = Database.find_user_account(self.current_user.id) # If phone no is exist, Greeting if self.current_user is not None: Speaker.speak('Good morning mister %s. How can i help you.' % self.current_user.fullname()) self.recognizer.start() # Otherwise, Goodbye else: Speaker.speak('Sorry. Your number is not in our system.') def receive(self, msg): self.recognizer.stop() # Pause Speech recognizer print('Callcenter receive msg : ' + msg) if KeywordProcessing.contains_keyword('exit', msg): self.answer = str('Do you want to exit.') self.state = State.EXIT_CONFIRM elif KeywordProcessing.contains_keyword('reset', msg): self.answer = str('How can i help you.') self.state = State.NORMAL elif KeywordProcessing.contains_keyword('operator', msg): self.answer = str('Do you want to talk to operator.') self.state = State.TALK_TO_OPERATOR elif self.state == State.NORMAL: # Package category if KeywordProcessing.contains_keyword('package', msg): # Recommend package if KeywordProcessing.contains_keyword('package_category', msg): package_category = [c for c in KeywordProcessing.keywords['package_category'] if c in msg][0] # Find interesting packages if self.user_account.package.payment == 'prepaid': self.recommend_packages = Database.find_list('prepaids', 'type', package_category) else: self.recommend_packages = Database.find_list('postpaids', 'type', package_category) # Generate answer self.answer = str('We recommend %d packages for you. ' % len(self.recommend_packages)) index = 1 for package in self.recommend_packages: self.answer += str('package number %d %s . ' % (index, package.name)) index += 1 self.answer += str('Which one you want to have a detail. Or you want to listen again.') self.state = State.SELECT_PACKAGE # Change state # Current package else: self.answer = str('Your current package is %s' % self.user_account.package.description()) self.state = State.REPEAT # Change state # How to elif KeywordProcessing.contains_keyword('how_to', msg): if KeywordProcessing.contains_keyword('topup', msg): self.answer = str('Please select your topup method. Online or phone.') self.state = State.SELECT_TOPUP_METHOD elif KeywordProcessing.contains_keyword('pay', msg): self.answer = str('Please select your payment method. Online or phone.') self.state = State.SELECT_PAYMENT_METHOD elif KeywordProcessing.contains_keyword('setup', msg): self.answer = str('What is your operating system.') self.state = State.SELECT_PHONE_OS # Bill category elif KeywordProcessing.contains_keyword('balance', msg) or KeywordProcessing.contains_keyword('expire', msg): if self.user_account.package.payment == 'postpaid': self.answer = str('Sorry, I don\'t understand your question') elif KeywordProcessing.contains_keyword('balance', msg): self.answer = self.user_account.report() self.state = State.REPEAT elif KeywordProcessing.contains_keyword('expire', msg): self.answer = str('Your account will expire on %s %s %s.' % (self.user_account.expiration_date.strftime('%d'), self.user_account.expiration_date.strftime('%B'), self.user_account.expiration_date.strftime('%Y'))) self.state = State.REPEAT elif KeywordProcessing.contains_keyword('bill', msg): if self.user_account.package.payment == 'postpaid': if KeywordProcessing.contains_keyword('unpaid', msg): all_bills = Database.find_list('bills', 'customer', ObjectId(self.current_user.id)) unpaid_bills = [bill for bill in all_bills if not bill.paid] if(len(unpaid_bills) > 0): self.answer = str('You have %d bills unpaid.' % len(unpaid_bills)) else: self.answer = str('You don\' have any unpaid bill.') elif KeywordProcessing.contains_keyword('how_to', msg) and KeywordProcessing.contains_keyword('pay', msg): self.answer = str('Please select your payment method. Online or phone.') self.state = State.SELECT_PAYMENT_METHOD elif KeywordProcessing.contains_keyword('when', msg): self.answer = str('Your bill is due on %d %s.' % (self.user_account.payment_date.strftime('%d'), self.user_account.payment_date.strftime('%B'))) else: self.answer = self.user_account.report() self.state = State.REPEAT else: self.answer = str('Sorry, I don\'t understand your question') # Troubleshooting elif KeywordProcessing.contains_keyword('cannot', msg): # Prepaid user if self.user_account.package.payment == 'prepaid': if KeywordProcessing.contains_keyword('internet', msg): if not self.user_account.package.is_internet_avail(): self.answer = str('Sorry, your current package is not support internet using.') else: self.answer = str('Sorry, i can\'t find any problem in your account. Do you want to talk to the operator.') self.state = State.TALK_TO_OPERATOR elif KeywordProcessing.contains_keyword('call', msg): if self.user_account.balance <= self.user_account.package.internal_calling_rate: self.answer = str('Sorry, your current balance is not enough.') else: self.answer = str('Sorry, i can\'t find any problem in your account. Do you want to talk to the operator.') self.state = State.TALK_TO_OPERATOR #Postpaid user else: if KeywordProcessing.contains_keyword('internet', msg): if not self.user_account.package.is_internet_avail(): self.answer = str('Sorry, your current package is not support internet using.') elif len([bill for bill in Database.find_list('bills', 'customer', self.current_user.id) if not bill.paid]) > 3: self.answer = str('Sorry, your account was suspended because you haven\'t pay monthly fee.') else: self.answer = str('Sorry, i can\'t find any problem in your account. Do you want to talk to the operator.') self.state = State.TALK_TO_OPERATOR elif KeywordProcessing.contains_keyword('call', msg): if self.user_account.package.payment == 'postpaid' and len([bill for bill in Database.find_list('bills', 'customer', self.current_user.id) if not bill.paid]) > 3: self.answer = str('Sorry, your account was suspended because you haven\'t pay monthly fee.') else: self.answer = str('Sorry, i can\'t find any problem in your account. Do you want to talk to the operator.') self.state = State.TALK_TO_OPERATOR elif self.state == State.REPEAT: # Repeat if KeywordProcessing.contains_keyword('confirm', msg): pass # Back to normal elif KeywordProcessing.contains_keyword('cancel', msg): self.answer = 'How can i help you.' self.state = State.NORMAL elif self.state == State.SELECT_PACKAGE: # Repeat if KeywordProcessing.contains_keyword('confirm', msg): pass elif KeywordProcessing.contains_keyword('cancel', msg): self.answer = 'How can i help you.' self.state = State.NORMAL # Explain user's interesting package detail elif KeywordProcessing.contains_keyword('ordinal', msg) and KeywordProcessing.get_index(msg) - 1 < len(self.recommend_packages): index = KeywordProcessing.get_index(msg) - 1 self.interesting_package = self.recommend_packages[index] self.answer = str(self.interesting_package.description()) self.answer += str(' Do you want to use this package.') self.state = State.CHANGE_PACKAGE else: self.answer = str('Sorry, i don\'t understand.') elif self.state == State.CHANGE_PACKAGE: if KeywordProcessing.contains_keyword('confirm', msg): if self.user_account.package.payment == 'prepaid': # Change package of account Database.update('accounts', 'customer', self.current_user.id, 'package', self.interesting_package.id) elif self.user_account.package.payment == 'postpaid': # Create new bill new_bill = Bill(self.current_user, self.interesting_package, 0, 0, 0, 0, datetime.datetime.now(), 0) Database.insert('bills', new_bill.tojson()) self.answer = 'package changing is complete.' # Get new user account self.user_account = Database.find_user_account(self.current_user.id) self.state = State.NORMAL elif KeywordProcessing.contains_keyword('cancel', msg): self.answer = 'How can i help you.' self.state = State.NORMAL else: self.answer = str('Sorry, i don\'t understand.') elif self.state == State.SELECT_PHONE_OS: if KeywordProcessing.contains_keyword('phone_os', msg): self.answer = Database.find_one('setups', 'os', msg).setup self.state = State.REPEAT else: self.answer = str('Sorry, we cannot find your operating system.') elif self.state == State.SELECT_PAYMENT_METHOD: if KeywordProcessing.contains_keyword('payment_method', msg): self.answer = Database.find_one('how_to_pays', 'method', msg).step self.state = State.REPEAT else: self.answer = str('Sorry, your payment method is not available.') elif self.state == State.SELECT_TOPUP_METHOD: if KeywordProcessing.contains_keyword('payment_method', msg): self.answer = Database.find_one('how_to_topups', 'method', msg).step self.state = State.REPEAT else: self.answer = str('Sorry, your payment method is not available.') elif self.state == State.TALK_TO_OPERATOR: if KeywordProcessing.contains_keyword('confirm', msg): self.answer = str('Transfer to operator. please wait for seconds.') elif KeywordProcessing.contains_keyword('cancel', msg): self.answer = str('How can i help you.') self.state = State.NORMAL elif self.state == State.EXIT_CONFIRM: if KeywordProcessing.contains_keyword('confirm', msg): self.answer = str('Thank you for using our service') self.state = State.EXIT self.recognizer.shutdown() elif KeywordProcessing.contains_keyword('cancel', msg): self.answer = str('How can i help you') self.state = State.NORMAL if self.answer is not None: Speaker.speak(self.answer) if self.state == State.REPEAT: Speaker.speak('Do you want to listen again.') self.recognizer.resume() # Resume Speech recognizer
import os import subprocess import time import logging import uuid from speech_recognizer import SpeechRecognizer from punctuator import Punctuator from number_utils.text2numbers import TextToNumbers speech_recognizer = SpeechRecognizer() punctuator = Punctuator(model_path="data/punctuator") text2numbers = TextToNumbers() class FileHandler: @staticmethod def get_recognized_text(blob): try: filename = str(uuid.uuid4()) os.makedirs('./records', exist_ok=True) new_record_path = os.path.join('./records', filename + '.webm') blob.save(new_record_path) new_filename = filename + '.wav' converted_record_path = FileHandler.convert_to_wav( new_record_path, new_filename) response_models_result = FileHandler.get_models_result( converted_record_path) return 0, new_filename, response_models_result except Exception as e: logging.exception(e) return 1, None, str(e)