-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceZoom.py
43 lines (35 loc) · 1.08 KB
/
faceZoom.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
import cv2
#zoom in on the first face found and return True.
#If no faces are found return False.
def doFaceZoom(imagePath):
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
anImage = cv2.imread(imagePath)
grayImage = cv2.cvtColor(anImage, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = faceCascade.detectMultiScale(
grayImage,
scaleFactor=1.2,
minNeighbors=5,
# minSize=(30, 30)
)
for (x, y, w, h) in faces:
zoomed_gray = grayImage[y:y+h, x:x+w]
#detect eyes
eyes = eyeCascade.detectMultiScale(
zoomed_gray,
scaleFactor=1.2,
minNeighbors=5,
# minSize=(30, 30)
)
if len(eyes) > 0:
extraCrop = float(15) #procent extra crop on each side
horOffset = int(round(h*(extraCrop/100.0)))
verOffset = int(round(w*(extraCrop/100.0)))
#crop & save image
cropped_color = anImage[y+horOffset:y+h-horOffset, x+verOffset:x+w-verOffset]
cv2.imwrite(imagePath, cropped_color)
print('face found and saved')
return True
print('no face found!')
return False