import cv2
import numpy

from ..utils.image_utils import read_image
from ..utils.histogram_analyzation import normalize
from ..utils.histogram_analyzation import calculate_peak_value

from filter import Filter


def get_input_vector(img):
    hist = cv2.calcHist([img], [0], None, [256], [0,255])
    hist = histgram_util.normalize(hist)

    return np.array([np.average(largest(calculate_peak_value(hist), 0.2))]).astype(np.float32)


class Posterized(Filter):
    """Filter for detecting posterized images"""

    name = 'posterized'
    speed = 1

    def __init__(self, threshold=0.5, invert_threshold=False, svm_file=None):
        """Initializes a posterized image filter

        :param threshold: threshold at which the given prediction is changed
                          from negative to positive
        :type threshold: float
        :param invert_threshold: whether the result should be greater than
                                 the given threshold (default) or lower
                                 for an image to be considered positive
        :type invert_threshold: bool
        """
        super(Posterized, self).__init__(threshold, invert_threshold)


    def predict(self, image_path, return_boolean=True, ROI=None):
        """Predict if a given image is posterized

        :param image_path: path to the image
        :type image_path: str
        :param return_boolean: whether to return the result as a
                               float between 0 and 1 or as a boolean
                               (threshold is given to the class)
        :type return_boolean: bool
        :param ROI: possible region of interest as a 4-tuple
                    (x0, y0, width, height), None if not needed
        :returns: the prediction as a bool or float depending on the
                  return_boolean parameter
        """
        image = read_image(image_path, ROI)
        prediction =  get_input_vector(image)[0]

        if prediction >= 0.002:
            return 1.0
        else:
            return 0.0

        if return_boolean:
            return self.boolean_result(prediction)

        return prediction

import cv2
import numpy

from ..utils.image_utils import read_image
from ..utils.histogram_analyzation import largest, normalize, \
                                          calculate_peak_value

from filter import Filter


def get_input_vector(img):
    hist = cv2.calcHist([img], [0], None, [256], [0, 255])
    hist = normalize(hist)

    return numpy.array([numpy.average(largest(calculate_peak_value(hist), 0.2))],
                       dtype=numpy.float32)


class Posterized(Filter):
    """Filter for detecting posterized images"""

    name = 'posterized'
    speed = 1

    def __init__(self, threshold=0.5, invert_threshold=False):
        """Initializes a posterized image filter

        :param threshold: threshold at which the given prediction is changed
                          from negative to positive
        :type threshold: float
        :param invert_threshold: whether the result should be greater than
                                 the given threshold (default) or lower
                                 for an image to be considered positive
        :type invert_threshold: bool
        """
        super(Posterized, self).__init__(threshold, invert_threshold)

    def predict(self, image_path, return_boolean=True, ROI=None):
        """Predict if a given image is posterized

        :param image_path: path to the image
        :type image_path: str
        :param return_boolean: whether to return the result as a
                               float between 0 and 1 or as a boolean
                               (threshold is given to the class)
        :type return_boolean: bool
        :param ROI: possible region of interest as a 4-tuple
                    (x0, y0, width, height), None if not needed
        :returns: the prediction as a bool or float depending on the
                  return_boolean parameter
        """
        image = read_image(image_path, ROI)
        prediction = get_input_vector(image)[0]

        if prediction >= 0.002:
            return 1.0
        else:
            return 0.0

        if return_boolean:
            return self.boolean_result(prediction)
        return prediction

Fix posterized filter