Skip to content

Instantly share code, notes, and snippets.

@ehrenfeu
Forked from dannguyen/face-boxer-usage.md
Created April 25, 2017 12:29
Show Gist options
  • Save ehrenfeu/daebb6714c10710437235e1b36bbf31d to your computer and use it in GitHub Desktop.
Save ehrenfeu/daebb6714c10710437235e1b36bbf31d to your computer and use it in GitHub Desktop.
A face-detection script in Python

This face-boxer.py script is more-or-less the same code that you'll find in the OpenCV tutorial: Face Detection using Haar Cascades. For another variation, with more explanation, check out RealPython's tutorial.

Usage

The face-boxer.py script is designed to be run from the command-line. It has two required arugments:

  1. The path to a XML file containing a Haar-cascade of visual features. In this example, it will be the features that make up a face.
  2. The path to an image file that you want to perform face-detection on. You can pass in more than one image file as space-separated arguments.

The face-boxer.py script will then read each image file and perform this routine: For every detected object in a given image, the object is highlighted in a light-blue box, and this altered image is saved to:

   /path/to/the-original-image--faces.jpg

Installation

If you are a Stanford student taking CompCiv, you should be able to copy the face-boxer.py script and follow the instructions without a problem on corn.stanford.edu. The script has been tested on Python 2.7.8 and OpenCV 2.4x

Download the haar cascade file

You'll need the XML file that contains the data necessary for OpenCV to do its work. The file below contains the pattern data for frontal-aspects of a face:

curl -so haarcascade_frontalface_default.xml http://stash.compciv.org/opencv/haarcascades/haarcascade_frontalface_default.xml

If detecting faces is boring to you, you can download a zipped archive of all the Haar-cascade files from here, which is simply a mirror of what's in the OpenCV repo. Object patterns include: eyes, eyes with glasses, full (human) bodies, lower bodies, license plates, smiles, and cat faces.

Download the photos

# Download some images
curl -so obama-phone.jpg https://farm9.staticflickr.com/8604/15891607122_794b16aff7_z_d.jpg
curl -so obama-3d.jpg https://farm5.staticflickr.com/4054/4291192697_2ba403a502_b_d.jpg

Call the script

python face-boxer.py haarcascade_frontalface_default.xml obama-phone.jpg obama-3d.jpg

Out-of-the-box results

(needs some tweaking obviously)

img

img

With a webcam

If you're interested in seeing the face-detection code work via your own webcam, check out this RealPython tutorial. Using your own webcam means you have to have Python and OpenCV installed on your own computer -- Mac users, check out this tutorial

import cv2
import os
import sys
from string import Template
# first argument is the haarcascades path
face_cascade_path = sys.argv[1]
face_cascade = cv2.CascadeClassifier(os.path.expanduser(face_cascade_path))
scale_factor = 1.1
min_neighbors = 3
min_size = (30, 30)
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
for infname in sys.argv[2:]:
image_path = os.path.expanduser(infname)
image = cv2.imread(image_path)
faces = face_cascade.detectMultiScale(image, scaleFactor = scale_factor, minNeighbors = min_neighbors,
minSize = min_size, flags = flags)
for( x, y, w, h ) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)
outfname = "/tmp/%s.faces.jpg" % os.path.basename(infname)
cv2.imwrite(os.path.expanduser(outfname), image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment