3-Minute Face Detection Cooking on macOS X
Overview
I’m planning to build a toy that uses a Raspberry Pi so that when it decides you’re a family member, it says “Welcome home, master,” and when you’re not family, it says “I’m calling the cops.”
As a preliminary step to a preliminary step, let’s try detecting a face in a still image.
By the way, face detection and face recognition mean completely different things.
- Face detection … detecting the part of an image that is a face.
- Face recognition … determining that a face belongs to a specific person.
For setting up the environment, please refer to my previous article.

MacOSX に Python2, Python3 仮想環境構築 - 長生村本郷Engineers'Blog
以下に移行しました。 kenzo0107.github.io
Environment
- MacOSX 10.11.5
- Python 3
Installing CV2
- Install cv2 with homebrew
1 | brew tap homebrew/science |
- Use Python 3
1 | $ source ~/py3env/bin/active |
- Copy cv2.so into pip’s site-packages
1 | (py3env)$ cd ~/py3env/lib/python3.4/site-packages |
- Verify the cv2 import
If the version is displayed, it worked.
1 | (py3env)$ python -c 'import cv2; print(cv2.__version__)' |
Installing the Script
1 | (py3env)$ cd ~/py3env |
Running the Script
1 | (py3env)$ python trimming.py <img_path> |
- Before

- After

The leather pants got recognized as a face too…
but for now, it basically works.
Key Points of This Script
- To improve the accuracy of the detection count, we tune the parameters of the following detectMultiScale method in the script.
1 | facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.02, minNeighbors=3, minSize=(7,7)) |
| Item | Value |
|---|---|
| scaleFactor | The scale by which the image is progressively shrunk during analysis |
| minNeighbors | The minimum number of neighbor rectangles |
| minSize | The size for recognizing the face part (height, width) |
And there we have it: the very first step is done ♪
