Skip to content

Instantly share code, notes, and snippets.

@syshen
Created November 1, 2011 06:56
Show Gist options
  • Save syshen/1330071 to your computer and use it in GitHub Desktop.
Save syshen/1330071 to your computer and use it in GitHub Desktop.
import sys, os
import cv
def detectObject(image):
size = cv.GetSize(image)
grayscale = cv.CreateImage(size, 8, 1)
cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
storage = cv.CreateMemStorage(0)
cv.EqualizeHist(grayscale, grayscale)
cascade = cv.Load("haarcascade_frontalface_alt.xml")
faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
print faces
if faces:
for i in faces:
((x, y, w, h), d) = i
cv.Rectangle(image, (int(x), int(y)),
(int(x+w), int(y+h)),
cv.CV_RGB(0,255,0), 3, 8, 0)
def displayObject(image):
cv.NamedWindow("face", 1)
cv.ShowImage("face", image)
cv.WaitKey(0)
cv.DestroyWindow("face")
def main():
image = cv.LoadImage("./faces.png")
detectObject(image)
displayObject(image)
cv.SaveImage("face2.png", image)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment