Last active
June 14, 2017 18:33
-
-
Save AdibContractorCC/b4887591b9a9d3e8aa84015780cbb5f0 to your computer and use it in GitHub Desktop.
Detect faces from live camera capture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import AVFoundation | |
import Vision | |
class LiveCaptureViewController: UIViewController { | |
public let cameraCapture = CameraCapture() | |
private var videoPreviewLayer : AVCaptureVideoPreviewLayer? | |
override func viewDidLoad() | |
{ | |
super.viewDidLoad() | |
cameraCapture.delegate = self | |
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: cameraCapture.captureSession) | |
videoPreviewLayer!.videoGravity = AVLayerVideoGravity.resize | |
videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.landscapeRight | |
view.layer.addSublayer(videoPreviewLayer!) | |
} | |
override func viewDidAppear(_ animated: Bool) | |
{ | |
super.viewDidAppear(animated) | |
videoPreviewLayer!.frame = view.layer.bounds | |
} | |
override func didReceiveMemoryWarning() | |
{ | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
override var shouldAutorotate: Bool { | |
return true | |
} | |
override var supportedInterfaceOrientations : UIInterfaceOrientationMask { | |
return .landscapeRight | |
} | |
func processBufferCaptured(buffer: CMSampleBuffer!, faceDetectionRequest: VNDetectFaceRectanglesRequest) | |
{ | |
// Override this | |
} | |
} | |
extension LiveCaptureViewController : CameraCaptureDelegate { | |
func bufferCaptured(buffer: CMSampleBuffer!) | |
{ | |
guard let pixelBuffer : CVPixelBuffer = CMSampleBufferGetImageBuffer(buffer) else { return } | |
let faceDetectionRequest = VNDetectFaceRectanglesRequest() | |
let myRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]) | |
try! myRequestHandler.perform([faceDetectionRequest]) | |
processBufferCaptured(buffer: buffer, faceDetectionRequest: faceDetectionRequest) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment