Esempio n. 1
0
def debug_image(block_size, frame_base, list_predictive, list_differences,
                output_location):
    logger = logging.getLogger(__name__)

    difference_vectors = []
    predictive_vectors = []
    out_image = Frame()
    out_image.create_new(frame_base.width, frame_base.height)
    out_image.copy_image(frame_base)

    black_image = Frame()
    black_image.create_new(frame_base.width, frame_base.height)

    if not list_predictive and not list_differences:
        out_image.save_image(output_location)
        return

    if list_predictive and not list_differences:
        out_image.copy_image(frame_base)
        out_image.save_image(output_location)
        return

    # load list into vector displacements
    for x in range(int(len(list_differences) / 4)):
        difference_vectors.append(
            DisplacementVector(int(list_differences[x * 4]),
                               int(list_differences[x * 4 + 1]),
                               int(list_differences[x * 4 + 2]),
                               int(list_differences[x * 4 + 3])))
    for x in range(int(len(list_predictive) / 4)):
        if (int(list_predictive[x * 4 + 0]) != int(list_predictive[x * 4 + 1])) and \
                (int(list_predictive[x * 4 + 2]) != int(list_predictive[x * 4 + 3])):
            predictive_vectors.append(
                DisplacementVector(int(list_predictive[x * 4 + 0]),
                                   int(list_predictive[x * 4 + 1]),
                                   int(list_predictive[x * 4 + 2]),
                                   int(list_predictive[x * 4 + 3])))

    # copy over predictive vectors into new image
    for vector in difference_vectors:
        out_image.copy_block(black_image, block_size, vector.x_1, vector.y_1,
                             vector.x_1, vector.y_1)

    out_image.save_image_quality(output_location, 25)
Esempio n. 2
0
    def run(self):
        # start from 1 because ffmpeg's extracted frames starts from 1
        for x in range(self.start_frame, self.frame_count + 1):

            # loading files area
            frame = Frame()
            frame.load_from_string_wait(self.inputs_dir + "frame" + str(x) + self.extension_type, self.cancel_token)

            # stop if thread was killed
            if not self.alive:
                return

            # if the compressed frame already exists, don't compress it
            if os.path.exists(self.compressed_static_dir + "compressed_" + str(x) + ".jpg"):
                continue

            frame.save_image_quality(self.compressed_static_dir + "compressed_" + str(x) + ".jpg",
                                     self.quality_minimum)
            frame.save_image_quality(self.compressed_moving_dir + "compressed_" + str(x) + ".jpg",
                                     int(self.quality_minimum * self.quality_moving_ratio))
Esempio n. 3
0
def compress_frames(context: Context):
    inputs_dir = context.input_frames_dir
    frame_count = context.frame_count
    quality_moving_ratio = context.quality_moving_ratio
    compressed_static_dir = context.compressed_static_dir
    compressed_moving_dir = context.compressed_moving_dir
    quality_minimum = context.quality_minimum
    extension_type = context.extension_type

    for x in range(1, frame_count + 1):
        if os.path.exists(compressed_static_dir + "compressed_" + str(x) +
                          ".jpg"):
            continue

        frame = Frame()
        frame.load_from_string(inputs_dir + "frame" + str(x) + extension_type)
        frame.save_image_quality(
            compressed_static_dir + "compressed_" + str(x) + ".jpg",
            quality_minimum)
        frame.save_image_quality(
            compressed_moving_dir + "compressed_" + str(x) + ".jpg",
            int(quality_minimum * quality_moving_ratio))
Esempio n. 4
0
def compress_frames(context: Context):
    """
    Use frame's save_image_quality function to save a series of compressed images, which are used in
    Dandere2x_Cpp as a loss function. This function on it's own is a bit esoteric - I recommend reading
    the white paper to understand why we need to compress these frames.

    Input:
        - context

    Output:
        - All the images in 'input_frames' compressed into two different folders, each with their own
          level of compression.
    """

    inputs_dir = context.input_frames_dir
    frame_count = context.frame_count
    quality_moving_ratio = context.quality_moving_ratio
    compressed_static_dir = context.compressed_static_dir
    compressed_moving_dir = context.compressed_moving_dir
    quality_minimum = context.quality_minimum
    extension_type = context.extension_type

    # start from 1 because ffmpeg's extracted frames starts from 1
    for x in range(1, frame_count + 1):

        # if the compressed frame already exists, don't compress it
        if os.path.exists(compressed_static_dir + "compressed_" + str(x) +
                          ".jpg"):
            continue

        frame = Frame()
        frame.load_from_string(inputs_dir + "frame" + str(x) + extension_type)
        frame.save_image_quality(
            compressed_static_dir + "compressed_" + str(x) + ".jpg",
            quality_minimum)
        frame.save_image_quality(
            compressed_moving_dir + "compressed_" + str(x) + ".jpg",
            int(quality_minimum * quality_moving_ratio))
Esempio n. 5
0
    def debug_image(block_size, frame_base, list_predictive, list_differences,
                    output_location):
        """
        Note:
            I haven't made an effort to maintain this method, as it's only for debugging.

        This section can best be explained through pictures. A visual way of expressing what 'debug'
        is doing is this section in the wiki.

        https://github.com/aka-katto/dandere2x/wiki/How-Dandere2x-Works#part-1-identifying-what-needs-to-be-drawn

        In other words, this method shows where residuals are, and is useful for finding good settings to use for a video.

        Inputs:
            - frame(x)
            - Residual vectors mapping frame(x)_residual -> frame(x)

        Output:
            - frame(x) minus frame(x)_residuals = debug_image
        """
        logger = logging.getLogger(__name__)

        difference_vectors = []
        predictive_vectors = []
        out_image = Frame()
        out_image.create_new(frame_base.width, frame_base.height)
        out_image.copy_image(frame_base)

        black_image = Frame()
        black_image.create_new(frame_base.width, frame_base.height)

        if not list_predictive and not list_differences:
            out_image.save_image(output_location)
            return

        if list_predictive and not list_differences:
            out_image.copy_image(frame_base)
            out_image.save_image(output_location)
            return

        # load list into vector displacements
        for x in range(int(len(list_differences) / 4)):
            difference_vectors.append(
                DisplacementVector(int(list_differences[x * 4]),
                                   int(list_differences[x * 4 + 1]),
                                   int(list_differences[x * 4 + 2]),
                                   int(list_differences[x * 4 + 3])))
        for x in range(int(len(list_predictive) / 4)):
            if (int(list_predictive[x * 4 + 0]) != int(list_predictive[x * 4 + 1])) and \
                    (int(list_predictive[x * 4 + 2]) != int(list_predictive[x * 4 + 3])):
                predictive_vectors.append(
                    DisplacementVector(int(list_predictive[x * 4 + 0]),
                                       int(list_predictive[x * 4 + 1]),
                                       int(list_predictive[x * 4 + 2]),
                                       int(list_predictive[x * 4 + 3])))

        # copy over predictive vectors into new image
        for vector in difference_vectors:
            out_image.copy_block(black_image, block_size, vector.x_1,
                                 vector.y_1, vector.x_1, vector.y_1)

        out_image.save_image_quality(output_location, 25)