             layer.frame = CGRect(x: offset.x, y: offset.y, width: finalSize.width, height: finalSize.height)
             self.didFocusOnFaces?()
         }
+    }
+
+    private func drawDebugRectangles(from image: UIImage, size: CGSize, features: [CIFeature]) -> UIImage {
+        // Draw rectangles around detected faces
+        let rawImage = UIImage(cgImage: image.cgImage!)
+        UIGraphicsBeginImageContext(size)
+        rawImage.draw(at: CGPoint.zero)
+
+        let context = UIGraphicsGetCurrentContext()
+        context?.setStrokeColor(UIColor.red.cgColor)
+        context?.setLineWidth(3)
+
+        features.forEach({
+            var faceViewBounds = $0.bounds
+            faceViewBounds.origin.y = size.height - faceViewBounds.origin.y - faceViewBounds.size.height
+
+            context?.addRect(faceViewBounds)
+            context?.drawPath(using: .stroke)
+        })
+
+        let newImage = UIGraphicsGetImageFromCurrentImageContext()!
+        UIGraphicsEndImageContext()
+        return newImage
     }
 
     private func imageLayer() -> CALayer {

        rect.size.width = CGFloat(rightBorder) - rect.origin.x
        rect.size.height = CGFloat(bottomBorder) - rect.origin.y

        var center = CGPoint(x: rect.origin.x + rect.size.width / 2.0, y: rect.origin.y + rect.size.height / 2.0)
        var offset = CGPoint.zero
        var finalSize = size

        DispatchQueue.main.async {
            if size.width / size.height > self.bounds.size.width / self.bounds.size.height {
                finalSize.height = self.bounds.size.height
                finalSize.width = size.width/size.height * finalSize.height
                center.x = finalSize.width / size.width * center.x
                center.y = finalSize.width / size.width * center.y

                offset.x = center.x - self.bounds.size.width * 0.5
                if offset.x < 0 {
                    offset.x = 0
                } else if offset.x + self.bounds.size.width > finalSize.width {

                }
                offset.x = -offset.x
            } else {
                var centerY = rect.origin.y + rect.size.height / 2.0

                finalSize.width = self.bounds.size.width
                finalSize.height = size.height / size.width * finalSize.width
                centerY = finalSize.width / size.width * centerY

                offset.y = centerY - self.bounds.size.height * CGFloat(1-0.618)
                if offset.y < 0 {
                    offset.y = 0
                } else if offset.y + self.bounds.size.height > finalSize.height {
