<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '<NAME>'


# Built-in Modules
import time
import argparse
# Standard Modules
import cv2
import numpy
import cPickle
# Custom Modules
import extended


class SkinDetector(object):
    def __init__(self, args):
        assert isinstance(args, argparse.Namespace), 'args must be of type argparse.Namespace'
        self.args = args
        self.mask = None
        logger.debug('SkinDetector initialised')

    @staticmethod
    def assert_image(img, grey=False):
        logger.info('Applying assertions...')
        depth = 3
        if grey:
            depth = 2
        assert isinstance(img, numpy.ndarray), 'image must be a numpy array'
        assert len(img.shape) == depth, 'skin detection can only work on color images'
        assert img.size > 100, 'seriously... you thought this would work?'

    def get_mask_hsv(self, img):
        logger.info('Applying hsv threshold')
        self.assert_image(image)
        lower_thresh = numpy.array([100, 50, 0], dtype=numpy.uint8)
        upper_thresh = numpy.array([120, 150, 255], dtype=numpy.uint8)
        img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
        msk_hsv = cv2.inRange(img_hsv, lower_thresh, upper_thresh)
        if self.args.debug:
            cv2.destroyAllWindows()
            cv2.imshow('input', img)
            cv2.imshow('mask', msk_hsv)
            cv2.waitKey(0)
        self.add_mask(msk_hsv)

    def get_mask_rgb(self, img):
        logger.info('Applying rgb thresholds')
        lower_thresh = numpy.array([45, 52, 108], dtype=numpy.uint8)
        upper_thresh = numpy.array([255, 255, 255], dtype=numpy.uint8)
        mask_a = cv2.inRange(img, lower_thresh, upper_thresh)
        mask_b = 255*((img[:, :, 2]-img[:, :, 1])/20)
        logger.debug('mask_b unique: {0}'.format(numpy.unique(mask_b)))
        mask_c = 255*((numpy.max(img, axis=2)-numpy.min(img, axis=2))/15)
        logger.debug('mask_d unique: {0}'.format(numpy.unique(mask_c)))
        msk_rgb = cv2.bitwise_and(mask_a, mask_b)
        msk_rgb = cv2.bitwise_and(mask_c, msk_rgb)
        if self.args.debug:
            cv2.destroyAllWindows()
            cv2.imshow('input', img)
            cv2.imshow('mask', msk_rgb)
            cv2.waitKey(0)
        self.add_mask(msk_rgb)

    def process(self, img):
        logger.debug('Initialising process')
        dt = time.time()
        self.assert_image(img)
        self.n_mask = 0
        #image = self.normalise(image)
        self.mask = numpy.zeros(img.shape[:2], dtype=numpy.uint8)
        self.get_mask_hsv(img)
        self.get_mask_rgb(img)
        logger.info('Thresholding sum of masks')
        self.threshold(self.args.thresh)
        if self.args.debug:
            cv2.destroyAllWindows()
            cv2.imshow('skin_mask', self.mask)
            cv2.imshow('input_img', img)
            cv2.waitKey(0)<commit_msg>Added skin detector<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = '<NAME>'


# Built-in Modules
import time
import argparse
# Standard Modules
import cv2
import numpy
# Custom Modules
import extended


class SkinDetector(object):
    def __init__(self, args):
        assert isinstance(args, argparse.Namespace), 'args must be of type argparse.Namespace'
        self.args = args
        self.mask = None
        logger.debug('SkinDetector initialised')

    @staticmethod
    def assert_image(img, grey=False):
        logger.info('Applying assertions...')
        depth = 3
        if grey:
            depth = 2
        assert isinstance(img, numpy.ndarray), 'image must be a numpy array'
        assert len(img.shape) == depth, 'skin detection can only work on color images'
        assert img.size > 100, 'seriously... you thought this would work?'

    def get_mask_hsv(self, img):
        logger.info('Applying hsv threshold')
        self.assert_image(image)
        lower_thresh = numpy.array([100, 50, 0], dtype=numpy.uint8)
        upper_thresh = numpy.array([120, 150, 255], dtype=numpy.uint8)
        img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
        msk_hsv = cv2.inRange(img_hsv, lower_thresh, upper_thresh)
        if self.args.debug:
            cv2.destroyAllWindows()
            cv2.imshow('input', img)
            cv2.imshow('mask', msk_hsv)
            cv2.waitKey(0)
        self.add_mask(msk_hsv)

    def get_mask_rgb(self, img):
        logger.info('Applying rgb thresholds')
        lower_thresh = numpy.array([45, 52, 108], dtype=numpy.uint8)
        upper_thresh = numpy.array([255, 255, 255], dtype=numpy.uint8)
        mask_a = cv2.inRange(img, lower_thresh, upper_thresh)
        mask_b = 255*((img[:, :, 2]-img[:, :, 1])/20)
        logger.debug('mask_b unique: {0}'.format(numpy.unique(mask_b)))
        mask_c = 255*((numpy.max(img, axis=2)-numpy.min(img, axis=2))/20)
        logger.debug('mask_d unique: {0}'.format(numpy.unique(mask_c)))
        msk_rgb = cv2.bitwise_and(mask_a, mask_b)
        msk_rgb = cv2.bitwise_and(mask_c, msk_rgb)
        if self.args.debug:
            cv2.destroyAllWindows()
            cv2.imshow('input', img)
            cv2.imshow('mask', msk_rgb)
            cv2.waitKey(0)
        self.add_mask(msk_rgb)

    def process(self, img):
        logger.debug('Initialising process')
        dt = time.time()
        self.assert_image(img)
        self.n_mask = 0
        #image = self.normalise(image)
        self.mask = numpy.zeros(img.shape[:2], dtype=numpy.uint8)
        self.get_mask_hsv(img)
        self.get_mask_rgb(img)
        logger.info('Thresholding sum of masks')
        self.threshold(self.args.thresh)
        if self.args.debug:
            cv2.destroyAllWindows()
            cv2.imshow('skin_mask', self.mask)
            cv2.imshow('input_img', img)
            cv2.waitKey(0)
{'fraction_non_alphanumeric': 0.1278648974668275, 'fraction_numerical': 0.02860589350336033, 'mean_word_length': 2.4900781719783525, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1594596', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1938, 'n_words': 393}