Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var faceDetector: VisionFaceDetector?
- func detectFaces(in image: UIImage) {
- // 1
- let vision = Vision.vision()
- let options = VisionFaceDetectorOptions()
- options.modeType = .accurate
- options.landmarkType = .all
- options.classificationType = .all
- options.minFaceSize = CGFloat(0.1)
- options.isTrackingEnabled = true
- // 2
- faceDetector = vision.faceDetector(options: options)
- // 3
- let visionImage = VisionImage(image: image)
- // 4
- faceDetector?.detect(in: visionImage) { [weak self] (faces, error) in
- guard error == nil else {
- self?.presentAlert(title: "Failed to detect faces", message: error?.localizedDescription)
- return
- }
- guard let faces = faces, let imageView = self?.imageView else { return }
- let widthRatio = imageView.frame.size.width / image.size.width
- let heightRatio = imageView.frame.size.height / image.size.height
- faces.forEach {
- // 5
- if $0.hasSmilingProbability {
- let faceLabel = FaceReplaceViewController.newEmojiLabel(frame: CGRect(x: $0.frame.minX * widthRatio, y: $0.frame.minY * heightRatio, width: $0.frame.width * widthRatio, height: $0.frame.height * heightRatio), smilingProbability: $0.smilingProbability)
- // 6
- if $0.hasHeadEulerAngleY {
- faceLabel.transform = CGAffineTransform(rotationAngle: ($0.headEulerAngleY / 180) * CGFloat.pi)
- }
- self?.imageView.addSubview(faceLabel)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment