def __init__(self): logging.info("starting camera") cam_props = { "width": 640, "height": 512, "cv_image_factor": config.Config.get().get("cv_image_factor"), "exposure_mode": config.Config.get().get("camera_exposure_mode"), "framerate": config.Config.get().get("camera_framerate"), "bitrate": config.Config.get().get("camera_jpeg_bitrate"), "jpeg_quality": int(config.Config.get().get("camera_jpeg_quality")) } self._camera = camera.Camera(props=cam_props) self.recording = False self.video_start_time = time.time() + 8640000 self._image_time = 0 self._cv_image_factor = int(config.Config.get().get( "cv_image_factor", 4)) self._image_refresh_timeout = float(config.Config.get().get( "camera_refresh_timeout", 0.1)) self._color_object_size_min = int(config.Config.get().get( "camera_color_object_size_min", 80)) / (self._cv_image_factor * self._cv_image_factor) self._color_object_size_max = int(config.Config.get().get( "camera_color_object_size_max", 32000)) / (self._cv_image_factor * self._cv_image_factor) self._path_object_size_min = int(config.Config.get().get( "camera_path_object_size_min", 80)) / (self._cv_image_factor * self._cv_image_factor) self._path_object_size_max = int(config.Config.get().get( "camera_path_object_size_max", 32000)) / (self._cv_image_factor * self._cv_image_factor) self._photos = [] self.load_photo_metadata() if not self._photos: self._photos = [] for filenames in os.walk(PHOTO_PATH): for filename in filenames[2]: if (PHOTO_PREFIX in filename or VIDEO_PREFIX in filename ) and PHOTO_THUMB_SUFFIX not in filename: self._photos.append({'name': filename}) self.save_photo_metadata() self._cnn_classifiers = {} cnn_model = config.Config.get().get("cnn_default_model", "") if cnn_model != "": try: self._cnn_classifiers[cnn_model] = CNNManager.get_instance( ).load_model(cnn_model) self._cnn_classifier_default = self._cnn_classifiers[cnn_model] logging.info("loaded: " + cnn_model + " " + str(self._cnn_classifier_default)) except Exception: logging.warning("model not found: %s", cnn_model) self._camera.grab_start() self._image_cv = self.get_image() super(Camera, self).__init__()
def run_server(): bot = None cam = None try: try: app.bot_config = Config.read() bot = CoderBot.get_instance( servo=(app.bot_config.get("move_motor_mode") == "servo"), motor_trim_factor=float( app.bot_config.get('move_motor_trim', 1.0))) audio = Audio.get_instance() audio.say(app.bot_config.get("sound_start")) try: cam = Camera.get_instance() Motion.get_instance() except picamera.exc.PiCameraError: logging.error("Camera not present") CNNManager.get_instance() EventManager.get_instance("coderbot") if app.bot_config.get('load_at_start') and app.bot_config.get( 'load_at_start'): app.prog = app.prog_engine.load( app.bot_config.get('load_at_start')) app.prog.execute() except ValueError as e: app.bot_config = {} logging.error(e) bot.set_callback(PIN_PUSHBUTTON, button_pushed, 100) remove_doreset_file() app.run(host="0.0.0.0", port=5000, debug=True, use_reloader=False, threaded=True) finally: if cam: cam.exit() if bot: bot.exit() app.shutdown_requested = True
def serve_legacy(): """ Serve the the legacy web application """ return render_template('main.html', host=request.host[:request.host.find(':')], locale=get_locale(), config=app.bot_config, program_level=app.bot_config.get("prog_level", "std"), cam=Camera.get_instance() != None, cnn_model_names=json.dumps([[name, name] for name in CNNManager.get_instance().get_models().keys()]))
def serve_legacy(): """ Serve the the legacy web application """ return render_template('main.html', host=request.host[:request.host.find(':')], locale=get_locale(), config=app.bot_config, program_level=app.bot_config.get("prog_level", "std"), cam=Camera.get_instance() != None, cnn_model_names=json.dumps([[name] for name in CNNManager.get_instance().get_models().keys()]))
def handle_cnn_models_new(): cam = Camera.get_instance() cnn = CNNManager.get_instance() logging.info("cnn_models_new") data = json.loads(request.get_data(as_text=True)) cnn.train_new_model(model_name=data["model_name"], architecture=data["architecture"], image_tags=data["image_tags"], photos_meta=cam.get_photo_list(), training_steps=data["training_steps"], learning_rate=data["learning_rate"]) return json.dumps({"name": data["model_name"], "status": 0})
def cnn_classify(self, model_name=None): classifier = None if model_name: classifier = self._cnn_classifiers.get(model_name) if classifier is None: classifier = CNNManager.get_instance().load_model(model_name) self._cnn_classifiers[model_name] = classifier else: classifier = self._cnn_classifier_default img = self.get_image() classes = classifier.classify_image(img.mat()) s_classes = sorted(classes.items(), key=lambda x: x[1], reverse=True) return s_classes
def run_server(): bot = None cam = None try: try: app.bot_config = Config.read() bot = CoderBot.get_instance(servo=(app.bot_config.get("move_motor_mode") == "servo"), motor_trim_factor=float(app.bot_config.get('move_motor_trim', 1.0))) audio = Audio.get_instance() audio.say(app.bot_config.get("sound_start")) try: cam = Camera.get_instance() Motion.get_instance() except picamera.exc.PiCameraError: logging.error("Camera not present") CNNManager.get_instance() EventManager.get_instance("coderbot") if app.bot_config.get('load_at_start') and app.bot_config.get('load_at_start'): app.prog = app.prog_engine.load(app.bot_config.get('load_at_start')) app.prog.execute() except ValueError as e: app.bot_config = {} logging.error(e) bot.set_callback(PIN_PUSHBUTTON, button_pushed, 100) remove_doreset_file() app.run(host="0.0.0.0", port=5000, debug=True, use_reloader=False, threaded=True) finally: if cam: cam.exit() if bot: bot.exit() app.shutdown_requested = True
def cnn_classify(self, model_name=None, top_results=3): classifier = None if model_name: classifier = self._cnn_classifiers.get(model_name) if classifier is None: classifier = CNNManager.get_instance().load_model(model_name) self._cnn_classifiers[model_name] = classifier else: classifier = self._cnn_classifier_default classes = None try: img = self.get_image() classes = classifier.classify_image(img.mat(), top_results=top_results) except Exception: logging.warning("classifier not available") classes = [("None", 1.0)] return classes
def __init__(self): logging.info("starting camera") cam_props = {"width":640, "height":512, "cv_image_factor": config.Config.get().get("cv_image_factor"), "exposure_mode": config.Config.get().get("camera_exposure_mode"), "framerate": config.Config.get().get("camera_framerate"), "bitrate": config.Config.get().get("camera_jpeg_bitrate"), "jpeg_quality": int(config.Config.get().get("camera_jpeg_quality"))} self._camera = camera.Camera(props=cam_props) self.recording = False self.video_start_time = time.time() + 8640000 self._image_time = 0 self._cv_image_factor = int(config.Config.get().get("cv_image_factor", 4)) self._image_refresh_timeout = float(config.Config.get().get("camera_refresh_timeout", 0.1)) self._color_object_size_min = int(config.Config.get().get("camera_color_object_size_min", 80)) / (self._cv_image_factor * self._cv_image_factor) self._color_object_size_max = int(config.Config.get().get("camera_color_object_size_max", 32000)) / (self._cv_image_factor * self._cv_image_factor) self._path_object_size_min = int(config.Config.get().get("camera_path_object_size_min", 80)) / (self._cv_image_factor * self._cv_image_factor) self._path_object_size_max = int(config.Config.get().get("camera_path_object_size_max", 32000)) / (self._cv_image_factor * self._cv_image_factor) self._photos = [] self.load_photo_metadata() if not self._photos: self._photos = [] for filenames in os.walk(PHOTO_PATH): for filename in filenames[2]: if (PHOTO_PREFIX in filename or VIDEO_PREFIX in filename) and PHOTO_THUMB_SUFFIX not in filename: self._photos.append({'name': filename}) self.save_photo_metadata() self._cnn_classifiers = {} cnn_model = config.Config.get().get("cnn_default_model", "") if cnn_model != "": try: self._cnn_classifiers[cnn_model] = CNNManager.get_instance().load_model(cnn_model) self._cnn_classifier_default = self._cnn_classifiers[cnn_model] logging.info("loaded: " + cnn_model + " " + str(self._cnn_classifier_default)) except Exception: logging.warning("model not found: %s", cnn_model) self._camera.grab_start() self._image_cv = self.get_image() super(Camera, self).__init__()
def cnn_detect_objects(self, model_name=None, top_results=3): classifier = None if model_name: classifier = self._cnn_classifiers.get(model_name) if classifier is None: classifier = CNNManager.get_instance().load_model(model_name) self._cnn_classifiers[model_name] = classifier else: classifier = self._cnn_classifier_default t0 = time.time() classes = None try: img = self.get_image() classes = classifier.detect_objects(img.mat(), top_results=top_results) except Exception: logging.warning("classifier not available") classes = [("None", 100)] raise logging.info("fps: %f", 1.0/(time.time()-t0)) return classes
def handle_cnn_models_list(): cnn = CNNManager.get_instance() logging.info("cnn_models_list") return json.dumps(cnn.get_models())
def handle_cnn_models_status(model_name): cnn = CNNManager.get_instance() logging.info("cnn_models_status") model_status = cnn.get_models().get(model_name) return json.dumps(model_status)
def handle_cnn_models_delete(model_name): cnn = CNNManager.get_instance() logging.info("cnn_models_delete") model_status = cnn.delete_model(model_name=model_name) return json.dumps(model_status)