Search code examples
swift4ios11face-detectionapple-vision

Face detection swift vision kit


I am trying Vision kit for iOS 11. I can use Vision and I can find boundbox values face. But I don't know how can I draw a rectangle using this points. I hope so my question is clear.


Solution

  • Hope you were able to use VNDetectFaceRectanglesRequest and able to detect faces. To show rectangle boxes there are lots of ways to achieve it. But simplest one would be using CAShapeLayer to draw layer on top your image for each face you detected.

    Consider you have VNDetectFaceRectanglesRequest like below

    let request = VNDetectFaceRectanglesRequest { [unowned self] request, error in
                if let error = error {
                    // somthing is not working as expected
                }
                else {
                    //  we got some face detected
                    self.handleFaces(with: request)
                }
            }
            let handler = VNImageRequestHandler(ciImage: ciImage, options: [:])
            do {
                try handler.perform([request])
            }
            catch {
               // catch exception if any
            }
    

    You can implement a simple method called handleFace for each face detected and use VNFaceObservation property to draw a CAShapeLayer.

    func handleFaces(with request: VNRequest) {
            imageView.layer.sublayers?.forEach { layer in
                layer.removeFromSuperlayer()
            }
            guard let observations = request.results as? [VNFaceObservation] else {
                return
            }
            observations.forEach { observation in
                let boundingBox = observation.boundingBox
                let size = CGSize(width: boundingBox.width * imageView.bounds.width,
                                  height: boundingBox.height * imageView.bounds.height)
                let origin = CGPoint(x: boundingBox.minX * imageView.bounds.width,
                                     y: (1 - observation.boundingBox.minY) * imageView.bounds.height - size.height)
    
                let layer = CAShapeLayer()
                layer.frame = CGRect(origin: origin, size: size)
                layer.borderColor = UIColor.red.cgColor
                layer.borderWidth = 2
    
                imageView.layer.addSublayer(layer)
            }
        }
    

    More info can be found here in Github repo iOS-11-by-Examples