Face Detection in IOS

Soubhi Hadri
4 min readMar 5, 2018

Being sick over the past two days prevented me from going to work. Therefore, I decided to play with OpenCV on iOS and I ended up with this project, which I want to share with you guys :)

The idea is to use Haar feature-based cascade classifiers, which is implemented in OpenCV and used for face detection. Nothing new, I have just put the pieces together and reached the results below.

In this article, you can find all steps for this to work. I also provided a link to the source I used with each step.

The main steps for this project:

  1. Import and configure OpenCV library to xcode project.
  2. Build a camera frame extractor to get a frame every time is available.
  3. Apply detectMultiScale to detect faces and show the results.

OpenCV with IOS project:

  1. Download the last version of OpenCV. You can find it here, and then decompress it.
  2. Create a new xcode project.
  3. Drag & drop the opencv2.framework file inside your project (You can create libs folder and drop the file inside).

4. Go to Linked Frameworks and Libraries and make sure that OpenCV is linked. If not, press (+) -> add other… -> add it. You need also to add the libraries shown below.

5. Go to Build Settings ->Framework Search Paths and make sure that path of opencv2.framework is correct. e.g. $(PROJECT_DIR)/face_detection/libs.

6. Create new group (folder) and name it classes.

7. Inside classes folder do: File -> New -> File… -> Coca Touch Class
-> name: OpencvWrapper , Subclass of : NSObject, Language : Objective-C
-> Next.

8. Rename OpencvWrapper.m to OpencvWrapper.mm

9. Press “Create Bridging Header” and Opencvproject-Bridging-Header.h will be created automatically.

10. Add to Opencvproject-Bridging-Header.h.

--

--

Soubhi Hadri

Software Engineer at Microsoft. Former Computer Vision Engineer at Shiseido Group 資生堂. Interested in drones, CV, ML and DL. www.soubhihadri.com