<commit_before># -*- coding: utf8 -*-

from __future__ import absolute_import, division, print_function

import numpy as np

from shot_detector.utils.collections import SmartDict

from shot_detector.utils.common import is_whole
from shot_detector.utils.numerical import shrink

from .base_extractor import BaseExtractor


# #
# # Size of vector, when we deal with computing.
# # For optimization issues it should be multiple by 2.
# # Perhaps it is better to put in `video_state`.
# #
DEFAULT_IMAGE_SIZE = SmartDict(
    width=16,
    height=16,
)

# #
# # For optimization issues it should be multiple by 8.
# # Perhaps it is better to put in `video_state`.
# #
DEFAULT_OPTIMIZE_FRAME_SIZE = SmartDict(
    width=16,
    height=16,
)

AV_FORMAT_COLOUR_SIZE = SmartDict(
    rgb24=(1 << 8),
    gray16le=(1 << 16),
)


class VectorBased(BaseExtractor):


    def __frame_features(self, frame_iterable, **kwargs):
        frame_repr_iterable = (frame.source for frame in frame_iterable)

    def optimize_reprs(self, frame_repr_iterable, av_format='rgb24', frame_size=DEFAULT_OPTIMIZE_FRAME_SIZE, **kwargs):

        for frame_repr in frame_repr_iterable:
            yield frame_repr.reformat(
                format=av_format,
                width=frame_size.width,
                height=frame_size.height,
            )


    def extract_frame_features(self, frame, video_state, *args, **kwargs):

        #print ('extract_frame_features frame = ', frame)

        image, video_state = self.build_image(frame, video_state)
        features, video_state = self.handle_image(image, video_state, *args, **kwargs)
        return features, video_state

    def handle_image(self, image, video_state, *args, **kwargs):
        image, video_state = self.transform_image(image, video_state)
        features, video_state = self.handle_transformed_image(image, video_state)
        video_state = self.store_sizes(image, video_state, *args, **kwargs)
        return features, video_state

    def handle_transformed_image(self, image, video_state, *args, **kwargs):
        features, video_state = self.build_features(image, video_state)
        features, video_state = self.handle_features(features, video_state)
        return features, video_state

    def handle_features(self, features, video_state, *args, **kwargs):
        features, video_state = self.transform_features(features, video_state)
        return features, video_state


    def build_image(self, frame, video_state, *args, **kwargs):
        vector, video_state = self.frame_to_image(frame, 'rgb24', video_state, *args, **kwargs)
        return vector, video_state

    def transform_image_size(self, vector, video_state, *args, **kwargs):
        image_size, video_state = self.get_image_size(video_state, *args, **kwargs)

        #print ('vector = ', vector)


        vector = shrink(vector, image_size.width, image_size.height)
        #print ('vector 2 = ', vector)

        return vector, video_state

    def frame_to_image(self, frame, av_format, video_state, *args, **kwargs):
        #print ('vector = ', frame, av_format, args, kwargs)

        optimized_frame, video_state = self.get_optimized_frame(<commit_msg>fix iter for filters<commit_after># -*- coding: utf8 -*-

from __future__ import absolute_import, division, print_function

import numpy as np

from shot_detector.utils.collections import SmartDict

from shot_detector.utils.common import is_whole
from shot_detector.utils.numerical import shrink

from .base_extractor import BaseExtractor


# #
# # Size of vector, when we deal with computing.
# # For optimization issues it should be multiple by 2.
# # Perhaps it is better to put in `video_state`.
# #
DEFAULT_IMAGE_SIZE = SmartDict(
    width=16,
    height=16,
)

# #
# # For optimization issues it should be multiple by 8.
# # Perhaps it is better to put in `video_state`.
# #
DEFAULT_OPTIMIZE_FRAME_SIZE = SmartDict(
    width=16,
    height=16,
)

AV_FORMAT_COLOUR_SIZE = SmartDict(
    rgb24=(1 << 8),
    gray16le=(1 << 16),
)


class VectorBased(BaseExtractor):


    def frame_features(self, frame_iterable, **kwargs):
        frame_repr_iterable = (frame.source for frame in frame_iterable)

    def optimize_reprs(self, frame_repr_iterable, av_format='rgb24', frame_size=DEFAULT_OPTIMIZE_FRAME_SIZE, **kwargs):

        for frame_repr in frame_repr_iterable:
            yield frame_repr.reformat(
                format=av_format,
                width=frame_size.width,
                height=frame_size.height,
            )


    def extract_frame_features(self, frame, video_state, *args, **kwargs):

        #print ('extract_frame_features frame = ', frame)

        image, video_state = self.build_image(frame, video_state)
        features, video_state = self.handle_image(image, video_state, *args, **kwargs)
        return features, video_state

    def handle_image(self, image, video_state, *args, **kwargs):
        image, video_state = self.transform_image(image, video_state)
        features, video_state = self.handle_transformed_image(image, video_state)
        video_state = self.store_sizes(image, video_state, *args, **kwargs)
        return features, video_state

    def handle_transformed_image(self, image, video_state, *args, **kwargs):
        features, video_state = self.build_features(image, video_state)
        features, video_state = self.handle_features(features, video_state)
        return features, video_state

    def handle_features(self, features, video_state, *args, **kwargs):
        features, video_state = self.transform_features(features, video_state)
        return features, video_state


    def build_image(self, frame, video_state, *args, **kwargs):
        vector, video_state = self.frame_to_image(frame, 'rgb24', video_state, *args, **kwargs)
        return vector, video_state

    def transform_image_size(self, vector, video_state, *args, **kwargs):
        image_size, video_state = self.get_image_size(video_state, *args, **kwargs)
        vector = shrink(vector, image_size.width, image_size.height)
        return vector, video_state

    def frame_to_image(self, frame, av_format, video_state, *args, **kwargs):
        #print ('vector = ', frame, av_format, args, kwargs)

        optimized_frame, video_state = self.get_optimized_frame(
{'fraction_non_alphanumeric': 0.09801744647105472, 'fraction_numerical': 0.007771609833465504, 'mean_word_length': 3.7235955056179777, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '523175', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1876, 'n_words': 483}