Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A face-detection script in Python

This 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.


The 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 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:



If you are a Stanford student taking CompCiv, you should be able to copy the script and follow the instructions without a problem on 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

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
curl -so obama-3d.jpg

Call the script

python haarcascade_frontalface_default.xml obama-phone.jpg obama-3d.jpg

Out-of-the-box results

(needs some tweaking obviously)



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 =
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)

This comment has been minimized.

Copy link

@gupta-rajat gupta-rajat commented Jan 17, 2017

This code is not working.


This comment has been minimized.

Copy link

@Madhivarman Madhivarman commented Apr 23, 2017

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


This comment has been minimized.

Copy link

@GeorgeHorsey GeorgeHorsey commented Dec 18, 2017

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


This comment has been minimized.

Copy link

@addi17662 addi17662 commented Apr 2, 2018

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


This comment has been minimized.

Copy link

@deepak728 deepak728 commented Apr 2, 2018

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


This comment has been minimized.

Copy link

@Aaaish Aaaish commented Jul 25, 2018

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


This comment has been minimized.

Copy link

@rishithavarma rishithavarma commented Jan 25, 2020

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
You can’t perform that action at this time.