Last active
June 14, 2017 18:17
-
-
Save AdibContractorCC/ec518b204a3f7b64f2f61d0a043cb844 to your computer and use it in GitHub Desktop.
Detect and highlight faces in live camera feed
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 UIKit | |
import CoreMedia | |
import Vision | |
class LiveCaptureTaggingViewController: LiveCaptureViewController { | |
private var faceLayers = [CAShapeLayer]() | |
override func processBufferCaptured(buffer: CMSampleBuffer!, faceDetectionRequest: VNDetectFaceRectanglesRequest) { | |
DispatchQueue.main.async { [unowned self] in | |
self.faceLayers.forEach{ $0.removeFromSuperlayer() } | |
self.faceLayers.removeAll() | |
guard let results = faceDetectionRequest.results, results.count > 0 else { | |
return | |
} | |
for observation in faceDetectionRequest.results as! [VNFaceObservation] { | |
let layer = CAShapeLayer() | |
let rect = observation.boundingBox.denormalized(newRect: self.view.frame) | |
let path = UIBezierPath(rect: rect) | |
layer.path = path.cgPath | |
layer.fillColor = UIColor.clear.cgColor | |
layer.strokeColor = UIColor.yellow.cgColor | |
layer.lineWidth = 4 | |
self.view.layer.addSublayer(layer) | |
self.faceLayers.append(layer) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment