<commit_before>//
//  FaceObscurationFilter.swift
//  FaceDetection
//
//  Created by <NAME> on 07/01/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import CoreImage

class FaceObscurationFilter : CIFilter {
    let inputImage: CIImage
    
    init(inputImage: CIImage) {
        self.inputImage = inputImage
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var outputImage: CIImage? {
        // Detect any faces in the image
        let detector = CIDetector(ofType: CIDetectorTypeFace, context:nil, options:nil)
        let features = detector.featuresInImage(inputImage)
        guard features.count > 0 else {
            // No features found
            // Nothing to pixellate - output image is the same as the input
            return inputImage
        }
        
        print("Features: \(features)")
        
        // Build a pixellated version of the image using the CIPixellate filter
        let imageSize = inputImage.extent.size
        let pixellationOptions = [
            kCIInputScaleKey: max(imageSize.width, imageSize.height) / 10,
            kCIInputCenterKey: CIVector(x: imageSize.width / 2, y: imageSize.height / 2)
        ]
        let pixellation = CIFilter(name: "CIPixellate", withInputParameters: pixellationOptions)
        guard let pixellatedImage = pixellation?.outputImage else {
            // Failed to pixellate
            return nil
        }
        
        // Build a masking image for each of the faces
        var maskImage: CIImage? = nil
        for feature in features {
            // Get feature position and radius for circle
            let xCenter = feature.bounds.origin.x + feature.bounds.size.width / 2.0
            let yCenter = feature.bounds.origin.y + feature.bounds.size.height / 2.0
            let radius = min(feature.bounds.size.width, feature.bounds.size.height) / 1.5
            
            // Input parameters for the circle filter
            var circleOptions: [String: AnyObject] = [:]
            circleOptions["inputRadius0"] = radius
            circleOptions["inputRadius1"] = radius + 1
            circleOptions["inputColor0"] = CIColor(red: 0, green: 1, blue: 0, alpha: 1)
            circleOptions["inputColor1"] = CIColor(red: 0, green: 0, blue: 0, alpha: 1)<commit_msg>Use PixellationFilter as part of FaceObscurationFilter<commit_after>//
//  FaceObscurationFilter.swift
//  FaceDetection
//
//  Created by <NAME> on 07/01/2016.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import CoreImage

class FaceObscurationFilter : CIFilter {
    let inputImage: CIImage
    
    init(inputImage: CIImage) {
        self.inputImage = inputImage
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var outputImage: CIImage? {
        // Detect any faces in the image
        let detector = CIDetector(ofType: CIDetectorTypeFace, context:nil, options:nil)
        let features = detector.featuresInImage(inputImage)
        guard features.count > 0 else {
            // No features found
            // Nothing to pixellate - output image is the same as the input
            return inputImage
        }
        
        print("Features: \(features)")
        
        // Build a pixellated version of the image using the CIPixellate filter
        guard let pixellatedImage = PixellationFilter(inputImage: inputImage).outputImage else {
            // Failed to pixellate
            return nil
        }
        
        // Build a masking image for each of the faces
        var maskImage: CIImage? = nil
        for feature in features {
            // Get feature position and radius for circle
            let xCenter = feature.bounds.origin.x + feature.bounds.size.width / 2.0
            let yCenter = feature.bounds.origin.y + feature.bounds.size.height / 2.0
            let radius = min(feature.bounds.size.width, feature.bounds.size.height) / 1.5
            
            // Input parameters for the circle filter
            var circleOptions: [String: AnyObject] = [:]
            circleOptions["inputRadius0"] = radius
            circleOptions["inputRadius1"] = radius + 1
            circleOptions["inputColor0"] = CIColor(red: 0, green: 1, blue: 0, alpha: 1)
            circleOptions["inputColor1"] = CIColor(red: 0, green: 0, blue: 0, alpha: 1)
{'fraction_non_alphanumeric': 0.08968411751711951, 'fraction_numerical': 0.015020985199911642, 'mean_word_length': 2.131396957123098, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1201639', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1221, 'n_words': 373}