Swift – face detection & problems

Hey, I’m Adrian, an Android developer and today, I did some swift.

Hypothesis

Have an already build iOS app written in Swift 3 that works amazing. The developer is on a short vacation and I wanted to not keep the client waiting. So I decided to implement a simple feature: face detection!

I already knew there was something already provided by Apple, but didn’t know how to use it and the provided samples weren’t that good.

Conclusion

After taking a picture using the front facing camera, have to check if there’s a face there or not.

Demonstration

I read in the original documentation and I tried to comply.
First and foremost, when taking a picture using the front camera, you have to turn it in the mirror.

Now, for face detection, I used the next function:

I get the image from the UIImageView (:3) and the ciimage (:4). I set the camera to be on high accuracy mode(:5) because it’s an important feature and not interested in “I think it’s a face”. I Create the face detector (:6) and start looking for faces (:7).

Pretty damn simple, only that it doesn’t work! I tried it on an asset picture and it worked very good, but on my face from a front facing camera, it doesn’t.

So I threw the phone on the table, and started to look for an explanation. By mistake I took a picture of me while the phone was horizontal and I saw that the face detector, detected a face. Eurika! So the face detection works when taking a picture in landscape mode, but not in portrait mode. Easy peasy, I add the next line after line 3:
image = UIImage(cgImage: (image.cgImage!), scale: (image.scale),orientation:.right)

Only that the fucker still didn’t work. So I tried again and again, tried to switch the orientation, tried to rotate 3 times to the right and and get a positive in any position. The darn thing would only work if it was rotated landscape. So I gave up…

Then, Oana thought why do I turn the image and not try to turn the facedetector… so we got to the following code:

The major difference is ‘imageOptions’, where I store the orientations that I want to check for the picture: up, down, left, right, upMirrored, downMirrored, leftMirrored and rightMirrored.




Leave a Comment

Blog

Recent posts