Search code examples
swiftdetectionface-detectionapple-vision

Detect if face is within a circle


Currently I am working on face detection app. I have implemented face detection part using Apple's Vision.

And app has custom white circle drawn over the screen (you can see in below image). enter image description here

Now, how can I detect if face is within that custom white circle or not?


Solution

  • I have also done similar project for fun. Link here: https://github.com/sawin0/FaceDetection

    For those who don't want to dive into repo.

    I have quick suggestion for you, if you have path of circle and face as CGPath then you can compare circle's and face's bounding box using contains(_:using:transform:) .

    Here is a code snippet

        let circleBox = circleCGPath.boundingBox
        let faceBox = faceRectanglePath.boundingBox
        
        if(circleBox.contains(faceBox)){
            print("face is inside the circle")
        }else{
            print("face is outside the circle")
        }
    

    I hope this helps you and others too.

    P.S. If there is any better way to do this then please feel free to share.