Esempio n. 1
0
    def __init__(self):
        """Initializes the GUI and connects its widgets to the corresponding functions."""
        super(self.__class__, self).__init__()
        Logger.info("Creating dataset creation GUI")
        self.setupUi(self)
        self.__set_logo_icon()

        self.last_folder_opened = None
        self.frames = []
        self.last_frame_image = None
        self.current_frame_id = 0
        self.current_module_id_in_frame = 0
        self.current_frame_modules = []
        self.discarded_modules = {}
        self.accepted_modules = {}
        self.misdetected_modules = {}

        self.module_counter = {
            "automatic": {
                "accepted": 0,
                "discarded": 0,
                "misdetected": 0
            },
            "manual": {
                "accepted": 0,
                "discarded": 0,
                "misdetected": 0
            }
        }

        self.thermo_thread = None

        self.__connect_widgets()
Esempio n. 2
0
    def __init__(self):
        """Initializes the ThermoThread for dataset creation.
        """
        super(self.__class__, self).__init__()
        Logger.info("Created dataset creation ThermoThread")
        self.camera_param_file_name = None

        self.__load_default_paths()

        self.app = tg.App(input_video_path=None, camera_param_file=self.camera_param_file_name)

        self.processing_frame = None
        self.processing_frame_id = None
    def __init__(self):
        """Initializes the GUI and connects its widgets to the corresponding functions."""
        super(self.__class__, self).__init__()
        Logger.info("Creating themoGUI")
        self.setupUi(self)
        self.__set_logo_icon()
        self.thermo_thread = ThermoGuiThread()
        self.is_stoppable = True

        self.last_folder_opened = None

        self.__connect_widgets()
        self.__connect_thermo_thread()

        self.capture = None
        self.webcam_port = None
Esempio n. 4
0
    def __init__(self):
        """Initializes the Thermo Thread.
        """
        super(ThermoGuiThread, self).__init__()
        Logger.info("Created ThermoGUI thread")

        self.camera_param_file_name = None
        self.input_file_name = None

        self.pause_time = 50
        self.is_paused = False

        self.webcam_port = None
        self.cap = None
        self.should_use_webcam = False

        self.__load_default_paths()

        self.app = tg.App(input_video_path=self.input_file_name, camera_param_file=self.camera_param_file_name)
Esempio n. 5
0
    def __init__(self, checkpoint_dir: str, model_class: type,
                 image_shape: np.ndarray, num_classes: int):
        """Initializes the inference object by loading the weights of an already trained model.

        :param checkpoint_dir: Directory of the checkpoint where the weights of the model are stored.
        :param model_class: Class associated to the stored weights. This class is used to build the :attr:`tf.graph` which will be used for the inference.
        :param image_shape: Shape of the images fed to the classifier. The images passed to the :func:`self.classify <thermography.classification.inference.Inference.classify>` function are resized according to this parameter.
        :param num_classes: Number of classes predicted by the model.

        .. warning:: The parameters must all be consistent with the preexisting weights!
        """
        self.checkpoint_dir = checkpoint_dir
        self.image_shape = image_shape
        self.num_classes = num_classes

        self.graph = tf.Graph()
        with self.graph.as_default():
            self.x = tf.placeholder(tf.float32, [None, *self.image_shape],
                                    name="input_image")
            self.keep_probability = tf.placeholder(tf.float32,
                                                   name="keep_probability")
            self.model = model_class(x=self.x,
                                     image_shape=self.image_shape,
                                     num_classes=self.num_classes,
                                     keep_prob=self.keep_probability)

        self.logits = self.model.logits
        self.probabilities = tf.nn.softmax(self.logits)

        # Add ops to save and restore all the variables.
        self.sess = tf.Session(graph=self.graph)

        # Restore variables from disk.
        with self.sess.as_default():
            with self.graph.as_default():
                self.saver = tf.train.Saver()
                self.saver.restore(
                    self.sess,
                    os.path.join(self.checkpoint_dir, self.model.name))

        Logger.info("Model restored.")
Esempio n. 6
0
def setup_logger(console_log_level: LogLevel = LogLevel.INFO,
                 file_log_level: LogLevel = LogLevel.DEBUG,
                 log_file_name: str = None):
    """Sets up the simple logger.
    :param console_log_level: Log level associated to the streaming log.
    :param file_log_level: Log level associated to the file log.
    :param log_file_name: If set, then the file log is written to this file.
    Otherwise a new log file will be created in the log directory returned by
    :attr:`Settings.log_dir <pm.settings.__Settings.log_dir>`.
    """
    if log_file_name is None:
        log_directory = Settings.log_dir
        name = "pm_logging{}.log".format(
            datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
        log_file_name = os.path.join(log_directory, name)

    Logger.set_file_logging_level(file_log_level)
    Logger.set_log_file(log_file_name)
    Logger.set_console_logging_level(console_log_level)
    Logger.init()

    Logger.info("Logging to {} file.".format(log_file_name))
Esempio n. 7
0
    def __load_video(self, video_raw: cv2.VideoCapture):
        if not video_raw.isOpened():
            Logger.error("Unable to read {} feed".format(self.video_path))

        self.frames = []

        num_video_frames = int(video_raw.get(cv2.CAP_PROP_FRAME_COUNT))
        if self.end_frame is None or self.end_frame > num_video_frames:
            Logger.warning("Setting end_frame to {}".format(num_video_frames))
            self.end_frame = num_video_frames

        num_frames = 0

        # Skip the first frames until the self_start frame.
        video_raw.set(cv2.CAP_PROP_POS_FRAMES, self.start_frame)

        Logger.info("Loading {} frames...".format(self.end_frame -
                                                  self.start_frame))
        bar = progressbar.ProgressBar(maxval=self.num_frames,
                                      widgets=[
                                          progressbar.Bar('=', '[', ']'), ' ',
                                          progressbar.Percentage()
                                      ])
        bar.start()
        for i in range(self.end_frame - self.start_frame):
            ret = video_raw.grab()
            if not ret:
                Logger.error(
                    "Could not load frame {}".format(i + self.start_frame))
                raise ValueError(
                    "Could not load frame {}".format(i + self.start_frame))

            self.frames.append(video_raw.retrieve()[1])
            num_frames += 1
            bar.update(num_frames)

        bar.finish()
        video_raw.release()
Esempio n. 8
0
 def __del__(self):
     Logger.info("Deleting inference object")
     if hasattr(self, 'sess'):
         self.sess.close()
Esempio n. 9
0
def download_oasi_data(custom_params: dict = None) -> None:
    """Downloads all the data specified in the passed argument to the data folder.

    :Example:

        .. code-block:: python

            domains = ['meteo', 'air']
            years = range(2000, 2018)
            download_params = {
                "domains": domains,
                "years": years
            }
            download_oasi_data(download_params)

    :param custom_params: Dictionary of custom params to be used to download the data.
    """
    if custom_params is None:
        custom_params = {}

    params = {
        "domains": ["air", "meteo"],
        "years": range(2000, 2018),
        "data_directory": pm.Settings.data_dir,
        "overwrite": False,
        "ignore_locations":
        ["Stabio_16"]  # Ignore this location, as it is downloaded
        # thorough the Oasi-Client interface which offers multiple temperature sensors.
    }
    params.update(custom_params)

    # Log the parameters being used.
    logger_msg = "Downloading oasi data with following params:"
    align = max([len(name) for name in params]) + 4
    for name, value in params.items():
        logger_msg += "\n{:>{align}}: {}".format(name, value, align=align)
    Logger.info(msg=logger_msg)

    domains = params["domains"]
    years = params["years"]
    data_directory = params["data_directory"]
    overwrite = params["overwrite"]

    if len(domains) == 0:
        raise RuntimeError("Domains passed to `download_oasi_data` is empty!")

    if not os.path.exists(data_directory):
        os.makedirs(data_directory, exist_ok=True)

    for dom in domains:
        Logger.info("Downloading domain {}".format(dom))
        dom_path = os.path.join(data_directory, dom)
        os.makedirs(dom_path, exist_ok=True)
        domain = requests.get(
            'http://www.oasi.ti.ch/web/rest/parameters?domain=' + dom)
        domain_features = json.loads(domain.text)

        for feature in domain_features:
            feature_path = os.path.join(dom_path, feature['code'])
            Logger.info("Downloading feature {}/{}".format(
                dom, feature["code"]))
            os.makedirs(feature_path, exist_ok=True)

            # Get locations associated to the features.
            locations = requests.get(
                'http://www.oasi.ti.ch/web/rest/locations?domain=' + dom +
                '&parameter=' + feature['code'])

            locations = json.loads(locations.text)

            for loc in locations:
                if loc['name'] + "_" + loc['code'] in params[
                        "ignore_locations"]:
                    Logger.warning(
                        "Ignoring {} location".format(loc['name'] + "_" +
                                                      loc['code']))
                    continue
                loc_path = os.path.join(feature_path,
                                        loc['name'] + "_" + loc['code'])
                os.makedirs(loc_path, exist_ok=True)
                for year in years:
                    csv_file_name = os.path.join(loc_path, str(year) + '.csv')

                    # If the file already exists, then continue to next data without
                    # overwriting.
                    if not overwrite and os.path.exists(csv_file_name):
                        Logger.debug(
                            "File {} already existing. Skipping to next "
                            "file".format(csv_file_name))
                        continue

                    Logger.debug("Requesting {}.{}.{}.{}".format(
                        dom, feature["code"], loc['name'], year))

                    locations = requests.get(
                        'http://www.oasi.ti.ch/web/rest/measure/csv?domain=' +
                        dom + '&resolution=h&parameter=' + feature['code'] +
                        '&from=' + str(year) + '-01-01&to=' + str(year) +
                        '-12-31&location=' + loc['code'])

                    first_line = locations.text.split('\n', 1)[0]
                    if first_line == '# GENERALE':
                        write_csv_file(csv_file_name, locations)
                    else:
                        Logger.debug("Request was not successful!")
Esempio n. 10
0
 def __del__(self):
     Logger.info("Deleting inference object")
     self.sess.close()
Esempio n. 11
0
    def __save_module_dataset(self):
        """Saves the data to the directory selected when opening the file explorer."""
        self.progress_bar_all_frames.setEnabled(True)
        self.progress_bar_intra_frame.setEnabled(True)
        button_reply = QtWidgets.QMessageBox.question(self, 'Save dataset',
                                                      "Want to save dataset to {}?".format(self.output_directory),
                                                      QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
                                                      QtWidgets.QMessageBox.No)
        if button_reply == QtWidgets.QMessageBox.No:
            Logger.warning("Rejected directory <{}> for saving all images".format(self.output_directory))
            self.output_directory = None
            self.__save_module_dataset()
        else:
            Logger.info("Saving all images to <{}>".format(self.output_directory))
            Logger.warning("If dialog freezes, check log file, but DON'T close the window!")
            working_modules_output_dir = os.path.join(self.output_directory, "working")
            broken_modules_output_dir = os.path.join(self.output_directory, "broken")
            misdetected_modules_output_dir = os.path.join(self.output_directory, "misdetected")

            overall_iter = 0

            def save_modules_into_directory(module_dict: dict, directory: str):
                global overall_iter

                os.mkdir(os.path.abspath(directory))
                for module_number, (module_id, registered_modules) in enumerate(module_dict.items()):
                    Logger.debug("Saving all views of module ID {}: view {}/{}".format(module_id, module_number,
                                                                                       len(module_dict.keys()) - 1))
                    self.progress_bar_all_frames.setValue(self.progress_bar_all_frames.value() + 1)
                    self.progress_bar_intra_frame.setValue(0)
                    self.progress_bar_intra_frame.setMaximum(len(registered_modules))
                    for m_index, m in enumerate(registered_modules):
                        name = "id_{0:05d}_frame_{1:05d}.jpg".format(module_id, m["frame_id"])
                        path = os.path.join(directory, name)
                        img = cv2.cvtColor(m["image"], cv2.COLOR_RGB2BGR)
                        cv2.imwrite(path, img)
                        self.progress_bar_intra_frame.setValue(m_index + 1)

            Logger.info("Saving working modules to <{}>".format(working_modules_output_dir))
            save_modules_into_directory(self.working_modules, working_modules_output_dir)
            Logger.info("Saved all working modules to <{}>".format(working_modules_output_dir))
            Logger.info("Saving broken modules to <{}>".format(broken_modules_output_dir))
            save_modules_into_directory(self.broken_modules, broken_modules_output_dir)
            Logger.info("Saved all broken modules to <{}>".format(broken_modules_output_dir))
            Logger.info("Saving misdetected modules to <{}>".format(misdetected_modules_output_dir))
            save_modules_into_directory(self.misdetected_modules, misdetected_modules_output_dir)
            Logger.info("Saved all misdetected modules to <{}>".format(misdetected_modules_output_dir))

        _ = QtWidgets.QMessageBox.information(self, "Saved!", "Saved all modules to {}".format(self.output_directory),
                                              QtWidgets.QMessageBox.Ok)
        self.close()
Esempio n. 12
0
    def __load_params(self) -> None:
        """Load the parameters related to camera."""
        self.camera = Camera(camera_path=self.camera_param_file)

        Logger.info("Using camera parameters:\n{}".format(self.camera))