Created
February 19, 2009 18:39
-
-
Save martymcguire/67044 to your computer and use it in GitHub Desktop.
This file contains hidden or 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/python | |
| # 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/opencv/haarcascades/haarcascade_frontalface_default.xml', | |
| 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