The Genesis
I love to draw. Specifically, I love to draw portraits. I find the singularity of our faces fascinating, how having so few parameters (eyes, nose, mouth, ears and a form) each of us has a different aspect.
Once you get into portrait drawing, the theory says that placing the details and finding the right proportions is what matters. Of course, you need to represent the details of each feature, but what gives you an air of similarity with the original are those features. Wouldn’t it be nice if there was an app that did all this for me? I checked in the app store and surprise, there was nothing! As I love to eat my own dog food, it was time to develop an application that, given a portrait, provides the coordinates and proportions of the face, so I can have an idea when projecting that into paper and pencil.
Naturally, the first task was to refresh my rusted knowledge about Face Detection on iOS and to get deep into Vision.
This framework performs face and face landmark detection, text detection, barcode recognition, image registration, and general feature tracking. Vision also allows the use of custom Core ML models for tasks like classification or object detection.
Wow, that’s a big chunk of features! I just need face and face landmark detection. A landmark is a feature in Vision language, like, left eye, right eye … apart from that, if I wanted to retrieve the face image from the device camera, I had to do it using AVFoundation.
After I started getting deep into it, my “potential third-party library help the open-source community alarm” started beeping. I was surprised that, although there were few tutorials about it, there was no library that encapsulated the face detection feature in one step, avoiding dealing with the low-level Apple frameworks and making easy the main purpose of a Face Detection on Mobile apps — to draw silly things on top 😝

Warhol
Warhol takes the name from Andy Warhol, an American artist, film director, and producer who was a leading figure in the visual art movement known as pop art. That is because when I think of portraits, I think of:

Why use it? As mentioned, you can forget about the rather cumbersome Vision and AVFoundation Apple frameworks, and focus on your real creative task. It’s a tool that will help you achieve your distinct feature.
Features:
What are the features of Warhol?
- Face detection from camera
- Face detection from UIImageView
- Face features conversion to the client coordinates
- Draw on top of the face
Usage Example
I won’t go through the process of Face Detection with the iOS SDK, the core of Warhol, as there are already a few good tutorials for that on the internet — for instance, this one. I will, however, detail how can we do the same with the Warhol interface, from the camera, and from an image instance. As you will see, we can replace several hundred lines of code and classes with just a few of them.
With camera
Import Warhol in the file you are going to use. Create an instance of CameraFaceDetectionViewController, and assign the view where you are going to draw to the cameraFrontView property of the former. (We will see how to draw just in a moment). You can then present the view controller:
import Warhol
let cameraViewController = CameraFaceDetectionViewController()
let faceView = FaceView()
faceView.backgroundColor = .clear
cameraViewController.cameraFrontView = faceView
present(cameraViewController, animated: true, completion: nil)
In order to draw, we should create a subclass of UIView that complies with the Warhol protocol FaceView. We can then draw in their func draw(_ rect: CGRect) function, given the updated view model property. Anytime Warhol detects a Face change, it updates the view model and calls setNeedsDisplay() on the view so it can trigger the drawing process:
import Warhol
final class FaceView: UIView, CameraFrontView {
var viewModel: FaceViewModel?
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext(),
let viewModel = viewModel else {
return
}
context.saveGState()
defer {
context.restoreGState()
}
context.addRect(viewModel.boundingBox)
...

You can implement the CameraFaceDetectionDelegate protocol to react to any change in the Face Detection. This can be convenient for the case when you do not want to draw on top, but just get the face feature (landmarks) coordinates. These are encapsulated in the given parameter FaceViewModel.
With image
In order to detect face features and draw on top, we should pass the UIImageView depicting the face to the SDK, and a closure where we draw on top of the image:
import Warhol
imageView.image = UIImage(named: "Face")
Warhol.drawLandmarks(from: imageView,
draw: { (viewModel, context) in
// draw with CGContext
},
error: {_ in })
If instead of modifying the past image, you want to generate a new UIImageView instance and preserve the original, use drawLandmarksInNewImage:
imageView.image = UIImage(named: "Face")
Warhol.drawLandmarksInNewImage(from: imageView,
draw: { (viewModel, context) in
self.draw(viewModel: viewModel, in: context)
},
completion: { newImage in
self.newImageView.image = newImage
},
error: {_ in })
The Source
To know more about Warhol and use it, go to the GitHub Repo Page. You can add the framework to your project via CocoaPods, Carthage, and Swift Package Manager as a subrepo.
The Future
This is just the beginning — there are a lot of cool things to do when detecting a face. Next releases will see more swell features, like:
- More face landmarks converted.
- Provide the recorded video with the drawing on top, as it is done on Instagram or Snapchat Stories.
- Free filters, so the client would just have to choose between some graphics that help to customize their user’s experience.
Of course, I would love you for the contribution to Warhol. PRs are more than welcome with new ideas, improvements, fixes, and suggestions. This project is under MIT license and in case of issues please use the dedicated section in GitHub.
In the second part of this post you can learn how to easily compose an overlay on top of the detected face.
Happy Warholing! 😜


Leave a Reply