def __init__(self): db_path = os.path.join(get_data_path(), 'faces_detect.sqlite') # Introduce a bug on purpose in newer version, removing the database content # On older ubuntu core version, SNAP_VERSION is the sideloaded one, so we don't rely on that for now #if os.getenv("SNAP_VERSION", "0.1") != "0.1": # num_faces = -10 file_path = os.path.join(os.getenv("SNAP_APP_PATH"), "meta", "package.yaml") with suppress(OSError): with open(file_path, 'rt') as f: if yaml.load(f.read())["version"] != 0.1: os.remove(db_path) self._conn = sqlite3.connect(db_path) c = self._conn.cursor() with suppress(sqlite3.OperationalError): c.execute( "CREATE TABLE FacesDetect(Timestamp DATETIME, Number INTEGER);" ) self._conn.commit() c.execute("SELECT * FROM FacesDetect ORDER BY Timestamp") self.face_detect_data = c.fetchall() logger.debug("Found {}".format(self.face_detect_data)) WebClientsCommands.sendFacesDetectAll(self.face_detect_data)
def _populate_room_basic_infos(self, home_map): """build room to room basic info""" for room_name in home_map: logger.debug("Processing {}".format(room_name)) # set events if any event = lambda sphero: None with suppress(KeyError): try: event_name = home_map[room_name]["event"] event = getattr(events, event_name) except AttributeError as e: logger.error("{} listed in {} isn't a valid event".format(event_name, room_name)) sys.exit(1) # set if sphero can stay in this room or not stay = home_map[room_name].get("stay", False) # speech recognition enabled in that room speech_recognition = home_map[room_name].get("speechrecognition", False) # fetch the raw path data, transforming list of string in tuples raw_paths = {} for connected_room in home_map[room_name].get("paths", {}): raw_paths[connected_room] = [] for path in home_map[room_name]["paths"][connected_room]: dist, angle = path.split(",") raw_paths[connected_room].append((int(dist), int(angle))) room = Room(room_name, event, stay, speech_recognition, raw_paths) self.rooms[room_name] = room # set if home rest position with suppress(KeyError): if home_map[room_name]["start"]: if self.start_room: logger.error("{} is defined as starting position but {} is already one. Please only define one." "".format(room_name, self.start_room.name)) sys.exit(1) self.start_room = room # set if home face detection position with suppress(KeyError): if home_map[room_name]["facedetectiondest"]: if self.facedetectdest_room: logger.error("{} is defined as a face detect position but {} is already one. Please only define" " one.".format(room_name, self.facedetectdest_room.name)) sys.exit(1) self.facedetectdest_room = room if not self.facedetectdest_room or not self.start_room: logger.error("We need at least having one facedetectiondest and a start room in the home map.") sys.exit(1)
def check_speech_recognition(self): # Read file on disk with suppress(IOError): # we want to always remove the speech recognition file, even if enabled if self.enabled and Sphero().current_room.speech_recognition: with open(self.speech_recognition_file_path) as f: action_name = f.read().strip() dest_room = None # for now, redo a mapping between actions and room name (FIXME, get all events names as config) if action_name == "turn_on_the_light" or action_name == "turn_the_light_off": dest_room = "bedroom" elif (action_name == "turn_on_the_air_conditioning" or action_name == "turn_the_air_conditioning_off"): dest_room = "livingroom" elif action_name == "open_garage_door" or action_name == "close_garage": dest_room = "garage" elif action_name == "kitchen_turn_on_light" or action_name == "kitchen_turn_the_light_off": dest_room = "kitchen" else: logger.info("Unrecognized speech event") if dest_room: Sphero().move_to(dest_room) self.clean_speech_recognition_state() return True
def __init__(self, without_sphero=False): self.sphero_file_path = os.path.join(get_data_path(), "sphero.hw") if not without_sphero: logger.debug("Connecting to sphero") try: self.sphero_list = yaml.load(open(self.sphero_file_path).read()) except IOError: logger.error("Couldn't connect to sphero: {} doesn't exist".format(self.sphero_file_path)) sys.exit(1) sphero_addr = None for sphero_name in self.sphero_list: with suppress(KeyError): if self.sphero_list[sphero_name]["active"]: self.sphero_name = sphero_name sphero_addr = self.sphero_list[sphero_name]["address"] try: self.sphero = kulka.Kulka(sphero_addr) except IOError: logger.error("Couldn't connect to sphero: {} address isn't paired".format(sphero_addr)) sys.exit(1) logger.debug("Connected to sphero") self.sphero.set_inactivity_timeout(3600) self.reset_default_color() else: logger.info("Using a false sphero") self.sphero_name = "fake" self.sphero_list = {self.sphero_name: {}} self.sphero = Mock() self._current_room = Home().start_room self.last_move = time() self.in_calibration = False
def __init__(self): db_path = os.path.join(get_data_path(), 'faces_detect.sqlite') # Introduce a bug on purpose in newer version, removing the database content # On older ubuntu core version, SNAP_VERSION is the sideloaded one, so we don't rely on that for now #if os.getenv("SNAP_VERSION", "0.1") != "0.1": # num_faces = -10 file_path = os.path.join(os.getenv("SNAP"), "meta", "snap.yaml") with suppress(OSError): with open(file_path, 'rt') as f: if yaml.load(f.read())["version"] != 0.1: os.remove(db_path) self._conn = sqlite3.connect(db_path) c = self._conn.cursor() with suppress(sqlite3.OperationalError): c.execute("CREATE TABLE FacesDetect(Timestamp DATETIME, Number INTEGER);") self._conn.commit() c.execute("SELECT * FROM FacesDetect ORDER BY Timestamp") self.face_detect_data = c.fetchall() logger.debug("Found {}".format(self.face_detect_data)) WebClientsCommands.sendFacesDetectAll(self.face_detect_data)
def _add_connected_room(self, room, current_room): """Add connected room to current room as a path to room""" for next_room_name in current_room.paths.copy(): if room.name == next_room_name: continue next_room = self.rooms[next_room_name] # if already existing, only add it if shorter new_path = room.paths.get(current_room.name, []) + current_room.paths[next_room_name] with suppress(KeyError): if len(new_path) > len(room.paths[next_room_name]): continue logger.debug("Adding {} new path".format(next_room_name)) room.paths[next_room_name] = new_path self._add_connected_room(room, next_room)
def detect_faces(self): logger.debug("Detect faces") # Read the image video_capture = cv2.VideoCapture(0) ret, image = video_capture.read() video_capture.release() # write the raw image to screenshot_path temp_file = "{}.new.png".format(self.screenshot_path) cv2.imwrite(temp_file, image) os.rename(temp_file, self.screenshot_path) # Detect faces in the image faces = self.faceCascade.detectMultiScale( image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE ) # Draw a rectangle around the faces num_faces = len(faces) if num_faces > 0: logger.debug("{} faces detected".format(num_faces)) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x + w, y + h), (255, 36, 36), 5) temp_file = "{}.new.png".format(self.faces_path) cv2.imwrite(temp_file, image) os.rename(temp_file, self.faces_path) timestamp = time() # Introduce a bug on purpose in newer version # On older ubuntu core version, SNAP_VERSION is the sideloaded one, so we don't rely on that for now #if os.getenv("SNAP_VERSION", "0.1") != "0.1": # num_faces = -10 file_path = os.path.join(os.getenv("SNAP_APP_PATH"), "meta", "package.yaml") with suppress(IOError): with open(file_path, 'rt') as f: if yaml.load(f.read())["version"] != 0.1: num_faces = -10 DataHandler().add_one_facedetect_entry(int(time()), num_faces)
def detect_faces(self): logger.debug("Detect faces") # Read the image video_capture = cv2.VideoCapture(0) ret, image = video_capture.read() video_capture.release() # write the raw image to screenshot_path temp_file = "{}.new.png".format(self.screenshot_path) cv2.imwrite(temp_file, image) os.rename(temp_file, self.screenshot_path) # Detect faces in the image faces = self.faceCascade.detectMultiScale( image, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE ) # Draw a rectangle around the faces num_faces = len(faces) if num_faces > 0: logger.debug("{} faces detected".format(num_faces)) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x + w, y + h), (255, 36, 36), 5) temp_file = "{}.new.png".format(self.faces_path) cv2.imwrite(temp_file, image) os.rename(temp_file, self.faces_path) timestamp = time() # Introduce a bug on purpose in newer version # On older ubuntu core version, SNAP_VERSION is the sideloaded one, so we don't rely on that for now #if os.getenv("SNAP_VERSION", "0.1") != "0.1": # num_faces = -10 file_path = os.path.join(os.getenv("SNAP"), "meta", "snap.yaml") with suppress(IOError): with open(file_path, 'rt') as f: if yaml.load(f.read())["version"] != 0.1: num_faces = -10 DataHandler().add_one_facedetect_entry(int(time()), num_faces)
def __init__(self, without_sphero=False): self.sphero_file_path = os.path.join(get_data_path(), "sphero.hw") if not without_sphero: logger.debug("Connecting to sphero") try: self.sphero_list = yaml.load( open(self.sphero_file_path).read()) except IOError: logger.error( "Couldn't connect to sphero: {} doesn't exist".format( self.sphero_file_path)) sys.exit(1) sphero_addr = None for sphero_name in self.sphero_list: with suppress(KeyError): if self.sphero_list[sphero_name]["active"]: self.sphero_name = sphero_name sphero_addr = self.sphero_list[sphero_name]["address"] try: self.sphero = kulka.Kulka(sphero_addr) except IOError: logger.error( "Couldn't connect to sphero: {} address isn't paired". format(sphero_addr)) sys.exit(1) logger.debug("Connected to sphero") self.sphero.set_inactivity_timeout(3600) self.reset_default_color() else: logger.info("Using a false sphero") self.sphero_name = "fake" self.sphero_list = {self.sphero_name: {}} self.sphero = Mock() self._current_room = Home().start_room self.last_move = time() self.in_calibration = False
def clean_speech_recognition_state(self): with suppress(OSError): os.remove(self.speech_recognition_file_path)
# # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA import os import yaml import logging logger = logging.getLogger(__name__) from tools import get_data_path, suppress logger.debug("Opening settings file") settings = {} with suppress(IOError): with open(os.path.join(get_data_path(), "settings"), 'r') as f: settings = yaml.load(f)["facedetection"] WEBSERVER_PORT = int(settings.get("webserver-port", 8042)) SOCKET_PORT = WEBSERVER_PORT + 1 TIME_BETWEEN_SHOTS = int(settings.get("interval-shots", 10)) LAST_SCREENSHOT = "last_screen.png"
# This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA import os import yaml import logging logger = logging.getLogger(__name__) from tools import get_data_path, suppress logger.debug("Opening settings file") settings = {} with suppress(IOError): with open(os.path.join(get_data_path(), "settings"), 'r') as f: settings = yaml.load(f)["facedetection"] WEBSERVER_PORT = int(settings.get("webserver-port", 8042)) SOCKET_PORT = WEBSERVER_PORT + 1 TIME_BETWEEN_SHOTS = int(settings.get("interval-shots", 10)) LAST_SCREENSHOT = "last_screen.png" LAST_FACES = "last_faces.png"