Created
May 11, 2011 03:28
-
-
Save jabbalaci/965867 to your computer and use it in GitHub Desktop.
Face Detection in Static Images with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Origin: http://creatingwithcode.com/howto/face-detection-in-static-images-with-python/ | |
Patched for Ubuntu 11.04 (the location of the XML file is different). | |
Installation: | |
sudo apt-get install python-opencv libcv-dev opencv-doc | |
""" | |
# face_detect.py | |
# Face Detection using OpenCV. Based on sample code from: | |
# http://python.pastebin.com/m76db1d6b | |
# Usage: python face_detect.py <image_file> | |
import sys, os | |
from opencv.cv import * | |
from opencv.highgui import * | |
def detectObjects(image): | |
"""Converts an image to grayscale and prints the locations of any | |
faces found""" | |
grayscale = cvCreateImage(cvSize(image.width, image.height), 8, 1) | |
cvCvtColor(image, grayscale, CV_BGR2GRAY) | |
storage = cvCreateMemStorage(0) | |
cvClearMemStorage(storage) | |
cvEqualizeHist(grayscale, grayscale) | |
cascade = cvLoadHaarClassifierCascade( | |
'/usr/share/doc/opencv-doc/examples/haarcascades/haarcascades/haarcascade_frontalface_default.xml.gz', | |
cvSize(1,1)) | |
faces = cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, | |
CV_HAAR_DO_CANNY_PRUNING, cvSize(50,50)) | |
if faces: | |
for f in faces: | |
print("[(%d,%d) -> (%d,%d)]" % (f.x, f.y, f.x+f.width, f.y+f.height)) | |
def main(): | |
image = cvLoadImage(sys.argv[1]); | |
detectObjects(image) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment