Esempio n. 1
0
    def cmd_tag(self, args):
        if len(args) != 2:
            raise ArgumentError(
                "git tessera show takes identifier as argument and new tag")

        key = args[0]
        for i in os.listdir(Tessera._tesserae):
            tessera_path = "%s/%s" % (Tessera._tesserae, i)
            if not stat.S_ISDIR(os.lstat(tessera_path).st_mode):
                continue
            if i.split('-')[0] == key or i == key:
                break
        if not tessera_path:
            raise ArgumentError("git tessera %s not found" % key)

        t = Tessera(tessera_path, self._config)
        t.add_tag(args[1])
        files = [
            os.path.join(t.tessera_path, "tessera"),
            os.path.join(t.tessera_path, "info")
        ]
        self.git.add(
            files, "tessera updated: add tag %s to %s" %
            (args[1], t.get_attribute("title")))
        return True
Esempio n. 2
0
    def find_best_frames(self, number_frames, region_size):
        """
        Find the indices of the best "number_frames" frames under the condition that all indices
        are within an interval of size "region_size".

        :param number_frames: Number of best frames the indices of which are to be found.
        :param region_size: Maximal width of index interval.
        :return: (List of frame indices, quality loss, time line position) with:
                List of frame indices: Indices of frames participating in mean frame computation.
                quality loss: Loss in average frame quality due to range restriction (%).
                time line position: Position of the average frame index relative to the total
                                    duration of the video.
        """

        # Check input arguments for validity.
        if number_frames > region_size:
            raise ArgumentError("Attempt to find " + str(number_frames) +
                                " good frames in "
                                "an index interval of size " +
                                str(region_size))
        elif region_size > self.number:
            raise ArgumentError("Size of best frames region " +
                                str(region_size) + " larger "
                                "than the total number of frames " +
                                str(self.number))

        best_indices = []
        rank_sum_opt = 0.

        # Construct a sliding window on the full index range. For each window position find the
        # best "number_frames" frames. Find the window and the best frame set within with the
        # highest overall score.
        for start_index in range(self.number - region_size + 1):
            end_index = start_index + region_size
            best_indices_in_range = sorted(range(start_index, end_index),
                                           key=self.frame_ranks.__getitem__,
                                           reverse=True)[:number_frames]
            rank_sum = sum(
                [self.frame_ranks[i] for i in best_indices_in_range])
            if rank_sum > rank_sum_opt:
                rank_sum_opt = rank_sum
                best_indices = best_indices_in_range

        # Compare the average frame quality with the optimal choice if no time restrictions were
        # present.
        rank_sum_global = sum([
            self.frame_ranks[i]
            for i in self.quality_sorted_indices[:number_frames]
        ])
        quality_loss_percent = round(
            100. * (rank_sum_global - rank_sum_opt) / rank_sum_global, 1)

        # For the frames included in mean frame computation compute the average position on the
        # video time line.
        cog_mean_frame = round(100 * mean(best_indices) / self.number, 1)

        return best_indices, quality_loss_percent, cog_mean_frame
Esempio n. 3
0
    def ls(self, args=[]):
        if not os.path.exists(self.tesserae):
            return False

        try:
            idx = args.index("--sort")
            sortfunc = GitTessera.SORTING[args[idx + 1]]
        except ValueError:
            sortfunc = GitTessera.SORTING["status"]
        except IndexError:
            raise ArgumentError(
                "Please specify a sort algorithm. Available: %s" %
                (", ".join(GitTessera.SORTING.keys())))
        except KeyError:
            raise ArgumentError("No sort algorithm for '%s' available" %
                                args[idx + 1])

        try:
            idx = args.index("--tags")
            tags = args[idx + 1].split(",")
        except ValueError:
            tags = None
        except IndexError:
            raise ArgumentError("Please specify minimum one tag.")

        contents = [
            self.tesserae + "/" + x for x in os.listdir(self.tesserae)
            if stat.S_ISDIR(os.lstat(self.tesserae + "/" + x).st_mode)
        ]
        tesserae = []
        for tessera_path in contents:
            t = Tessera(tessera_path, self._config)

            if t.get_attribute("updated") == 0:
                tessera_info = "%s/info" % tessera_path
                fout = open(tessera_info, "w")
                author, author_time = self.git.read_author(tessera_path)
                import re
                r = re.compile("^([^\<]+) \<([^\>]+)\>$")
                m = r.search(author)
                if m:
                    fout.write("author: %s\n" % m.group(1))
                    fout.write("email: %s\n" % m.group(2))
                    fout.write("updated: %d\n" % author_time)
                fout.close()

            te_tags = t.get_attribute("tags")
            if not tags or any(x in te_tags for x in tags):
                tesserae.append(t)

        tesserae = sorted(tesserae, cmp=sortfunc)
        return tesserae
Esempio n. 4
0
    def cmd_remove(self, args):
        if len(args) != 1:
            raise ArgumentError(
                "git tessera remove takes identifier as argument")

        key = args[0]
        tessera_file = None
        tessera_path = None
        for i in os.listdir(Tessera._tesserae):
            tessera_path = "%s/%s" % (Tessera._tesserae, i)
            if not stat.S_ISDIR(os.lstat(tessera_path).st_mode):
                continue
            if i.split('-')[0] == key or i == key:
                tessera_file = "%s/tessera" % tessera_path
                break
        if not tessera_file:
            raise TesseraError("git tessera %s not found" % key)

        t = Tessera(tessera_path, self._config)
        stdout.write("remove tessera %s: %s ? [Y/n] " %
                     (key, t.get_attribute("title")))
        try:
            answer = stdin.readline().strip()
        except KeyboardInterrupt:
            return False
        if not answer or answer.lower() == "y":
            files = [
                "%s/%s" % (tessera_path, x) for x in os.listdir(tessera_path)
            ]
            self.git.rm(files,
                        "tessera removed: %s" % t.get_attribute("title"))

            from shutil import rmtree
            rmtree(tessera_path)
Esempio n. 5
0
def update_limit(resource, limit):
    """
    Update notification limits
    """
    if resource not in ['cpu', 'memory', 'storage', 'swap']:
        msg = """
Invalid resource name entered.

Valid resource names are:
 - cpu
 - memory
 - storage
 - swap
"""
        raise ArgumentError(msg)

    password = keyring.get_password('sms_password', 'Administrator')
    if password == None:
        print(
            'No Password set.\nSet a new password using `sms --update-password`.'
        )
        return

    user_password = getpass.getpass()
    if user_password != password:
        print('\nWrong Password entered')
        return

    if limit >= 100 or limit <= 0:
        msg = """
Invalid limit entered.

Limit must be between 0 and 100.
"""
        raise ArgumentError(msg)

    file_path = os.path.join(os.path.expanduser('~'), '.sms/settings.json')
    settings_file = open(file_path)
    settings = json.load(settings_file)
    settings_file.close()

    settings['limit'][resource] = limit

    with open(file_path, 'w') as f:
        json.dump(settings, f)
Esempio n. 6
0
def load_projects(projects_path):
    """Load all project YAML files

    It modifies global dict PROJECTS

    :param str projects_path: YAML files directory
    """

    global PROJECTS

    # Get the base path to check for subdirectories presence
    try:
        projects_dir = os.walk(projects_path).next()
    except StopIteration:
        raise ArgumentError("Projects path not found")

    if not len(projects_dir[1]):
        raise ArgumentError("No project directory found")

    # setup projects
    for project_name in projects_dir[1]:
        project_path = generate_path([projects_path, project_name])
        logger.info('Project: {}'.format(project_path))
        appli_files = os.listdir(project_path)
        try:
            file_id = appli_files.index('defaults.yml')
            default_file = appli_files.pop(file_id)

            default_path = generate_path([project_path, default_file])
            if is_yaml_file(default_path):
                project_struct = load_yaml_file(default_path)
                PROJECTS[project_name] = Project(project_name, project_struct)
        except ValueError:
            PROJECTS[project_name] = Project(project_name)

        # setup applications
        for appli_file in appli_files:
            appli_path = generate_path([project_path, appli_file])
            logger.info('Application: {}'.format(appli_path))
            if not is_yaml_file(appli_path):
                logger.info('ignore {}'.format(appli_path))
                continue
            appli_info = load_yaml_file(appli_path)
            appli_name = appli_file.split('.')[0]
            PROJECTS[project_name].add_application(appli_name, appli_info)
Esempio n. 7
0
    def cmd_edit(self, args):
        if len(args) < 1:
            raise ArgumentError(
                "git tessera edit takes one or more identifier as argument")

        tessera_paths = []
        for key in args:
            tessera_path = None
            found = False
            for i in os.listdir(Tessera._tesserae):
                tessera_path = "%s/%s" % (Tessera._tesserae, i)
                if not stat.S_ISDIR(os.lstat(tessera_path).st_mode):
                    continue
                if i.split('-')[0] == key or i == key:
                    found = True
                    break
            if not found:
                raise TesseraError("git tessera %s not found" % key)

            tessera_paths.append(tessera_path)

        while True:
            tessera_files = ["%s/tessera" % x for x in tessera_paths]
            _edit(tessera_files, self._config)

            # if self.git.is_dirty():
            failed = []
            while tessera_paths:
                tessera_path = tessera_paths.pop()
                t = Tessera(tessera_path, self._config)
                if not t.error:
                    t._write_info()
                    files = [
                        "%s/tessera" % tessera_path,
                        "%s/info" % tessera_path
                    ]
                    self.git.add(
                        files,
                        "tessera updated: %s" % t.get_attribute("title"))
                    continue
                # failed parsing
                failed.append(tessera_path)

            if failed:
                stdout.write("Abort ? [y/N] ")
                try:
                    answer = stdin.readline().strip()
                except KeyboardInterrupt:
                    break
                if answer and answer.lower() == "y":
                    break
                tessera_paths = failed
            else:
                break

        return True
Esempio n. 8
0
    def cmd_config(self, args):
        if len(args) < 1:
            raise ArgumentError(
                "specify minimum one argument to read the config's value")

        setting = args[0].split(".")
        if len(setting) < 2:
            raise ArgumentError(
                "to set a config value you have to use the schema: section.option"
            )

        if len(args) > 1:
            self._config.set(setting[0], setting[1], args[1])
            self._config.store()
            return True

        option = self._config.get(setting[0], ".".join(setting[1:]))
        print("%s has value %s" % (args[0], option))
        return True
    def start(self, name):
        """
        Start a timer.

        :param name: Name of the timer
        :return: -
        """

        if name not in self.counters.keys():
            raise ArgumentError("Attempt to start timer with undefined name")
        else:
            self.counters[name][1] = time()
    def reset(self, name):
        """
        Reset a timer to zero without deleting it.

        :param name: Name of the timer.
        :return: -
        """

        if name not in self.counters.keys():
            raise ArgumentError("Attempt to reset timer with undefined name")
        else:
            self.counters[name][0] = 0.
    def delete(self, name):
        """
        Delete a named timer.

        :param name: Name of the timer
        :return: -
        """

        if name not in self.counters.keys():
            raise ArgumentError("Attempt to delete timer with undefined name")
        else:
            del self.counters[name]
Esempio n. 12
0
    def set_roi(self, y_min, y_max, x_min, x_max):
        """
        Make the stacking region snmaller than the intersection size. Be careful: The pixel
        indices in this method refer to the shape of the intersection of all frames, i.e., the
        shape of the full mean frame. In general, the original frames are somewhat larger.

        If all four index bounds are zero, set the ROI to the full frame.

        :param y_min: Lower y pixel bound
        :param y_max: Upper y pixel bound
        :param x_min: Lower x pixel bound
        :param x_max: Upper x pixel bound
        :return: The new averaged frame, restricted to the ROI
        """

        if self.intersection_shape is None:
            raise WrongOrderingError("Method 'set_roi' is called before 'align_frames'")

        # On the first call, keep a copy of the full mean frame and original intersection shape.
        if not self.ROI_set:
            self.mean_frame_original = self.mean_frame.copy()
            self.intersection_shape_original = self.intersection_shape.copy()

        if y_min == 0 and y_max == 0 and x_min == 0 and x_max == 0:
            y_min = 0
            y_max = self.intersection_shape_original[0][1] - \
                self.intersection_shape_original[0][0]
            x_min = 0
            x_max = self.intersection_shape_original[1][1] - \
                self.intersection_shape_original[1][0]
        elif y_min < 0 or y_max > self.intersection_shape_original[0][1] - \
                self.intersection_shape_original[0][0] or \
                x_min < 0 or x_max > self.intersection_shape_original[1][1] - \
                self.intersection_shape_original[1][0] or \
                y_min >= y_max or x_min >= x_max:
            raise ArgumentError("Invalid ROI index bounds specified")

        # Reduce the intersection shape and mean frame to the ROI.
        self.intersection_shape = [[y_min+self.intersection_shape_original[0][0],
                                    y_max+self.intersection_shape_original[0][0]],
                                   [x_min+self.intersection_shape_original[1][0],
                                    x_max+self.intersection_shape_original[1][0]]]

        # Re-compute global offsets of current frame relative to reference frame.
        self.dy = [self.intersection_shape[0][0] - self.frame_shifts[idx][0] for idx in range(self.frames.number)]
        self.dx = [self.intersection_shape[1][0] - self.frame_shifts[idx][1] for idx in range(self.frames.number)]

        self.ROI_set = True

        self.mean_frame = self.mean_frame_original[y_min:y_max, x_min:x_max]

        return self.mean_frame
    def stop(self, name):
        """
        Stop a timer.

        :param name: Name of the timer
        :return: Elapsed time accumulated during start/stop intervals of this timer so far.
        """

        if name not in self.counters.keys():
            raise ArgumentError("Attempt to stop timer with undefined name")
        else:
            self.counters[name][0] += time() - self.counters[name][1]
            return self.counters[name][0]
    def read(self, name):
        """
        Read out a timer.

        :param name: Name of the timer
        :return: Elapsed time accumulated during start/stop intervals of this timer so far.
        """

        if name not in self.counters.keys():
            raise ArgumentError(
                "Attempt to read out timer with undefined name")
        else:
            return self.counters[name][0]
    def create(self, name):
        """
        Create a named timer, and start it.

        :param name: Name of the timer
        :return: -
        """

        if name in self.counters.keys():
            raise ArgumentError(
                "Attempt to initialize timer with existing name")
        else:
            self.counters[name] = [0., time()]
    def frames_mono_blurred_laplacian(self, index):
        """
        Look up a Laplacian-of-Gaussian of a frame object with a given index.

        :param index: Frame index
        :return: LoG of a frame with index "index".
        """

        if not 0 <= index < self.number:
            raise ArgumentError("Frame index " + str(index) + " is out of bounds")
        if self.frames_monochrome_blurred_laplacian is not None:
            # print("Accessing LoG number " + str(index))
            return self.frames_monochrome_blurred_laplacian[index]
        else:
            raise WrongOrderingError("Attempt to look up a LoG frame version before computing it")
    def frames_mono(self, index):
        """
        Look up the monochrome version of the frame object with a given index.

        :param index: Frame index
        :return: Monochrome frame with index "index".
        """

        if not 0 <= index < self.number:
            raise ArgumentError("Frame index " + str(index) + " is out of bounds")
        if self.frames_monochrome is not None:
            # print("Accessing frame monochrome " + str(index))
            return self.frames_monochrome[index]
        else:
            raise WrongOrderingError("Attempt to look up a monochrome frame before computing it")
    def frames_mono_blurred(self, index):
        """
        Look up a Gaussian-blurred frame object with a given index.

        :param index: Frame index
        :return: Gaussian-blurred frame with index "index".
        """

        if not 0 <= index < self.number:
            raise ArgumentError("Frame index " + str(index) +
                                " is out of bounds")

        # print("Accessing frame with Gaussian blur " + str(index))
        # The blurred frames are buffered, and this frame has been stored before. Just return
        # the frame.
        if self.frames_monochrome_blurred[index] is not None:
            return self.frames_monochrome_blurred[index]

        # If the blurred frame is cached, just return it.
        if self.gaussian_available_index == index:
            return self.gaussian_available

        # The frame has not been stored for re-use, compute it.
        else:

            # Get the monochrome frame. If it is not cached, this involves I/O.
            frame_mono = self.frames_mono(index)

            # If the mono image is 8bit, interpolate it to 16bit.
            if frame_mono.dtype == uint8:
                frame_mono = frame_mono.astype(uint16) * 256

            # Compute a version of the frame with Gaussian blur added.
            frame_monochrome_blurred = GaussianBlur(
                frame_mono, (self.configuration.frames_gauss_width,
                             self.configuration.frames_gauss_width), 0)

            # If the blurred frames are buffered, store the current frame at the current index.
            if self.buffer_gaussian:
                self.frames_monochrome_blurred[
                    index] = frame_monochrome_blurred

            # If frames are not buffered, cache the current frame.
            else:
                self.gaussian_available_index = index
                self.gaussian_available = frame_monochrome_blurred

            return frame_monochrome_blurred
Esempio n. 19
0
    def cmd_init(self, args):
        if len(args) != 0:
            raise ArgumentError("git tessera init takes no arguments")

        if os.path.exists(Tessera._tesserae):
            raise TesseraError("git tesserae directory already exists: %s" %
                               Tessera._tesserae)

        os.mkdir(Tessera._tesserae)

        files = []
        for source in ["template", "config"]:
            files.append(_install(Tessera._tesserae, source))

        self.git.add(files, "tessera: initialized")
        return True
    def frames_mono_blurred(self, index):
        """
        Look up a Gaussian-blurred frame object with a given index.

        :param index: Frame index
        :return: Gaussian-blurred frame with index "index".
        """

        if not 0 <= index < self.number:
            raise ArgumentError("Frame index " + str(index) + " is out of bounds")
        if self.frames_monochrome_blurred is not None:
            # print("Accessing frame with Gaussian blur " + str(index))
            return self.frames_monochrome_blurred[index]
        else:
            raise WrongOrderingError("Attempt to look up a Gaussian-blurred frame version before"
                                     " computing it")
    def frames_mono_blurred_laplacian(self, index):
        """
        Look up a Laplacian-of-Gaussian of a frame object with a given index.

        :param index: Frame index
        :return: LoG of a frame with index "index".
        """

        if not 0 <= index < self.number:
            raise ArgumentError("Frame index " + str(index) +
                                " is out of bounds")

        # print("Accessing LoG number " + str(index))
        # The LoG frames are buffered, and this frame has been stored before. Just return the frame.
        if self.frames_monochrome_blurred_laplacian[index] is not None:
            return self.frames_monochrome_blurred_laplacian[index]

        # If the blurred frame is cached, just return it.
        if self.laplacian_available_index == index:
            return self.laplacian_available

        # The frame has not been stored for re-use, compute it.
        else:

            # Get the monochrome frame. If it is not cached, this involves I/O.
            frame_monochrome_blurred = self.frames_mono_blurred(index)

            # Compute a version of the frame with Gaussian blur added.
            frame_monochrome_laplacian = convertScaleAbs(Laplacian(
                frame_monochrome_blurred[::self.configuration.
                                         align_frames_sampling_stride, ::self.
                                         configuration.
                                         align_frames_sampling_stride],
                CV_32F),
                                                         alpha=self.alpha)

            # If the blurred frames are buffered, store the current frame at the current index.
            if self.buffer_laplacian:
                self.frames_monochrome_blurred_laplacian[
                    index] = frame_monochrome_laplacian

            # If frames are not buffered, cache the current frame.
            else:
                self.laplacian_available_index = index
                self.laplacian_available = frame_monochrome_laplacian

            return frame_monochrome_laplacian
Esempio n. 22
0
def remove_email(email):
    """
    Remove name and email address
    """
    file_path = os.path.join(os.path.expanduser('~'), '.sms/settings.json')
    settings_file = open(file_path)
    settings = json.load(settings_file)
    settings_file.close()

    if email not in settings['email']:
        msg = 'email provided does not exist.'
        raise ArgumentError(msg)

    del settings['email'][email]

    with open(file_path, 'w') as f:
        json.dump(settings, f)
Esempio n. 23
0
    def cmd_show(self, args):
        if len(args) != 1:
            raise ArgumentError(
                "git tessera show takes identifier as argument")

        gt = GitTessera(self._config)
        t = gt.get(args[0])
        if not t:
            return False

        short = t.summary()
        length = len(short)
        print "=" * length
        print short
        print "=" * length
        print t.content
        return True
    def frames(self, index):
        """
        Look up the original frame object with a given index.

        :param index: Frame index
        :return: Frame with index "index".
        """

        # # Code for positioning at arbitrary index.
        # # Check for valid frame number.
        # if 0 <= index < self.number:
        #     # Set frame position
        #     cap.set(cv2.CAP_PROP_POS_FRAMES, index)

        if not 0<=index<self.number:
            raise ArgumentError("Frame index " + str(index) + " is out of bounds")
        # print ("Accessing frame " + str(index))
        return self.frames_original[index]
Esempio n. 25
0
    def extract_channel(self, index, color):
        """
        Extract a color channel from an RGB frame.

        :param index: Frame index
        :param color: Either "red" or "green" or "blue"
        :return: 2D array with the selected color channel of the frame with index "index"
        """

        if not self.color:
            raise ShapeError(
                "Cannot extract color channel from monochrome image")
        colors = ['red', 'green', 'blue']
        if not color in colors:
            raise ArgumentError(
                "Invalid color selected for channel extraction")

        # Collapse the 3D array by the color dimension.
        return self.frames[index][:, :, colors.index(color)]
Esempio n. 26
0
def modify_email(old_name, old_email, new_name, new_email):
    """
    Modify saved name and email address
    """
    file_path = os.path.join(os.path.expanduser('~'), '.sms/settings.json')
    settings_file = open(file_path)
    settings = json.load(settings_file)
    settings_file.close()

    if old_email not in settings['email']:
        msg = 'email provided does not exist.'
        raise ArgumentError(msg)

    del settings['email'][old_email]

    settings['email'][new_email] = new_name

    with open(file_path, 'w') as f:
        json.dump(settings, f)
Esempio n. 27
0
    def add_monochrome(self, color):
        """
        Same as method "extract_channel", but for all frames. Add a list of monochrome frames
        "self.frames_mono". If the original frames are monochrome, just point the monochrome frame
        list to the original images (no deep copy!).

        :param color: Either "red" or "green" or "blue"
        :return: -
        """

        if self.color:
            colors = ['red', 'green', 'blue']
            if not color in colors:
                raise ArgumentError(
                    "Invalid color selected for channel extraction")
            self.frames_mono = [
                frame[:, :, colors.index(color)] for frame in self.frames
            ]
        else:
            self.frames_mono = self.frames
Esempio n. 28
0
def update_email(action, *args):
    if action not in ['add', 'modify', 'remove']:
        msg = """
Invalid action entered.

Valid actions are:
 - add
 - modify
 - remove
"""
        raise ArgumentError(msg)

    password = keyring.get_password('sms_password', 'Administrator')
    if password == None:
        print(
            'No Password set.\nSet a new password using `sms --update-password`.'
        )
        return

    user_password = getpass.getpass()
    if user_password != password:
        print('\nWrong Password entered')
        return

    if action == 'add':
        if len(args) != 2:
            raise ArgumentError
        add_email(*args)
    elif action == 'modify':
        if len(args) != 4:
            raise ArgumentError
        modify_email(*args)
    elif action == 'remove':
        if len(args) != 1:
            print(args)
            raise ArgumentError
        remove_email(*args)
    def add_monochrome(self, color):
        """
        Create monochrome versions of all frames. Add a list of monochrome frames
        "self.frames_mono". If the original frames are monochrome, just point the monochrome frame
        list to the original images (no deep copy!). Also, add a blurred version of the frame list
        (using a Gaussian filter) "self.frames_mono_blurred", and the Laplacian of that image.

        :param color: Either "red" or "green", "blue", or "panchromatic"
        :return: -
        """

        if self.color:
            colors = ['red', 'green', 'blue', 'panchromatic']
            if not color in colors:
                raise ArgumentError("Invalid color selected for channel extraction")
            self.frames_monochrome = []
        elif self.depth == 8:
            self.frames_monochrome = self.frames_original

        self.frames_monochrome_blurred = []
        self.frames_monochrome_blurred_laplacian = []

        for frame_index, frame in enumerate(self.frames_original):
            # After every "signal_step_size"th frame, send a progress signal to the main GUI.
            if self.progress_signal is not None and frame_index % self.signal_step_size == 0:
                self.progress_signal.emit("Gaussians / Laplacians",
                                          int((frame_index / self.number) * 100.))

            # If frames are in color mode, or the depth is 16bit, produce a 8bit B/W version.
            if self.color or self.depth != 8:
                # If frames are in color mode, create a monochrome version with same depth.
                if self.color:
                    if color == 'panchromatic':
                        frame_mono = cvtColor(frame, COLOR_BGR2GRAY)
                    else:
                        frame_mono = frame[:, :, colors.index(color)]
                else:
                    frame_mono = frame

                # If depth is larger than 8bit, reduce the depth to 8bit.
                if self.depth != 8:
                    frame_mono = ((frame_mono) / 255.).astype(np.uint8)

                self.frames_monochrome.append(frame_mono)

            # Add a version of the frame with Gaussian blur added.
            frame_monochrome_blurred = GaussianBlur(self.frames_monochrome[frame_index], (
                self.configuration.frames_gauss_width, self.configuration.frames_gauss_width), 0)
            self.frames_monochrome_blurred.append(frame_monochrome_blurred)

            # Compute the scaling factor for "convertScaleAbs" depending on the data type of the
            # monochrome image. The Laplacian is 8bit. If the monochrome image is 16bit, values
            # have to be scaled down.

            # Add the Laplacian of the down-sampled blurred image.
            if self.configuration.rank_frames_method == "Laplace":
                self.frames_monochrome_blurred_laplacian.append(convertScaleAbs(Laplacian(
                    frame_monochrome_blurred[::self.configuration.align_frames_sampling_stride,
                    ::self.configuration.align_frames_sampling_stride], CV_32F),
                    alpha=1))

        if self.progress_signal is not None:
            self.progress_signal.emit("Gaussians / Laplacians", 100)
Esempio n. 30
0
def create_report(resource):
    pdf = FPDF(orientation='P', format='A4')
    try:
        pdf.add_font('Montserrat',
                     '',
                     os.path.join(os.path.expanduser('~'),
                                  '.sms/Montserrat-Regular.ttf'),
                     uni=True)
        pdf.add_font('Montserrat',
                     'B',
                     os.path.join(os.path.expanduser('~'),
                                  '.sms/Montserrat-Bold.ttf'),
                     uni=True)
        font_style = 'Montserrat'
    except:
        font_style = 'Arial'

    g = core.Get()

    if resource == 'Summary':
        pdf.add_page()
        pdf.set_author('SMS')

        pdf.set_font(font_style, 'B', 10)
        pdf.cell(0, txt='System Monitoring System', ln=1, align='C')

        pdf.set_font(font_style, 'B', 32)
        pdf.cell(0, h=25, txt='Report', ln=1, align='C')

        pdf.set_font(font_style, 'B', 20)
        pdf.cell(0, h=15, txt='Overall Usage Summary - ', ln=1)

        pdf.set_font(font_style, '', 12)
        text = 'OS: {}'.format(g.os())
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Uptime: {} seconds'.format(g.uptime())
        pdf.cell(0, h=5, txt=text, ln=1)
        pdf.ln()

        # CPU data
        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='CPU:-', ln=1)
        pdf.set_font(font_style, '', 12)
        cpu_dict = g.cpu()
        if os.name != 'nt':
            text = 'Load Average: {} {} {}'.format(cpu_dict['load_avg'][0],
                                                   cpu_dict['load_avg'][1],
                                                   cpu_dict['load_avg'][2])
            pdf.cell(0, h=5, txt=text, ln=1)
        text = 'User: {} %'.format(cpu_dict['user'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'System: {} %'.format(cpu_dict['system'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Idle: {} %'.format(cpu_dict['idle'])
        pdf.cell(0, h=5, txt=text, ln=1)
        if os.name != 'nt':
            text = 'I/O Wait: {} %'.format(cpu_dict['iowait'])
            pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Cores: {}'.format(cpu_dict['num_cores'])
        pdf.cell(0, h=5, txt=text, ln=1)
        pdf.ln()

        # Memory data
        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Memory:-', ln=1)
        pdf.set_font(font_style, '', 12)
        memory_dict = g.memory()
        text = 'Total: {:,} Bytes'.format(memory_dict['total'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Used: {:,} Bytes'.format(memory_dict['used_incl'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Free: {:,} Bytes'.format(memory_dict['free'])
        pdf.cell(0, h=5, txt=text, ln=1)
        pdf.ln()

        # Network data
        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Network:-', ln=1)
        pdf.set_font(font_style, '', 12)
        networks = g.network()
        for network_dict in networks:
            text = 'Interface: {}'.format(network_dict['interface'])
            pdf.cell(0, h=5, txt=text, ln=1)
            if os.name != 'nt':
                text = 'IP: {}'.format(network_dict['ip'])
                pdf.cell(0, h=5, txt=text, ln=1)
            pdf.ln()

        # Storage data
        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Storage:-', ln=1)
        pdf.set_font(font_style, '', 12)
        storages = g.storage()
        for storage_dict in storages:
            text = 'Device: {}'.format(storage_dict['device'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Mounted: {}'.format(storage_dict['mountpoint'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Total: {:,} Bytes'.format(storage_dict['total'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Used: {:,} Bytes ({}%)'.format(storage_dict['used'],
                                                   storage_dict['percent'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Free: {:,} Bytes'.format(storage_dict['free'])
            pdf.cell(0, h=5, txt=text, ln=1)
            pdf.ln()

        # Swap data
        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Swap:-', ln=1)
        pdf.set_font(font_style, '', 12)
        swap_dict = g.swap()
        text = 'Total: {:,} Bytes'.format(swap_dict['total'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Used: {:,} Bytes ({}%)'.format(swap_dict['used'],
                                               swap_dict['percent'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Free: {:,} Bytes'.format(swap_dict['free'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Swapped in: {:,} Bytes'.format(swap_dict['sin'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Swapped out: {:,} Bytes'.format(swap_dict['sout'])
        pdf.cell(0, h=5, txt=text, ln=1)
        pdf.ln()

        # Users data
        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Users:-', ln=1)
        pdf.set_font(font_style, '', 12)
        users = g.users()
        for user_dict in users:
            text = 'Name: {}'.format(user_dict['name'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Session started: {}'.format(user_dict['sess_started'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Host: {}'.format(user_dict['host'])
            pdf.cell(0, h=5, txt=text, ln=1)
            pdf.ln()

    elif resource == 'CPU':
        pdf.add_page()
        pdf.set_author('SMS')

        pdf.set_font(font_style, 'B', 10)
        pdf.cell(0, txt='System Monitoring System', ln=1, align='C')

        pdf.set_font(font_style, 'B', 32)
        pdf.cell(0, h=25, txt='Report', ln=1, align='C')

        pdf.set_font(font_style, 'B', 20)
        pdf.cell(0, h=15, txt='CPU Usage Summary - ', ln=1)

        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Overall:-', ln=1)
        pdf.set_font(font_style, '', 12)
        cpu_dict = g.cpu()
        if os.name != 'nt':
            text = 'Load Average: {} {} {}'.format(cpu_dict['load_avg'][0],
                                                   cpu_dict['load_avg'][1],
                                                   cpu_dict['load_avg'][2])
            pdf.cell(0, h=5, txt=text, ln=1)
        text = 'User: {} %'.format(cpu_dict['user'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'System: {} %'.format(cpu_dict['system'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Idle: {} %'.format(cpu_dict['idle'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'I/O Wait: {} %'.format(cpu_dict['iowait'])
        pdf.cell(0, h=5, txt=text, ln=1)

        counter = 1
        for cpu_core in cpu_dict['cores']:
            pdf.ln()
            pdf.set_font(font_style, 'B', 16)
            text = 'Core {}:-'.format(counter)
            pdf.cell(0, h=15, txt=text, ln=1)
            pdf.set_font(font_style, '', 12)
            text = 'User: {} %'.format(cpu_core['user'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'System: {} %'.format(cpu_core['system'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Idle: {} %'.format(cpu_core['idle'])
            pdf.cell(0, h=5, txt=text, ln=1)
            if os.name != 'nt':
                text = 'I/O Wait: {} %'.format(cpu_core['iowait'])
                pdf.cell(0, h=5, txt=text, ln=1)
            counter += 1

    elif resource == 'Memory':
        pdf.add_page()
        pdf.set_author('SMS')

        pdf.set_font(font_style, 'B', 10)
        pdf.cell(0, txt='System Monitoring System', ln=1, align='C')

        pdf.set_font(font_style, 'B', 32)
        pdf.cell(0, h=25, txt='Report', ln=1, align='C')

        pdf.set_font(font_style, 'B', 20)
        pdf.cell(0, h=15, txt='Memory Usage Summary - ', ln=1)

        pdf.set_font(font_style, '', 12)
        memory_dict = g.memory()
        text = 'Total: {:,} Bytes'.format(memory_dict['total'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Available: {:,} Bytes'.format(memory_dict['available'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Used (excl. Cache & buffer): {:,} Bytes ({}%)'.format(
            memory_dict['used_excl'], memory_dict['percent'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Used (incl. Cache & buffer): {:,} Bytes'.format(
            memory_dict['used_incl'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Free: {:,} Bytes'.format(memory_dict['free'])
        pdf.cell(0, h=5, txt=text, ln=1)

    elif resource == 'Network':
        pdf.add_page()
        pdf.set_author('SMS')

        pdf.set_font(font_style, 'B', 10)
        pdf.cell(0, txt='System Monitoring System', ln=1, align='C')

        pdf.set_font(font_style, 'B', 32)
        pdf.cell(0, h=25, txt='Report', ln=1, align='C')

        pdf.set_font(font_style, 'B', 20)
        pdf.cell(0, h=15, txt='Network Usage Summary - ', ln=1)

        pdf.set_font(font_style, '', 12)
        networks = g.network()
        for network_dict in networks:
            text = 'Interface: {}'.format(network_dict['interface'])
            pdf.cell(0, h=5, txt=text, ln=1)
            if os.name != 'nt':
                text = 'IP: {}'.format(network_dict['ip'])
                pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Bytes sent: {}'.format(network_dict['bytes_sent'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Bytes received: {}'.format(network_dict['bytes_recv'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Packets sent: {}'.format(network_dict['packets_sent'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Packets received: {}'.format(network_dict['packets_recv'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Errors in: {}'.format(network_dict['errin'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Errors out: {}'.format(network_dict['errout'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Dropped in: {}'.format(network_dict['dropin'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Dropped out: {}'.format(network_dict['dropout'])
            pdf.cell(0, h=5, txt=text, ln=1)
            pdf.ln()

        speed_dict = core.test_speed()

        text = 'Download speed: {}'.format(speed_dict['down_speed'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Upload speed: {}'.format(speed_dict['up_speed'])
        pdf.cell(0, h=5, txt=text, ln=1)

    elif resource == 'Storage':
        pdf.add_page()
        pdf.set_author('SMS')

        pdf.set_font(font_style, 'B', 10)
        pdf.cell(0, txt='System Monitoring System', ln=1, align='C')

        pdf.set_font(font_style, 'B', 32)
        pdf.cell(0, h=25, txt='Report', ln=1, align='C')

        pdf.set_font(font_style, 'B', 20)
        pdf.cell(0, h=15, txt='Storage Usage Summary - ', ln=1)

        pdf.set_font(font_style, '', 12)
        storages = g.storage()
        for storage_dict in storages:
            text = 'Device: {}'.format(storage_dict['device'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Mounted: {}'.format(storage_dict['mountpoint'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Type: {}'.format(storage_dict['fstype'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Options: {}'.format(storage_dict['options'])
            pdf.multi_cell(0, h=5, txt=text)
            text = 'Total: {:,} Bytes'.format(storage_dict['total'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Used: {:,} Bytes ({}%)'.format(storage_dict['used'],
                                                   storage_dict['percent'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Free: {:,} Bytes'.format(storage_dict['free'])
            pdf.cell(0, h=5, txt=text, ln=1)
            pdf.ln()

    elif resource == 'Process':
        pdf.add_page()
        pdf.set_author('SMS')

        pdf.set_font(font_style, 'B', 10)
        pdf.cell(0, txt='System Monitoring System', ln=1, align='C')

        pdf.set_font(font_style, 'B', 32)
        pdf.cell(0, h=25, txt='Report', ln=1, align='C')

        pdf.set_font(font_style, 'B', 20)
        pdf.cell(0, h=15, txt='Process Usage Summary - ', ln=1)

        pdf.set_font(font_style, '', 12)
        processes = g.process()
        for process_dict in processes:
            text = 'PID: {}'.format(process_dict['pid'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Name: {}'.format(process_dict['name'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'User: {}'.format(process_dict['user'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Status: {}'.format(process_dict['status'])
            pdf.multi_cell(0, h=5, txt=text)
            text = 'Created: {} seconds since the epoch'.format(
                process_dict['created'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Memory: {} %'.format(process_dict['memory'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'CPU: {} %'.format(process_dict['cpu'])
            pdf.cell(0, h=5, txt=text, ln=1)
            pdf.ln()

    elif resource == 'Miscellaneous':
        pdf.add_page()
        pdf.set_author('SMS')

        pdf.set_font(font_style, 'B', 10)
        pdf.cell(0, txt='System Monitoring System', ln=1, align='C')

        pdf.set_font(font_style, 'B', 32)
        pdf.cell(0, h=25, txt='Report', ln=1, align='C')

        pdf.set_font(font_style, 'B', 20)
        pdf.cell(0, h=15, txt='Miscellaneous Usage Summary - ', ln=1)

        pdf.set_font(font_style, '', 12)
        text = 'OS: {}'.format(g.os())
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Uptime: {} seconds'.format(g.uptime())
        pdf.cell(0, h=5, txt=text, ln=1)
        pdf.ln()

        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Users:-', ln=1)
        pdf.set_font(font_style, '', 12)
        users = g.users()
        for user_dict in users:
            text = 'Name: {}'.format(user_dict['name'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Session started: {}'.format(user_dict['sess_started'])
            pdf.cell(0, h=5, txt=text, ln=1)
            text = 'Host: {}'.format(user_dict['host'])
            pdf.cell(0, h=5, txt=text, ln=1)
            pdf.ln()

        pdf.set_font(font_style, 'B', 16)
        pdf.cell(0, h=15, txt='Swap:-', ln=1)
        pdf.set_font(font_style, '', 12)
        swap_dict = g.swap()
        text = 'Total: {:,} Bytes'.format(swap_dict['total'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Used: {:,} Bytes ({}%)'.format(swap_dict['used'],
                                               swap_dict['percent'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Free: {:,} Bytes'.format(swap_dict['free'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Swapped in: {:,} Bytes'.format(swap_dict['sin'])
        pdf.cell(0, h=5, txt=text, ln=1)
        text = 'Swapped out: {:,} Bytes'.format(swap_dict['sout'])
        pdf.cell(0, h=5, txt=text, ln=1)

    else:
        msg = """
Invalid Resource Name provided.

The following options are available:
 - Summary
 - CPU
 - Memory
 - Network
 - Storage
 - Process
 - Miscellaneous
"""
        raise ArgumentError(msg)

    return pdf