-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptExtractingFaces.py
47 lines (44 loc) · 2.28 KB
/
ScriptExtractingFaces.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
import cv2
import glob
import sys
faceDet = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faceDet_two = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
faceDet_three = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
faceDet_four = cv2.CascadeClassifier("haarcascade_frontalface_alt_tree.xml")
def detect_faces():
files = glob.glob("%s/*" %str(sys.argv[1])) #Get list of all images
print(files)
filenumber = 0
for f in files:
frame = cv2.imread(f) #Open image
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #Convert image to grayscale
#Detect face using 4 different classifiers
face = faceDet.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
face_two = faceDet_two.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
face_three = faceDet_three.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
face_four = faceDet_four.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10, minSize=(5, 5), flags=cv2.CASCADE_SCALE_IMAGE)
#Go over detected faces, stop at first detected face, return empty if no face.
if len(face) == 1:
facefeatures = face
elif len(face_two) == 1:
facefeatures = face_two
elif len(face_three) == 1:
facefeatures = face_three
elif len(face_four) == 1:
facefeatures = face_four
else:
facefeatures = ""
#Cut and save face
for (x, y, w, h) in facefeatures: #get coordinates and size of rectangle containing face
print("face found in file: %s" %f)
filename = f.replace("%s\\" %str(sys.argv[1]), "")
filename = filename.replace(".jpg", ".png")
print("filename %s" %filename)
gray = gray[y-30:y+h+50, x-30:x+w+50] #Cut the frame to size
try:
out = cv2.resize(gray, (350, 350)) #Resize face so all images have same size
cv2.imwrite("%s/%s" %(str(sys.argv[2]),filename), out) #Write image
except:
pass #If error, pass file
filenumber += 1 #Increment image number
detect_faces() #Call function