Face Detection in iOS

To play, in face with vision

Tony Wilson jesuraj
1 min readOct 25, 2021

In iOS detecting a face from image is very easy with VISION

Yes vision giving some super power for iOS developers (Like infinity stone).

my other blog detecting text from image using vision

Step -1

import Vision

step -2

vision need image as cgimage so image into cgimage

guard let cgImage = yourImage.image?.cgImage else {return }

step -3

request handling with VNImageRequestHandler .

let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])do {try handler.perform([request])} catch let reqErr {print("Error", reqErr)}

VNDetectFaceRectanglesRequest will do the rest works

let request = VNDetectFaceRectanglesRequest { (req, err) inif let err = err {print("Error in Pic", err)return}req.results?.forEach({ (res) inguard let faceObservation = res as? VNFaceObservation else {return}print(faceObservation.boundingBox)})}

thats it. Now face VNFaceObservation will give the frame of face in the image like (0.4031423330307007, 0.6201969385147095, 0.1809350848197937, 0.15218837559223175)

with these axis we get where the face take places.

It will support for multi faces in image too, Thats why we use foreach

If any mistake or you need to shout me, comments session in always opened

நன்றி வணக்கம் || Thank you

--

--