-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detect.py
68 lines (56 loc) · 1.7 KB
/
face_detect.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
59
60
61
62
63
64
65
66
67
import cv2
import sys
import os
import shutil
import numpy as np
# Get user supplied values
#imagePath = sys.argv[1]
cascPath = "haarcascade_frontalface_default.xml"
resultPath = "faces_result"
maxFaceHeightPx = 0.7 * 480
minFaceHeightPx = 0.3 * 480
##############
# face detect function
##############
def getFaces(faceCascade, grayScale):
faces = faceCascade.detectMultiScale(
grayScale,
scaleFactor=1.3,
minNeighbors=5,
minSize=(50, 50),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
return faces
def isGoodFaceMatch(faces):
goodMatches = list()
for face in enumerate(faces):
if(face[3] < maxFaceHeightPx && face[3] > minFaceHeightPx):
goodMatches.append(face)
return faces
#prepare result dir
if not os.path.exists(resultPath):
os.makedirs(resultPath)
for the_file in os.listdir(resultPath):
file_path = os.path.join(resultPath, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
#loop through input files
#for imagePath, dirs, filenames in os.walk(imagePath):
# print(imagePath)
# for f in filenames:
# filePath = imagePath + "/" + f
# image = cv2.imread(filePath)
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#
# # Detect faces in the image
# faceCascade = cv2.CascadeClassifier(cascPath)
# faces = getFaces(faceCascade, gray)
#
# if len(faces) > 0:
# print "Found {0} faces in {1}".format(len(faces), filePath)
# for (x, y, w, h) in faces:
# cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.imwrite("faces_result/f_" + f, image)