     
     
     func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
-        if let buffer = sampleBuffer {
-            //            let image: UIImage = GMVUtility.sampleBufferTo32RGBA(buffer)
+        if let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
+            let ciimage = CIImage(cvImageBuffer: imageBuffer)
+            let orientation = getCIDetectorImageOrientation(from: self.deviceOrientation, self.devicePosition)
+            let features = detector.features(in: ciimage, options: [CIDetectorEyeBlink: true, CIDetectorSmile: true, CIDetectorImageOrientation: orientation]) as! [CIFaceFeature]
+            for f in features {
+                if(f.hasSmile && !f.leftEyeClosed && !f.rightEyeClosed) {
+                    // Good picture!
+                }
+            }
             
-            let myPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
-            let myCIimage         = CIImage(cvPixelBuffer: myPixelBuffer!)
-            let image        = UIImage(ciImage: myCIimage)
-            
-            let devicePosition: AVCaptureDevicePosition = AVCaptureDevicePosition.front // TODO: Update when switching is added
-            let deviceOrientation: UIDeviceOrientation = UIDeviceOrientation.portrait
-            let orientation: GMVImageOrientation = GMVUtility.imageOrientation(from: deviceOrientation, with: devicePosition, defaultDeviceOrientation: deviceOrientation)
-            let options = [GMVDetectorImageOrientation: orientation]
-            if let faces = self.faceDetector?.features(in: image, options: options) {
-                if(faces.count > 0) {
-                    print("Detected \(faces.count) faces!")
-                }
-            }
         } else {
             print("Error with buffer!")
         }
         
+    }
+    
+    func getCIDetectorImageOrientation(from deviceOrientation: UIDeviceOrientation, _ cameraPos: AVCaptureDevicePosition ) -> Int {
+        
+        var exifOrientation = 0
+        let isUsingFrontFacingCamera: Bool = cameraPos == AVCaptureDevicePosition.front
+        switch (deviceOrientation) {
+        case UIDeviceOrientation.portraitUpsideDown:  // Device oriented vertically, home button on the top
+            exifOrientation = 8;
+            break;
+        case UIDeviceOrientation.landscapeLeft:       // Device oriented horizontally, home button on the right
+            if (isUsingFrontFacingCamera) {
+                exifOrientation = 3;
+            }
+            else {
+                exifOrientation = 1;
+            }
+            break;
+        case UIDeviceOrientation.landscapeRight:      // Device oriented horizontally, home button on the left
+            if (isUsingFrontFacingCamera) {
+                exifOrientation = 1;
+            }
+            else {
+                exifOrientation = 3;
+            }
+            break;
+        default:
+            exifOrientation = 6;
+            break;
+        }
+        return exifOrientation
     }
     
     


class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
    
    var faceDetector: GMVDetector?
    var videoDataOutput = AVCaptureVideoDataOutput()
    var videoDataOutputQueue: DispatchQueue?
    var imageSet: Bool = false
    
    // The captured image
    var selectedImage = UIImage()
    

        updateCameraSelection()
        setupVideoProcessing()
        setupCameraPreview()

        self.imageView.backgroundColor = #colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)
        
//        let ciimage = CIImage(image: #imageLiteral(resourceName: "sample"))!
//        let features = detector.features(in: ciimage, options: [CIDetectorEyeBlink: true, CIDetectorSmile: true]) as! [CIFaceFeature]
//        for f in features {
//            print("smile: \(f.mouthPosition) left eye: \(f.leftEyePosition) right eye \(f.rightEyePosition)")
//            
//        }
//        print("Detected \(features.count) features")
    }
    
    override func viewDidAppear(_ animated: Bool) {
