Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active July 29, 2023 22:05
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save dannguyen/cfa2fb49b28c82a1068f to your computer and use it in GitHub Desktop.
Save dannguyen/cfa2fb49b28c82a1068f 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)
@gupta-rajat
Copy link

This code is not working.

@Madhivarman
Copy link

do you import cv2 file actually you need to download cv2 file from external then you have to import them
if you use linux then it will be too easy

@GeorgeHorsey
Copy link

How does it detect a face? Do you feed it sample images?

@addi17662
Copy link

... face_cascade_path = sys.argv[1]
Traceback (most recent call last):
File "", line 2, in
IndexError: list index out of range

@deepak728
Copy link

deepak728 commented Apr 2, 2018

In terminal along with (python filename.py) add haarcascade_frontalface_default.xml
it should be like ( python filename.py haarcascade_frontalface_default.xml )

@Aaaish
Copy link

Aaaish commented Jul 25, 2018

this code is not running no output occurs,
im using pycharm python 3.6

@rishithavarma
Copy link

I downloaded opencv and num py and I am using sublime as my editor and anaconda prompt but the code is not running can you help me with that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment