<commit_before>import logging
from collections import namedtuple

import cv2

import numpy as np
from raspicam.localtypes import Dimension

LOG = logging.getLogger(__name__)
MutatorOutput = namedtuple('MutatorOutput', 'intermediate_frames motion_regions')


def resizer(dimension):
    def fun(frames, motion_regions):
        return MutatorOutput([cv2.resize(frames[-1], dimension)], motion_regions)
    return fun


def togray(frames, motion_regions):
    return MutatorOutput([cv2.cvtColor(frames[-1], cv2.COLOR_BGR2GRAY)], motion_regions)


def blur(pixels):
    def fun(frames, motion_regions):
        return MutatorOutput([cv2.GaussianBlur(frames[-1], (pixels, pixels), 0)], motion_regions)
    return fun


def masker(mask_filename):

    LOG.debug('Setting mask to %s', mask_filename)
    if not mask_filename:
        return lambda frames, motion_regions: MutatorOutput([frames[-1]], motion_regions)

    mask = cv2.imread(mask_filename, 0)

    def fun(frames, motion_regions):
        frame = frames[-1]

        if len(frame.shape) == 3:
            LOG.warning('Unable to apply the mask to a color image. Convert to B/W first!')
            return MutatorOutput([frame], motion_regions)

        if frame.shape != mask.shape:
            LOG.warning('Mask has differend dimensions than the processed image. '
                        'It should be %s but is %s', frame.shape, mask.shape)
            resized_mask = cv2.resize(mask, (frame.shape[1], frame.shape[0]))
        else:
            resized_mask = mask
        bitmask = cv2.inRange(resized_mask, 0, 0) != 0
        output = np.ma.masked_array(frame, mask=bitmask, fill_value=0).filled()
        return MutatorOutput([output], motion_regions)
    return fun


class MotionDetector:

    def __init__(self):
        self.fgbg = cv2.createBackgroundSubtractorMOG2()

    def __call__(self, frames, motion_regions):
        fgmask = self.fgbg.apply(frames[-1])
        shadows = cv2.inRange(fgmask, 127, 127) == 255
        without_shadows = np.ma.masked_array(fgmask, mask=shadows, fill_value=0).filled()
        _, contours, _ = cv2.findContours(
            without_shadows,
            cv2.RETR_EXTERNAL,
            cv2.CHAIN_APPROX_SIMPLE)
        contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 30]<commit_msg>Add mask to intermediate frames<commit_after>import logging
from collections import namedtuple

import cv2

import numpy as np
from raspicam.localtypes import Dimension

LOG = logging.getLogger(__name__)
MutatorOutput = namedtuple('MutatorOutput', 'intermediate_frames motion_regions')


def resizer(dimension):
    def fun(frames, motion_regions):
        return MutatorOutput([cv2.resize(frames[-1], dimension)], motion_regions)
    return fun


def togray(frames, motion_regions):
    return MutatorOutput([cv2.cvtColor(frames[-1], cv2.COLOR_BGR2GRAY)], motion_regions)


def blur(pixels):
    def fun(frames, motion_regions):
        return MutatorOutput([cv2.GaussianBlur(frames[-1], (pixels, pixels), 0)], motion_regions)
    return fun


def masker(mask_filename):

    LOG.debug('Setting mask to %s', mask_filename)
    if not mask_filename:
        return lambda frames, motion_regions: MutatorOutput([frames[-1]], motion_regions)

    mask = cv2.imread(mask_filename, 0)

    def fun(frames, motion_regions):
        frame = frames[-1]

        if len(frame.shape) == 3:
            LOG.warning('Unable to apply the mask to a color image. Convert to B/W first!')
            return MutatorOutput([frame], motion_regions)

        if frame.shape != mask.shape:
            LOG.warning('Mask has differend dimensions than the processed image. '
                        'It should be %s but is %s', frame.shape, mask.shape)
            resized_mask = cv2.resize(mask, (frame.shape[1], frame.shape[0]))
        else:
            resized_mask = mask
        bitmask = cv2.inRange(resized_mask, 0, 0) != 0
        output = np.ma.masked_array(frame, mask=bitmask, fill_value=0).filled()
        return MutatorOutput([resized_mask, output], motion_regions)
    return fun


class MotionDetector:

    def __init__(self):
        self.fgbg = cv2.createBackgroundSubtractorMOG2()

    def __call__(self, frames, motion_regions):
        fgmask = self.fgbg.apply(frames[-1])
        shadows = cv2.inRange(fgmask, 127, 127) == 255
        without_shadows = np.ma.masked_array(fgmask, mask=shadows, fill_value=0).filled()
        _, contours, _ = cv2.findContours(
            without_shadows,
            cv2.RETR_EXTERNAL,
            cv2.CHAIN_APPROX_SIMPLE)
        contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 30]
{'fraction_non_alphanumeric': 0.10686528497409327, 'fraction_numerical': 0.018566493955094993, 'mean_word_length': 3.1738738738738737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3683320', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1404, 'n_words': 348}