Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # http://creatingwithcode.com/howto/face-detection-in-static-images-with-python/
- # 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 *
- import Image, ImageDraw
- 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) # 8 is depth and 1 is channels
- 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.total > 0:
- for f in faces:
- print("[(%d,%d) -> (%d,%d)]" % (f.x, f.y, f.x+f.width, f.y+f.height))
- x1,y1,x2,y2=f.x,f.y,f.x+f.width,f.y+f.height
- print_rectangle(x1,y1,x2,y2) #call to a python pil
- def print_rectangle(x1,y1,x2,y2): #function to modify the img
- im = Image.open(sys.argv[1])
- draw = ImageDraw.Draw(im)
- draw.rectangle([x1,y1,x2,y2])
- im.save(sys.argv[1])
- def main():
- image = cvLoadImage(sys.argv[1])
- detectObjects(image)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement