title: Real time face detection with Camera on swift 3
body: <p>How can I do face detection in realtime just as "Camera" does? like white round shape around and over the face. I use <code>AVCapturSession</code> . I found that the image I saved for facial detection. Below I have attached my current code. it only captures image when I press the button and save it into the photo gallery. some please help me to create real-time round shape over according to the person's face!</p>
  
  <p>code</p>
  
  <pre><code>class CameraFaceRecongnitionVC: UIViewController {
  
      @IBOutlet weak var imgOverlay: UIImageView!
      @IBOutlet weak var btnCapture: UIButton!
  
      let captureSession = AVCaptureSession()
      let stillImageOutput = AVCaptureStillImageOutput()
      var previewLayer : AVCaptureVideoPreviewLayer?
  
      // If we find a device we'll store it here for later use
      var captureDevice : AVCaptureDevice?
  
      override func viewDidLoad() {
          super.viewDidLoad()
          btnCapture.CameraButton()
          roundButton.RoundButtonForFaceRecong()
  
          // Do any additional setup after loading the view, typically from a nib.
          captureSession.sessionPreset = AVCaptureSessionPresetHigh
  
          if let devices = AVCaptureDevice.devices() as? [AVCaptureDevice] {
              // Loop through all the capture devices on this phone
              for device in devices {
                  // Make sure this particular device supports video
                  if (device.hasMediaType(AVMediaTypeVideo)) {
              // Finally check the position and confirm we've got the front camera
                      if(device.position == AVCaptureDevicePosition.front) {
                          captureDevice = device
                          if captureDevice != nil {
                              print("Capture device found")
                              beginSession()
                          }
                      }
                  }
              }
          }
      }
  
      @IBAction func actionCameraCapture(_ sender: AnyObject) {
  
          print("Camera button pressed")
          saveToCamera()
      }
  
      func beginSession() {
  
          do {
              try captureSession.addInput(AVCaptureDeviceInput(device: captureDevice))
              stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
  
              if captureSession.canAddOutput(stillImageOutput) {
                  captureSession.addOutput(stillImageOutput)
              }
  
          }
          catch {
              print("error: \(error.localizedDescription)")
          }
  
          guard let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) else {
              print("no preview layer")
              return
          }
  
          self.view.layer.addSublayer(previewLayer)
          previewLayer.frame = self.view.layer.frame
          captureSession.startRunning()
  
         // self.view.addSubview(navigationBar)
          self.view.addSubview(imgOverlay)
          self.view.addSubview(btnCapture)
      }
  
      func saveToCamera() {
  
          if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {
  
              stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (CMSampleBuffer, Error) in
                  if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(CMSampleBuffer) {
  
                      if let cameraImage = UIImage(data: imageData) {
  
                          UIImageWriteToSavedPhotosAlbum(cameraImage, nil, nil, nil)
                      }
                  }
              })
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  }
  </code></pre>