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 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 __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']
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 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 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 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 get_weather(self): location_info = Network.get_location() if self.location_info != location_info: self.location_info = location_info latitude = self.location_info['lat'] longitude = self.location_info['lon'] location = "{1} {2}, {0}".format(self.location_info['country'], self.location_info['city'], self.location_info['zip']) try: weather_req_url = "https://api.darksky.net/forecast/{0}/{1},{2}?lang={3}&units={4}".format( self.weather_api_token, latitude, longitude, self.weather_lang, self.weather_unit) response = requests.get(weather_req_url) weather_info = json.loads(response.text) Logger.debug("request: " + str(weather_req_url) + " response: " + str(response) + " json: " + str(weather_info)) except Exception as err: Logger.critical("Exception: {0}".format(err)) return None if self.weather_info != weather_info: self.weather_info = weather_info degree_sign = u'\N{DEGREE SIGN}' temperature = "{0}{1}".format( str(int(self.weather_info['currently']['temperature'])), degree_sign) currently = self.weather_info['currently']['summary'] forecast = self.weather_info["hourly"]["summary"] icon_id = self.weather_info['currently']['icon'] self.weather_icon = None if icon_id in icons: self.weather_icon = icons[icon_id] if self.weather_icon is not None: image = Image.open(self.weather_icon) image = image.resize((100, 100), Image.ANTIALIAS) image = image.convert('RGB') photo = ImageTk.PhotoImage(image) self.icon_label.config(image=photo) self.icon_label.image = photo else: self.icon_label.config(image='') self.currently_label.config(text=currently) self.forecast_label.config(text=forecast) self.temperature_label.config(text=temperature) self.location_label.config(text=location) Logger.info("get_weather") # updates every hour self.after(60 * 60 * 1000, self.get_weather)
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 callback_recognition(self, recognizer, audio): try: command = self._api_recognition(audio) self.validate_command(command) except UnboundLocalError as err: Logger.warning("UnboundLocalError : {0} ".format(err)) except RequestError as err: Logger.warning("RequestError : {0} ".format(err)) except UnknownValueError as err: Logger.debug("UnknownValueError : {0} ".format(err))
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 __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 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 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_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_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__(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__(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): 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 __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 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 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 enabled(): global network_status try: socket.setdefaulttimeout(3) socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("8.8.8.8", 53)) network_status = GLO_MSG['NETWORK_CONNECTION_SUCCESS'] Logger.debug("Internet connection enabled") return True except socket.error as socket_error: network_status = GLO_MSG['NETWORK_CONNECTION_FAILURE'] Logger.critical("Internet connection disabled socket.error : {0}".format(socket_error)) return False except OSError as ose_error: network_status = GLO_MSG['NETWORK_CONNECTION_FAILURE'] Logger.critical("Internet connection disabled OSError : {0}".format(ose_error)) return False except Exception as err: network_status = GLO_MSG['NETWORK_CONNECTION_FAILURE'] Logger.critical("Internet connection disabled exception : {0}".format(err)) return False
def _camera_connection(self): Logger.debug("Find camera connection") try: self.camera = cv2.VideoCapture(0) if not self.camera.isOpened(): raise NameError except cv2.error as exception: Logger.critical( "OpenCV camera hardware problem: {0}".format(exception)) self.api_info = GLO_MSG['API_CAMERA_CONNECTION_FAILURE'] self.api_runs = self.Disabled return except Exception as exception: Logger.critical( "Camera hardware is not connected: {0}".format(exception)) self.api_info = GLO_MSG['API_CAMERA_CONNECTION_FAILURE'] self.api_runs = self.Disabled return self.api_info = GLO_MSG['API_WINDOW_INITIALIZED'] self.api_runs = self.Enabled return
def __init__(self, parent): Frame.__init__(self, parent, bg=ApiSettings.Background) image = Image.open(icons["user_logged"]) image = image.resize((20, 20), Image.ANTIALIAS) image = image.convert('RGB') self.photo = ImageTk.PhotoImage(image) self.user_icon_label = Label(self, bg=ApiSettings.Background, image=self.photo) self.user_icon_label.pack(side=LEFT, anchor=N) self.user_name = "Log In" self.user_name_label = Label(self, text=self.user_name, font=(ApiSettings.Font, ApiSettings.SmallTextSize), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.user_name_label.pack(side=LEFT, anchor=N) Logger.debug("Initialization of User class")
def __init__(self, parent): Frame.__init__(self, parent, bg=ApiSettings.Background) self.time_format = 24 self.date_format = " %A : %d %B %Y" self.time = '' self.time_label = Label(self, font=(ApiSettings.Font, ApiSettings.LargeTextSize), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.time_label.pack(side=TOP, anchor=W) self.date = '' self.date_label = Label(self, text=self.date, font=(ApiSettings.Font, ApiSettings.MediumTextSize), fg=ApiSettings.Foreground, bg=ApiSettings.Background) self.date_label.pack(side=TOP, anchor=W) Logger.debug("Initialization of Clock class") self.tick()
def stop_animation(self): self.run = False self.canvas.pack_forget() Logger.debug("Stop pulse text animation")