-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_face_and_eyes_image.py
177 lines (134 loc) · 4.94 KB
/
find_face_and_eyes_image.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import time
import cv2
import cv2.cv as cv
import numpy as np
from PIL import Image
"""
Find a face in an image using OpenCV.
Basic workflow:
* Use Haar facial detection algorithm to find faces
* For each face, use Haar eye detection algorithm to find eyes
* Draw the picture and draw bounding boxes around faces and eyes
"""
def main():
face_settings = {
'scaleFactor' : 1.3,
'minNeighbors' : 1,
'minSize' : (5,5),
'flags' : cv2.cv.CV_HAAR_SCALE_IMAGE|cv2.cv.CV_HAAR_DO_ROUGH_SEARCH
}
eye_settings = {
'scaleFactor' : 1.3,
'minNeighbors' : 3,
'minSize' : (5,5),
'flags' : cv2.cv.CV_HAAR_SCALE_IMAGE|cv2.cv.CV_HAAR_DO_ROUGH_SEARCH
}
# Other flags:
# cv2.cv.CV_HAAR_SCALE_IMAGE
# cv2.cv.CV_HAAR_FIND_BIGGEST_OBJECT
# cv2.cv.CV_HAAR_DO_ROUGH_SEARCH
## Obama - works (but detects to many features)
#IMAGE_FILE = "images/obama.jpg"
#face_settings['scaleFactor'] = 1.3
#face_settings['minNeighbors'] = 1
#eye_settings['scaleFactor'] = 1.3
#eye_settings['minNeighbors'] = 3
## GWBush - works
#IMAGE_FILE = "images/bush.jpg"
#face_settings['scaleFactor'] = 1.3
#face_settings['minNeighbors'] = 1
#eye_settings['scaleFactor'] = 1.3
#eye_settings['minNeighbors'] = 3
## Me - works (but four total features, with overlap)
#IMAGE_FILE = "images/me.jpg"
#face_settings['scaleFactor'] = 1.3
#face_settings['minNeighbors'] = 1
#eye_settings['scaleFactor'] = 1.3
#eye_settings['minNeighbors'] = 3
# Photo from webcam
IMAGE_FILE = "images/opencv.png"
face_settings['scaleFactor'] = 1.3
face_settings['minNeighbors'] = 1
eye_settings['scaleFactor'] = 1.3
eye_settings['minNeighbors'] = 3
draw_face_boxes(IMAGE_FILE, face_settings, eye_settings)
def draw_face_boxes(filename, face_settings, eye_settings):
image = img2np_opencv(filename)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
face_classifier0 = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
face_classifier1 = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
face_classifier2 = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
eyepair_classifier = cv2.CascadeClassifier("haarcascade_mcs_eyepair_big.xml")
left_classifier = cv2.CascadeClassifier("haarcascade_lefteye_2splits.xml")
right_classifier = cv2.CascadeClassifier("haarcascade_righteye_2splits.xml")
eye_classifier = cv2.CascadeClassifier("haarcascade_eye.xml")
# --------
# FACES:
# Get rectangle for entire face
print("From faces:")
rects = detect(gray, face_classifier2, face_settings)
# Draw face rectangles
vis = image.copy()
draw_rects(vis, rects, (255, 0, 0))
# --------
# EYES:
# Look for eyes in each face rectangle
for i, (x1, y1, x2, y2) in enumerate(rects):
# Crop to just this face
grayface = gray[y1:y2, x1:x2]
visface = vis[y1:y2, x1:x2]
# Get rectangle for eye
print("From inside:")
all_classifiers = [left_classifier, right_classifier]
all_colors = [(50,255,50), (50,255,50)]
for classifier,color in zip(all_classifiers, all_colors):
subrects = detect(grayface, classifier, eye_settings)
draw_rects(visface, subrects, color)
cv2.imwrite("images/detected_faces_eyes.jpg", vis)
cv2.imshow('facedetect', vis)
cv2.waitKey()
def detect(img, cascade, settings):
"""
Given an image and a cascade object,
detect features.
"""
# Parameters taken verbatim from OpenCV 2.4 example:
# https://github.com/opencv/opencv/blob/2.4/samples/python2/facedetect.py
rects = cascade.detectMultiScale(img,
**settings)
if len(rects) == 0:
return []
print(rects)
# Turn width and height of box
# into plot-ready image coordinates.
# rects contains (x,y),(w,h), return (x,y),(x+w,x+h)
rects[:,2:] += rects[:,:2]
return rects
def img2np_pillow(filename):
"""
Load an image using Pillow
and convert it to a 3D Numpy array
with shape (W, H, 3)
"""
with Image.open(filename) as image:
nparr = np.fromstring(image.tobytes(), dtype=np.uint8)
nparr = im_arr.reshape((image.size[1], image.size[0], 3))
return nparr
def img2np_opencv(filename):
"""
Load an image using OpenCV
and convert it to a 3D Numpy array
with shape (W, H, 3)
"""
img = cv2.imread(filename)
return img
def draw_rects(img, rects, color):
"""
Use OpenCV to draw rectangles
on top of image
"""
for x1, y1, x2, y2 in rects:
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
if __name__=="__main__":
main()