def validate_command(self, command): if command.lower() in GLO_CMD.values(): detected_command_id = GET_COMMAND(command.lower()) Logger.info("GLO_CMD command available -> {0}".format(command.lower())) self.callback_command_detected(detected_command_id) else: Logger.info("Detected command: {0}".format(command.lower()))
def stop_authorization(self): self.auth.stop() self.api_window.stop_pulse_text() Logger.debug("Stop authorization process") if self.authorization_complete: self.speaker.say("Witaj w inteligentnym lustrze {0}".format(self.authorized_user_name)) self.api_window.user_view(name=self.authorized_user_name, display=True)
def __init__(self, parent): Frame.__init__(self, parent, bg=ApiSettings.Background) self.news_label = Label(self, text='Wiadomości :', font=(ApiSettings.Font, ApiSettings.MediumTextSize), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.news_label.pack(side=TOP, anchor=W) self.news_list = None self.news_url = "https://news.google.com/rss?hl=pl&gl=PL&ceid=PL:pl" self.headlines_container = Frame(self, bg=ApiSettings.Background) self.headlines_container.pack(side=TOP) self.display_news_number = 5 self.headlines_iterator = 0 self.headlines_label_list = [ NewsHeadline(self.headlines_container) for i in range(self.display_news_number) ] for headline in self.headlines_label_list: headline.pack(side=TOP, anchor=W) Logger.debug("Initialization of News class") self.get_headlines() self.refresh_headlines()
def add_detected_face(self, name): Logger.debug("Detected {0}".format(name)) if name in self.detected: self.detected[name] += 1 else: self.detected[name] = 1 self.recognition_confidence()
def say(self, text): try: Logger.info("Speaker say : {0}".format(text)) self.engine_speak.say(text) self.engine_speak.runAndWait() except Exception as err: Logger.error("Speaker error : {0}".format(err))
def _api_recognition(self, audio): if self.api_option is ApiOption.GOOGLE: return self.recognizer.recognize_google(audio, language=self.language) elif self.api_option is ApiOption.GOOGLE_CLOUD: # Support languages: https://cloud.google.com/speech-to-text/docs/languages return self.recognizer.recognize_google_cloud(audio, credentials_json='', language=self.language) elif self.api_option is ApiOption.SPHINX: # Support languages : https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/ return self.recognizer.recognize_sphinx(audio, language=self.language) elif self.api_option is ApiOption.WIT: # Support languages : https://wit.ai/faq, login required return self.recognizer.recognize_wit(audio, key='',) elif self.api_option is ApiOption.AZURE_BING: # Support languages : https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/language-support, login required self.recognizer.recognize_bing(audio, key='', language=self.language) elif self.api_option is ApiOption.LEX: # Support languages: ONLY ENG -> https://docs.aws.amazon.com/lex/latest/dg/gl-limits.html return self.recognizer.recognize_lex(audio) elif self.api_option is ApiOption.HOUNDIFY: # Support languages: ONLY ENG, login required return self.recognizer.recognize_houndify(audio, client_id='', client_key='') elif self.api_option is ApiOption.IBM: # Support languages : https://www.ibm.com/watson/services/language-translator/, login required return self.recognizer.recognize_ibm(audio, username='', password='', language=self.language) else: Logger.error("Api recognition option is not defined")
def recognition_confidence(self): Logger.debug("Authorization confidence {0}".format(self.detected)) if self.samples_confidence in self.detected.values(): Logger.debug("Authorization confidence {0}".format(self.samples_confidence)) self.authorization_process_running = False for name, confidence in self.detected.items(): if self.samples_confidence == confidence: self.callback_authorized_user(name)
def run_api_window(self): if self.api_window.api_runs: self.api_window.refresh() else: self.close_thread = True self.quit_authorization = True Logger.debug("Close thread : \"{0}\"".format(GET_MESSAGE(self.api_window.api_info))) self.MessagesHandler.send_message(self.api_window.api_info)
def get_message(self): if not self.__messages_queue.empty(): message_id = self.__messages_queue.get() Logger.debug("Received : {0}".format(GET_MESSAGE(message_id))) self.__messages_queue.task_done() self.__messages_locker.release() return message_id return None
def run(self): Logger.info("User_Interface thread runs") self.init_window() self.init_camera() self.run_authorization() while not self.close_thread: self.run_api_window() self.run_messages_handler() Logger.info("User_Interface thread ends")
def get_headlines(self): try: self.news_list = feedparser.parse(self.news_url) Logger.info("Get number of news: {0} from: {1}".format( len(self.news_list), self.news_url)) except Exception as err: Logger.critical("News exception: {0}".format(err)) # updates every 5 minutes self.after(5 * 60 * 1000, self.get_headlines)
def run_authorization(self): if self.camera.api_runs: self.start_authorization() while not (self.authorization_complete or self.quit_authorization): self.run_api_window() self.stop_authorization() self.network_connection() else: Logger.error("Authorization process will not start when camera is not connected") self.api_window.start_pulse_text("Wymagana \nautoryzacja")
def __init__(self, messages_handler): Thread.__init__(self, name="UC_Thread") self.MessagesHandler = messages_handler self.close_thread = False self.command_recognition = None self.network_enabled = False self.window_enabled = False Logger.debug("Initialization of User Command Thread class")
def __init__(self, parent): Frame.__init__(self, parent, bg=ApiSettings.Background) self.resizeX = 20 self.resizeY = 20 self.connections_container = Frame(self, bg=ApiSettings.Background) self.connections_container.pack(side=RIGHT, anchor=SE) """ Loads menu bar (enable/disable) icons """ wifi_disable_image = Image.open(icons["wifi_disabled"]) wifi_enable_image = Image.open(icons["wifi_enabled"]) self.wifi_disable_icon = ImageTk.PhotoImage( wifi_disable_image.resize((self.resizeX, self.resizeY), Image.ANTIALIAS).convert('RGB')) self.wifi_enable_icon = ImageTk.PhotoImage( wifi_enable_image.resize((self.resizeX, self.resizeY), Image.ANTIALIAS).convert('RGB')) camera_disable_image = Image.open(icons["camera_disabled"]) camera_enable_image = Image.open(icons["camera_enabled"]) self.camera_disable_icon = ImageTk.PhotoImage( camera_disable_image.resize((self.resizeX, self.resizeY), Image.ANTIALIAS).convert('RGB')) self.camera_enable_icon = ImageTk.PhotoImage( camera_enable_image.resize((self.resizeX, self.resizeY), Image.ANTIALIAS).convert('RGB')) microphone_disable_image = Image.open(icons["microphone_disabled"]) microphone_enable_image = Image.open(icons["microphone_enabled"]) self.microphone_disable_icon = ImageTk.PhotoImage( microphone_disable_image.resize((self.resizeX, self.resizeY), Image.ANTIALIAS).convert('RGB')) self.microphone_enable_icon = ImageTk.PhotoImage( microphone_enable_image.resize((self.resizeX, self.resizeY), Image.ANTIALIAS).convert('RGB')) """ Creates label of each menu icon """ self.wifi_label = Label(self.connections_container, bg=ApiSettings.Background, image=self.wifi_disable_icon) self.wifi_label.pack(side=RIGHT, anchor=CENTER) self.camera_label = Label(self.connections_container, bg=ApiSettings.Background, image=self.camera_disable_icon) self.camera_label.pack(side=RIGHT, anchor=CENTER) self.microphone_label = Label(self.connections_container, bg=ApiSettings.Background, image=self.microphone_disable_icon) self.microphone_label.pack(side=RIGHT, anchor=CENTER) Logger.debug("Initialization of Connections class")
def get_location(): try: location_req_url = "http://ip-api.com/json/{0}".format(Network.get_ip()) response = requests.get(location_req_url) location_json = json.loads(response.text) Logger.debug("request: " + str(location_req_url) + " response: " + str(response) + " json: " + str(location_json)) return location_json except Exception as err: Logger.critical("Exception: {0}".format(err)) return None
def wait_for_ui_initialization(self): Logger.info("Waiting for initialization network and window") def ui_initialized(): return self.window_enabled and self.network_enabled while not (ui_initialized() or self.close_thread): self.run_messages_handler() if ui_initialized(): self.init_command_recognition()
def get_ip(): try: ip_reg_url = "http://jsonip.com/" response = requests.get(ip_reg_url) ip_json = json.loads(response.text) Logger.debug("request: " + str(ip_reg_url) + " response: " + str(response) + " json: " + str(ip_json)) return ip_json['ip'] except Exception as err: Logger.critical("Exception: {0}".format(err)) return None
def __init__(self): try: self.engine_speak = pyttsx3.init() self.engine_speak.setProperty('rate', 130) self.engine_speak.setProperty('volume', 1) voices = self.engine_speak.getProperty('voices') self.engine_speak.setProperty('voice', voices[0].id) Logger.debug("Speaker Class initialized correctly") except Exception as err: Logger.error("Speaker initialization error : {0}".format(err))
def __init__(self): super().__init__() """ Initialization of Tkinter api window """ self.tk = Tk() self.tk.title("Smart Mirror") self.tk.configure(background=ApiSettings.Background) self.api_full_screen = True self.tk.attributes("-fullscreen", self.api_full_screen) self.tk.bind("q", self.quit_api) self.tk.bind("f", self.full_screen) """ Initialization of frames layout """ self.top_frame = Frame(self.tk, background=ApiSettings.Background) self.top_frame.pack(side=TOP, fill=BOTH, expand=YES) self.center_frame = Frame(self.tk, background=ApiSettings.Background) self.center_frame.pack(side=TOP, fill=BOTH, expand=YES) self.bottom_frame = Frame(self.tk, background=ApiSettings.Background) self.bottom_frame.pack(side=TOP, fill=BOTH, expand=YES) """ Initialization of api window features """ self.connections_menu = ConnectionsMenu(self.bottom_frame) self.connections_menu.pack(side=RIGHT, anchor=SW, padx=ApiSettings.PaddingX, pady=ApiSettings.PaddingY) self.clock = Clock(self.top_frame) self.clock_displayed = False self.clock_view(display=True) self.right_top_corner = Frame(self.top_frame, background=ApiSettings.Background) self.right_top_corner.pack(side=TOP, fill=BOTH, expand=YES) self.user_logged = User(self.right_top_corner) self.user_displayed = False self.pulse_text = PulseText(self.center_frame) self.weather = None self.weather_displayed = False self.news = None self.news_displayed = False self.api_info = GLO_MSG['API_WINDOW_INITIALIZED'] self.api_runs = True Logger.debug("Initialization of Application Window class")
def init_program_threads(): message_queue = Queue() message_locker = Lock() message_handler = MessagesHandler(messages_queue=message_queue, messages_locker=message_locker) main_ui_thread = UiThread(messages_handler=message_handler) main_ui_thread.start() main_uc_thread = UcThread(messages_handler=message_handler) main_uc_thread.start() message_queue.join() Logger.debug('Threads starts successfully')
def init_properties(): parser = ArgumentParser( prog='smartmirror', description='Smart Mirror program', epilog='more detailed information in README.md file https://github.com/not4juu/SmartMirror' ) parser.add_argument('-v', '--verbose', action='count', help='show verbose logs on console') parser.add_argument('--version', action='version', version='%(prog)s 1.0.0') args = parser.parse_args() init_logger(logs_to_file=True, verbose=args.verbose) Logger.debug('Initialization of properties finish successfully')
def init_command_recognition(self): Logger.debug("Initialization of command recognition") self.command_recognition = CommandsRecognition( self.command_detection_callback) if self.command_recognition.api_runs: self.command_recognition.background_listen() else: self.close_thread = True Logger.debug("Command recognition class state: {0}".format( GET_MESSAGE(self.command_recognition.api_info))) self.MessagesHandler.send_message(self.command_recognition.api_info)
def __init__(self, messages_handler): Thread.__init__(self, name="UI_Thread") self.MessagesHandler = messages_handler self.close_thread = False self.api_window = None self.camera = None self.speaker = None self.authorization_complete = False self.authorized_user_name = "" self.quit_authorization = False Logger.debug("Initialization of User Interface Thread class")
def run(self, method='opencv_face_recognition', debug=False): Logger.debug("Start authorization thread: {0}".format(method)) self.thread_running = True self.authorization_process_running = True self.debug = debug if method is 'opencv_face_recognition': target = self.run_opencv_face_recognition if method is 'dlib_face_recognition': target = self.run_dlib_face_recognition listener_thread = Thread(target=target) listener_thread.daemon = True listener_thread.start()
def __init__(self, parent): Frame.__init__(self, parent, bg=ApiSettings.Background) self.weather_api_token = '' # token from https://darksky.net/dev/ account self.location_info = None self.weather_info = None self.weather_lang = 'pl' self.weather_unit = 'auto' self.weather_icon = None self.degree_frame = Frame(self, bg=ApiSettings.Background) self.degree_frame.pack(side=TOP, anchor=E) self.temperature_label = Label(self.degree_frame, font=(ApiSettings.Font, ApiSettings.HugeTextSize), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.temperature_label.pack(side=RIGHT, anchor=CENTER) self.icon_label = Label(self.degree_frame, bg=ApiSettings.Background) self.icon_label.pack(side=LEFT, anchor=NW, padx=ApiSettings.PaddingX) self.currently_label = Label(self, font=(ApiSettings.Font, ApiSettings.LargeTextSize - 10), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.currently_label.pack(side=TOP, anchor=E) self.forecast_label = Label(self, font=(ApiSettings.Font, ApiSettings.MediumTextSize, 'normal', 'italic'), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.forecast_label.pack(side=TOP, anchor=E) self.location_label = Label(self, font=(ApiSettings.Font, ApiSettings.SmallTextSize), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.location_label.pack(side=TOP, anchor=E) if self.weather_api_token: Logger.debug("Initialization of Weather class") self.get_weather() else: Logger.error( "Please define api token for https://darksky.net/dev weather data will not be available" )
def __init__(self, parent): Frame.__init__(self, parent, bg=ApiSettings.Background) image = Image.open(icons["news"]) image = image.resize((20, 20), Image.ANTIALIAS) image = image.convert('RGB') photo = ImageTk.PhotoImage(image) self.icon_label = Label(self, bg=ApiSettings.Background, image=photo) self.icon_label.image = photo self.icon_label.pack(side=LEFT, anchor=N) self.headline_text = "" self.headline_text_label = Label(self, text=self.headline_text, font=(ApiSettings.Font, ApiSettings.SmallTextSize), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.headline_text_label.pack(side=LEFT, anchor=N) Logger.debug("Initialization of News Headline class")
def __init__(self, parent): self.canvas = Canvas(parent, bg=ApiSettings.Background, highlightthickness=0) width = int(self.canvas.cget("width")) / 2 height = int(self.canvas.cget("height")) / 2 self.text = self.canvas.create_text(width, height, fill=ApiSettings.Foreground, font=(ApiSettings.Font, ApiSettings.LargeTextSize), text="no text") self.run = False self.color = 0 self.brighten = True Logger.debug("Initialization of PulseText class")
def run_messages_handler(self): handler = { GLO_MSG['NETWORK_CONNECTION_FAILURE']: self.handler_api_window_close, GLO_MSG['API_CAMERA_CONNECTION_FAILURE']: self.handler_api_window_close, GLO_MSG['API_USER_QUIT']: self.handler_api_window_close, GLO_MSG['NETWORK_CONNECTION_SUCCESS']: self.handler_network_success, GLO_MSG['API_WINDOW_INITIALIZED']: self.handler_window_success, } message_id = self.MessagesHandler.get_message() if message_id is None: return message_id call_handler = handler.get( message_id, lambda: self.MessagesHandler.send_message_again(message_id)) Logger.debug(call_handler.__name__) call_handler()
def run_messages_handler(self): handler = { GLO_MSG['MICROPHONE_FAILURE']: self.handler_microphone_failure, GLO_MSG['MICROPHONE_INITIALIZED']: self.handler_microphone_initialized, GLO_MSG['DISPLAY_WEATHER']: self.handler_display_weather, GLO_MSG['HIDE_WEATHER']: self.handler_hide_weather, GLO_MSG['DISPLAY_NEWS']: self.handler_display_news, GLO_MSG['HIDE_NEWS']: self.handler_hide_news, GLO_MSG['DISPLAY_CLOCK']: self.handler_display_clock, GLO_MSG['HIDE_CLOCK']: self.handler_hide_clock, GLO_MSG['LOGOUT']: self.handler_logout, } message_id = self.MessagesHandler.get_message() if message_id is None: return None call_handler = handler.get( message_id, lambda: self.MessagesHandler.send_message_again(message_id)) Logger.debug(call_handler.__name__) call_handler()
def __init__(self, callback, language="pl-PL", api_option=ApiOption.GOOGLE): super().__init__() self.callback_command_detected = callback self.api_option = api_option self.language = language self.listen_thread = None self.phrase_time_limit = 3 try: self.recognizer = Recognizer() self.microphone = Microphone() """ adjust the recognizer sensitivity to ambient noise and record audio from the microphone """ with self.microphone as source: self.recognizer.adjust_for_ambient_noise(source) except OSError as ose_err: Logger.critical("OSError: {0}".format(ose_err)) self.api_runs = False self.api_info = GLO_MSG['MICROPHONE_FAILURE'] return except Exception as err: Logger.critical("Exception: {0}".format(err)) self.api_runs = False self.api_info = GLO_MSG['MICROPHONE_FAILURE'] return Logger.debug("Initialization of CommandsRecognition class") self.api_info = GLO_MSG['MICROPHONE_INITIALIZED']