-
Notifications
You must be signed in to change notification settings - Fork 0
/
facedetect.py
59 lines (56 loc) · 2.15 KB
/
facedetect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
"""
Python implementation by: Roman Stanchak, James Bowman
"""
import os, sys, time
import cv2.cv as cv
minSize = (20, 20)
imageScale = 1
haarScale = 2
minNeighbors = 3
haarFlags = cv.CV_HAAR_DO_CANNY_PRUNING
def detectFace(img, cascade):
# allocate temporary images
gray = cv.CreateImage((img.width,img.height), 8, 1)
small_img = cv.CreateImage((cv.Round(img.width / imageScale),cv.Round (img.height / imageScale)), 8, 1)
# convert color input image to grayscale
cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
# scale input image for faster processing
cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
cv.EqualizeHist(small_img, small_img)
faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),haarScale, minNeighbors, haarFlags, minSize)
if faces:
print "\tDetected ", len(faces), " object(s)"
for ((x, y, w, h), n) in faces:
#the input to cv.HaarDetectObjects was resized, scale the
#bounding box of each face and convert it to two CvPoints
pt1 = (int(x * imageScale), int(y * imageScale))
pt2 = (int((x + w) * imageScale), int((y + h) * imageScale))
cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
return img
else:
return False
#go through all directories and sub directories and sniff for
def readDirectory(fileLocation, cascade):
for root, dirs, files in os.walk(fileLocation):
print root, "has:"
for name in files:
if name.find(".jpg") >=1 :
#sequentially loop, load and detect.
print "Analysing " + name +":"
#measure how long it takes
t = cv.GetTickCount()
#load in the image
image = cv.LoadImage(os.path.join(root,name), 1)
match = detectFace(image, cascade)
if match:
#save a new image with a box round each face
cv.SaveImage( fileLocation + "/face_" + name, match)
t = cv.GetTickCount() -t
print "\tTime = %gms" %(t/(cv.GetTickFrequency()*1000.))
if __name__ == '__main__':
cascade = cv.Load('haar/haarcascade_frontalface_default.xml')
if len(sys.argv) != 2:
print 'please provide a directory to read'
sys.exit(1)
readDirectory(sys.argv[1], cascade)